在 WordPress 中显示来自特定类别的最新帖子

问题描述

我不知道如何在 wordpress 中仅显示最近帖子的特定类别帖子。 我使用此代码并且其工作正常,但我需要显示来自名为“站点”的自定义分类)类别的帖子

<?PHP $orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

$args=array(
'category__in' => $category_ids,'post__not_in' => array($post->ID),'posts_per_page'=> 4,// Number of related posts that will be shown.
'ignore_sticky_posts'=>1
);

$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related_posts"><h3>Related Videos</h3><ul>';
while( $my_query->have_posts() ) {
$my_query->the_post();?>

<li><div class="relatedthumb"><a href="<?PHP the_permalink()?>" rel="bookmark" title="<?PHP the_title(); ?>"><?PHP the_post_thumbnail(); ?></a></div>
<div class="relatedcontent">
<h3><a href="<?PHP the_permalink()?>" rel="bookmark" title="<?PHP the_title(); ?>"><?PHP the_title(); ?></a></h3>

</div>
</li>
<?
}
echo '</ul></div>';
}
}
$post = $orig_post;
wp_reset_query(); ?>
 

解决方法

我认为你必须填写 $category_ids = array();使用您的类别 ID。只需在面板中检查您的类别 ID 并将其放入此数组中,例如 $category_ids = array(25);