admin管理员组文章数量:1325589
In 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
// How to put this part in get_template_part ?
$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();
// end get_template_part ?>
<?php get_footer(); ?>
How can I have the variables from category-about.php
in the template file posts-loop.php
?
Looking at this comment and this answer I have trouble putting it all together.
I would like to understand this better instead of using any of the helper functions provided in the answers. I understand that the correct way would be using set_query_var
and get_query_var
however I require a bit of assistance with that.
Instead of writing the core loop code for each category I like to just define the $args
in the category template and then make use of that in the template file. Any help is much appreciated.
In 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
// How to put this part in get_template_part ?
$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();
// end get_template_part ?>
<?php get_footer(); ?>
How can I have the variables from category-about.php
in the template file posts-loop.php
?
Looking at this comment and this answer I have trouble putting it all together.
I would like to understand this better instead of using any of the helper functions provided in the answers. I understand that the correct way would be using set_query_var
and get_query_var
however I require a bit of assistance with that.
Instead of writing the core loop code for each category I like to just define the $args
in the category template and then make use of that in the template file. Any help is much appreciated.
3 Answers
Reset to default 5In 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/passing-variables-to-get_template_part-in-wordpress/#comment-110459
https://wordpress.stackexchange/a/176807/77054
WordPress 5.5 allows passing $args
to get_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/core/2020/07/17/passing-arguments-to-template-files-in-wordpress-5-5/
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.
本文标签: get template partPass a variable to gettemplatepart
版权声明:本文标题:get template part - Pass a variable to get_template_part 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742189770a2429995.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论