填充全局$wp_the_query的过程

问题描述 投票:0回答:1

我想知道怎么做 do_action( 'template_redirect' );正在填充 [queried_object] => WP_Term 对象 ( [term_id] => 43 [名称] => 媒体 [slug] => 媒体 [术语组] => 0 [term_taxonomy_id] => 42 [分类] => 类别 [描述] => [父] => 0 [计数] => 0 [过滤器] => 原始 [猫ID] => 43 [类别计数] => 0 [类别描述] => [猫名] => 媒体 [category_nicename] => 媒体 [类别_父级] => 0 ) 在全局 $wp_the_query

if ( wp_using_themes() ) {
    /**
     * Fires before determining which template to load.
     *
     * @since 1.5.0
     */
    echo "checking global before in template_redirect";
global $wp_the_query;
print_r($wp_the_query);
echo "checking global before in template_redirect";


    do_action( 'template_redirect' );

    echo "checking global after template_redirect ";
    print_r($wp_the_query);
    echo "checking global after template_redirect ";
}

我在 do_action( 'template_redirect' ); 之前打印了一个查询,在 do_action( 'template_redirect' ); 之后打印了一个查询。删除 do_action( 'template_redirect' );清空 $wp_the_query->queried_object

php wordpress woocommerce wordpress-plugin-creation wordpress-hook
1个回答
0
投票

当 wordpress 运行时,它需要弄清楚在屏幕上显示什么,这是由称为

$wp_the_query

的东西控制的
  • template_redirect
    之前:
    $wp_the_query
    开始收集有关应显示内容的信息,例如类别或帖子,但尚未完成。
  • do_action('template_redirect')
    :这是一个告诉Wordpress“在显示页面之前完成所有准备工作”的命令,它有助于填写
    $wp_the_query
    中缺少的细节,特别是名为
    queried_object
    的部分。
  • template_redirect
    之后:现在
    $wp_the_query
    已经拥有了所有需要的东西,比如要显示哪个类别,如果删除此步骤
    $wp_the_query->queried_object
    可能会保持为空或不完整。

因此该操作非常重要,因为它确保 Wordpress 准备好在屏幕上显示正确的内容

© www.soinside.com 2019 - 2024. All rights reserved.