【问题标题】:list wordpress posts from current category only仅列出当前类别中的 wordpress 帖子
【发布时间】:2012-10-12 05:25:13
【问题描述】:

我正在尝试在我的 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>

【问题讨论】:

  • 我想我家里有这段代码。如果您通过回答此评论来提醒我,我将分享它。会检查我是否也在收件箱中收到它。
  • 谢谢 Pablo,如果可以的话,这将非常有帮助。

标签: wordpress get posts


【解决方案1】:
<!--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');

【讨论】:

  • 谢谢巴鲁,但这似乎不起作用。你打算用codeforeach($myposts as $post) 做什么:
【解决方案2】:

所以我发现这段代码非常适合显示当前类别中的所有帖子。

 <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>

但我有几个要排除的类别。 有些帖子存在于两个类别中,我想排除在类别 8,9 和 11 中显示帖子。

有什么想法吗?

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      终于用这里的代码解决了这个问题: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>
      

      【讨论】:

        【解决方案5】:
        $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);
        

        它对我有用!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多