我正在创建一个wordpress主题。我已经将所有类别动态添加到顶部菜单中。现在,当我点击任何菜单项时,我想要该类别下的所有帖子。但是代码获取所有类别的所有帖子。这是我的代码。
<?php
global $wp_query;
$id = $wp_query->post->ID;
$the_query = new WP_Query( $id );
$count = 0;
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();?>
<div class="page-header"><h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3></div>
<?php
$count++;
}
} else {?>
no posts found
<?php }
/* Restore original Post Data */
wp_reset_postdata();
?>
我如何解决问题。
答案 0 :(得分:0)
试试这个
<?php
global $wp_query;
$id = $wp_query->post->ID;
$the_query = new WP_Query( $id );
$count = 0;
// The Loop
query_posts('cat=22'); #enter your category id here
if ( $the_query->have_posts() ) {
while ($the_query->have_posts()) : the_post();
$the_query->the_post();?>
<div class="page-header"><h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>
</div>
<?php
$count++;
endwhile;
}
else
{
?>
no posts found
<?php
}
/* Restore original Post Data */
wp_reset_postdata();
?>
答案 1 :(得分:0)
$the_query = new WP_Query('cat='.$id );