
网络开发中的Git:了解一个项目的典型工作流程
wp_print_community_events_markup ( No parameters )
wp_print_community_events_markup: 这个动作用于打印WordPress中社区活动小工具的标记: 当小工具被加载时,它将在页面的页脚被触发。
为事件和新闻仪表盘小工具的社区活动部分打印标记。
function wp_print_community_events_markup() { ?> <div class="community-events-errors notice notice-error inline hide-if-js"> <p class="hide-if-js"> <?php _e( 'This widget requires JavaScript.' ); ?> </p> <p class="community-events-error-occurred" aria-hidden="true"> <?php _e( 'An error occurred. Please try again.' ); ?> </p> <p class="community-events-could-not-locate" aria-hidden="true"></p> </div> <div class="community-events-loading hide-if-no-js"> <?php _e( 'Loading…' ); ?> </div> <?php /* * Hide the main element when the page first loads, because the content * won't be ready until wp.communityEvents.renderEventsTemplate() has run. */ ?> <div id="community-events" class="community-events" aria-hidden="true"> <div class="activity-block"> <p> <span id="community-events-location-message"></span> <button class="button-link community-events-toggle-location" aria-expanded="false"> <span class="dashicons dashicons-location" aria-hidden="true"></span> <span class="community-events-location-edit"><?php _e( 'Select location' ); ?></span> </button> </p> <form class="community-events-form" aria-hidden="true" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" method="post"> <label for="community-events-location"> <?php _e( 'City:' ); ?> </label> <?php /* translators: Replace with a city related to your locale. * Test that it matches the expected location and has upcoming * events before including it. If no cities related to your * locale have events, then use a city related to your locale * that would be recognizable to most users. Use only the city * name itself, without any region or country. Use the endonym * (native locale name) instead of the English name if possible. */ ?> <input id="community-events-location" class="regular-text" type="text" name="community-events-location" placeholder="<?php esc_attr_e( 'Cincinnati' ); ?>" /> <?php submit_button( __( 'Submit' ), 'secondary', 'community-events-submit', false ); ?> <button class="community-events-cancel button-link" type="button" aria-expanded="false"> <?php _e( 'Cancel' ); ?> </button> <span class="spinner"></span> </form> </div> <ul class="community-events-results activity-block last"></ul> </div> <?php }
在WordPress中,你可以使用 add_action
函数添加一个钩子函数。钩子函数是在特定事件发生时自动触发的函数。
add_action
函数的第一个参数是钩子名称,第二个参数是要执行的函数名,第三个参数是函数的优先级(可选)。
例如,添加一个在文章保存后执行的钩子函数的代码如下:
function my_save_post_function( $post_ID, $post ) { // 执行你的代码 } add_action( 'save_post', 'my_save_post_function', 10, 2 );
这里, save_post
是钩子名称, my_save_post_function
是要执行的函数名,10是函数的优先级,2是传递给钩子函数的参数数量。
wp_reset_query()
函数用于重置WordPress的查询。在自定义查询或修改查询参数之后,你可能需要使用这个函数来重置到默认的查询。例如:
// 自定义查询 $args = array( 'post_type' => 'product', 'posts_per_page' => 10, ); $custom_query = new WP_Query($args); // 重置查询 wp_reset_query();
创建自定义的WordPress插件需要以下步骤:
使用 get_post
和 get_page
获取单个文章或页面的详细信息:
// 获取文章或页面的ID $post_id = get_the_ID(); // 获取文章或页面的详细信息 $post = get_post($post_id); // 获取文章或页面的标题 $title = $post->post_title; // 获取文章或页面的内容 $content = $post->post_content;
wp_enqueue_script
和 wp_enqueue_style
函数的使用:
wp_enqueue_script()
:此函数位于wp-includes/script-loader.php文件中。它会将JavaScript文件添加到WordPress网站的页脚中。您可以在主题的functions.php文件中调用此函数,以添加自定义的JavaScript文件。wp_enqueue_style()
:此函数位于wp-includes/script-loader.php文件中。它会将CSS文件添加到WordPress网站的部分中。您可以在主题的functions.php文件中调用此函数,以添加自定义的CSS文件。使用 wp_head
和 wp_footer
在主题中添加自定义内容:
wp_head()
:此函数位于wp-includes/general-template.php文件中。它会在文档的部分中输出内容。您可以在主题的header.php文件中调用此函数,以在部分中添加自定义内容。wp_footer()
:此函数位于wp-includes/general-template.php文件中。它会在文档的部分中输出内容。您可以在主题的footer.php文件中调用此函数,以在部分中添加自定义内容。