admin管理员组文章数量:1312655
I have created a custom loop using WP_Query
to show posts associated to a custom post type I created.
<?php
$the_query = new WP_Query( array (
'post_type' => 'scp_content'
));
?>
<?php
if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<section id="post-<?php the_id(); ?>" class="main-content">
<h2><?php the_field( 'section_heading' ); ?></h2>
<div class="container-fluid col-lg-9">
<p><?php the_field( 'section_content' ); ?></p>
</div>
</section>
<?php endwhile; endif; wp_reset_postdata(); ?>
Each post has one specific category assigned and all categorized posts can be viewed when the said category is clicked via a category menu that I created.
The issue: Regardless of which category I click, I am shown all posts. I have tried implementing: get_category();
, get_query_var();
, get_the_category();
and is_category();
within my loop to try and specify posts shown via current category selected but to no success.
Assuming I am using these function incorrectly, how can I show the current category posts when a user clicks on the specific category in the menu?
I have created a custom loop using WP_Query
to show posts associated to a custom post type I created.
<?php
$the_query = new WP_Query( array (
'post_type' => 'scp_content'
));
?>
<?php
if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<section id="post-<?php the_id(); ?>" class="main-content">
<h2><?php the_field( 'section_heading' ); ?></h2>
<div class="container-fluid col-lg-9">
<p><?php the_field( 'section_content' ); ?></p>
</div>
</section>
<?php endwhile; endif; wp_reset_postdata(); ?>
Each post has one specific category assigned and all categorized posts can be viewed when the said category is clicked via a category menu that I created.
The issue: Regardless of which category I click, I am shown all posts. I have tried implementing: get_category();
, get_query_var();
, get_the_category();
and is_category();
within my loop to try and specify posts shown via current category selected but to no success.
Assuming I am using these function incorrectly, how can I show the current category posts when a user clicks on the specific category in the menu?
Share Improve this question asked Nov 8, 2016 at 1:45 WeebleWobbWeebleWobb 273 silver badges11 bronze badges 1 |1 Answer
Reset to default 0Use the function get_the_category();
inside the WordPress loop and pass the get_the_ID()
function which will get the current post ID in the loop.
get_the_ID()
will return the post id and whereas the the_id();
will just display.
if( $the_query->have_posts() ) : while( $the_query->have_posts() ) : $the_query->the_post();
$category_array = get_the_category( get_the_ID() );
print_r( $category_array );
endwhile;
endif;
wp_reset_postdata();
Hopefully, the above code will solve the issue.
本文标签: Only show current category post
版权声明:本文标题:Only show current category post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741913130a2404564.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
has_archive
to true, you don't need the custom query you have above. – Milo Commented Nov 8, 2016 at 2:23