我有一个定制的配件贴类型。查看帖子时,还会显示相关帖子。它看起来很棒,但它也显示了相关帖子中的当前帖子。
有没有办法将当前帖子从循环中排除?
<div>
<?php
$category = get_the_category();
$model = $category[1]->cat_name;
$accessory = array(\'numberposts\' => 8, \'offset\'=> 1, \'post_type\' => \'accessory\', \'category_name\' => $model, \'order\' => \'DESC\');
query_posts( $accessory );
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div>
<?php wp_link_pages( array( \'before\' => \'<div class="page-link">\' . __( \'Pages:\', \'openeye\' ), \'after\' => \'</div>\' ) ); ?>
<h4><?php the_title(); ?></h4>
Part Number: <?php echo get_post_meta($post->ID, "accessory-part-number", true); ?>
<?php $desc = get_post_meta($post->ID, "accessory-description", true); ?>
<p><?php echo utf8_truncate( $desc ); ?></p>
<a href="<?php echo get_permalink(); ?>">Learn more about the <?php the_title(); ?></a>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
最合适的回答,由SO网友:Chris_O 整理而成
尝试以下操作:
为您的单个配件。php模板:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
// excludes this post from \'Related posts\' in the sidebar
$GLOBALS[\'current_id\'] = $post->ID;
?>
对于侧边栏或要显示相关帖子的位置:
<?php
if (is_singular(\'accessory\') ) :
global $post;
$categories = get_the_category();
$exclude = $GLOBALS[\'current_id\'];
$args = array(
\'post_type\' => \'accessory\',
\'post__not_in\' => array($exclude),
\'posts_per_page\' => -1
);
foreach ($categories as $category) :
$posts = get_posts($args);
if(count($posts) > 1) {
//do stuff
}
endforeach;
?>