【问题标题】:list wordpress posts from current category and列出当前类别的 wordpress 帖子和
【发布时间】:2018-07-24 22:33:19
【问题描述】:

在此处使用此代码:http://www.snilesh.com/resources/wordpress/wordpress-recent-posts-from-current-same-category/

<h3>Recently Post From Same Category</h3>
<ul>
<?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__not_in' =>
array($post->ID),'post_status'=>'publish'));
foreach($myposts as $post) :
setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?></a>
</li>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
</ul>

我能够显示当前类别中的所有帖子。我要显示的是当前类别和类别 186 中的所有帖子。

【问题讨论】:

    标签: php wordpress posts


    【解决方案1】:

    如果您想显示同时属于当前类别类别 186 的帖子,请改用 category__and

    $myposts = get_posts(array(
        'numberposts' => 5,
        'offset' => 0,
        'category__and' => array($category, 186),
        'post__not_in' => array($post->ID),
        'post_status'=>'publish'
    ));
    

    更多详情:WP_Query - Category Parameters

    P.S.:get_posts() 内部使用 WP_Query 类来获取帖子,因此其参数是相同的。

    【讨论】:

      猜你喜欢
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      相关资源
      最近更新 更多