[英]How to display on a single.php all posts of category, in which this single.php is situated
例如,我有一些类别,其中包括五个帖子。 当我选择该帖子时,需要显示其所属类别中的其余四个帖子。
<?php
$infocat = get_the_category();
$info = $infocat[0]->cat_ID;
$array = "orderby=rand&showposts=10&cat=$info";
query_posts($array);
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<a class="permlinccat" href="<?php the_permalink() ?>" title="Перейти к посту: <?php the_title(); ?>" ><?php the_title(); ?></a>
<?php endwhile; else: ?>
<?php endif; wp_reset_query(); ?>
尝试了此功能-几乎不错,但仅获取帖子标题
找到了解决方案...
<?php
$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),
'showposts'=>5 //
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Current category</h3><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title=" <?php the_title_attribute(); ? >"><?php the_title(); ?>
<img src="<?php echo first_image() ?>"title="<?php the_title(); ?>" alt="<?php the_title(); ?>"/>
</a>
<?php
}
echo '</ul>';
}
}
?>
试试这个:)只需获取当前类别并从查询中排除当前帖子即可。
<?php
$cat = get_the_category();
$catOK = $cat[0]->cat_ID; //if multiple categories
$postID = $post->ID;
query_posts(
array(
'cat' => $catOK,
'post__not_in' => array($postID)
)
);
while (have_posts()) : the_post(); ?>
YOUR HTML CODE
<?php endwhile; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.