6

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

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

我一直在尝试使用 get_posts 函数,但正在努力寻找如何动态选择当前类别。即在这里放置什么category=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>
4

5 回答 5

9

最后用这里的代码解决了这个问题: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>
于 2012-10-24T10:49:13.870 回答
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');
于 2012-10-22T12:00:24.527 回答
2
$args=array(
'cat' => get_query_var('cat'),
  'orderby' => 'title',
  'order' => 'ASC',
  'posts_per_page'=>-1,
  'caller_get_posts'=>1
);
$my_query = new WP_Query($args);

它对我有用!

于 2013-07-15T10:05:16.140 回答
0

So I found this bit of code which works great in showing all posts in the current category.

 <ul id="catnav">

 <?php
foreach( ( get_the_category() ) as $category ) {
$the_query = new WP_Query('category_name=' . $category->category_nicename . '&showposts=5&order=ASC');
while ($the_query->have_posts()) : $the_query->the_post();
?>
            <li>
                <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
            </li>
<?php endwhile; ?>
<?php
}
?>


</ul>

But I have several categories I wish to exclude. Some posts exist in two categories I want to exclude showing the post in categories 8,9 and 11.

Any ideas?

于 2012-10-23T10:01:10.500 回答
0

我认为最好从类别 id 而不是类别名称中获取帖子,这样您就可以编写 if else 条件,并且可以排除 id 为 8、9、11 的帖子

于 2012-10-23T11:40:32.070 回答