将变量传递给get_template_part
3 个回答
- 投票数
-
- 2017-06-14
在
category-about.php
中,我有<?php /** * The template for displaying the posts in the About category * */ ?> <!-- category-about.php --> <?php get_header(); ?> <?php $args = array( 'post_type' => 'post', 'category_name' => 'about', ); ?> <?php // important bit set_query_var( 'query_category', $args ); get_template_part('template-parts/posts', 'loop'); ?> <?php get_footer(); ?>
和我现在拥有的模板文件
posts-loops.php
中<!-- posts-loop.php --> <?php // important bit $args = get_query_var('query_category'); $cat_name = $args['category_name']; $query = new WP_Query( $args ); if($query->have_posts()) : ?> <section id="<?php echo $cat_name ?>-section"> <h1 class="<?php echo $cat_name ?>-section-title"> <strong><?php echo ucfirst($cat_name) ?> Section</strong> </h1><?php while($query->have_posts()) : $query->the_post(); ?> <strong><?php the_title(); ?></strong> <div <?php post_class() ?> > <?php the_content(); ?> </div> <?php endwhile; ?> </section> <?php endif; wp_reset_query();
,并且有效.
参考:
http://keithdevon.com/passing-variables-to-get_template_part-in-wordpress/#comment-110459 https://wordpress.stackexchange.com/a/176807/77054In
category-about.php
I have<?php /** * The template for displaying the posts in the About category * */ ?> <!-- category-about.php --> <?php get_header(); ?> <?php $args = array( 'post_type' => 'post', 'category_name' => 'about', ); ?> <?php // important bit set_query_var( 'query_category', $args ); get_template_part('template-parts/posts', 'loop'); ?> <?php get_footer(); ?>
and in template file
posts-loops.php
I now have<!-- posts-loop.php --> <?php // important bit $args = get_query_var('query_category'); $cat_name = $args['category_name']; $query = new WP_Query( $args ); if($query->have_posts()) : ?> <section id="<?php echo $cat_name ?>-section"> <h1 class="<?php echo $cat_name ?>-section-title"> <strong><?php echo ucfirst($cat_name) ?> Section</strong> </h1><?php while($query->have_posts()) : $query->the_post(); ?> <strong><?php the_title(); ?></strong> <div <?php post_class() ?> > <?php the_content(); ?> </div> <?php endwhile; ?> </section> <?php endif; wp_reset_query();
and it works.
Reference:
http://keithdevon.com/passing-variables-to-get_template_part-in-wordpress/#comment-110459 https://wordpress.stackexchange.com/a/176807/77054 -
- 2019-01-23
第一种方式:
包含模板部分的模板:
$value_to_sent = true; set_query_var( 'my_var', $value_to_sent );
包含的模板部分:
$get_my_var = get_query_var('my_var'); if ($get_my_var == true) { ... }
第二种方式:
包含模板部分的模板:
global $my_var; $my_var= true;
包含的模板部分:
global $my_var; if ($my_var == true) { ... }
我宁愿采用第一种方法.First way:
Template in which template part is included:
$value_to_sent = true; set_query_var( 'my_var', $value_to_sent );
Included template part:
$get_my_var = get_query_var('my_var'); if ($get_my_var == true) { ... }
Second way:
Template in which template part is included:
global $my_var; $my_var= true;
Included template part:
global $my_var; if ($my_var == true) { ... }
I would rather go with the first way. -
- 2020-08-13
WordPress 5.5 允许将
$args
传递给get_template_part
.除了此功能外,还有一些模板函数可以接受参数.get_header get_footer get_sidebar get_template_part_{$slug} get_template_part
请参阅以下详细信息: https://make.wordpress.org/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/ >
WordPress 5.5 allows passing
$args
toget_template_part
. Apart from this function there are sets of template function which can now accept arguments.get_header get_footer get_sidebar get_template_part_{$slug} get_template_part
Please refer more details at: https://make.wordpress.org/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/
在
category-about.php
中,我有如何从模板文件
category-about.php
中的category-about.php
中获取变量?查看此评论和<一个href=" https://wordpress.stackexchange.com/questions/176804/passing-a-variable-to-get-template-part">此答案,我很难将它们放在一起.
我想更好地理解这一点,而不是使用答案中提供的任何帮助程序功能.我知道正确的方法是使用
set_query_var
和get_query_var
,但是我需要一些帮助.我不想只为每个类别编写核心循环代码,而是想在类别模板中定义
$args
,然后在模板文件中使用它.非常感谢您的帮助.