仅列出当前类别的wordpress帖子

use*_*225 6 wordpress get posts

我正在尝试在我的wordpress网站上创建第二个导航菜单.

我希望这只显示当前类别中所有帖子的链接.

我一直在试验get_posts函数,但我很难找到如何动态选择当前类别.即在这里放置什么类别= x

任何帮助是极大的赞赏

这是我一直在使用的模板代码

<ul id="catnav">

     <?php
     global $post;
     $myposts = get_posts('numberposts=5&category=1');
     foreach($myposts as $post) :
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>



    </ul>
Run Code Online (Sandbox Code Playgroud)

use*_*225 8

最后用这里的代码解决了这个问题:http://www.snilesh.com/resources/wordpress/wordpress-recent-posts-from-current-same-category/

修改它以包括当前页面和列表升序

<ul id="catnav">
<?php
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post_status'=>'publish', 'order'=>'ASC' ));
foreach($myposts as $post) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
<li><a href="?p=46">Why Us?</a></li>

</ul>
Run Code Online (Sandbox Code Playgroud)


小智 4

<!--Insted Of this-->
$myposts = get_posts('numberposts=5&category=1');
<!--Use This-->
$cat_ID = get_query_var('cat');
query_posts('cat='.$cat_ID.'&showposts=5&order=ASC');
Run Code Online (Sandbox Code Playgroud)