从sidesbar.php的循环中排除当前帖子ID

时间:2012-05-31 作者:egr103

目前,下面所有代码都显示了来自当前页面所在类别的3篇随机帖子。因此,如果当前页面属于“艺术家”类别,例如,请在页面的下面显示“艺术家”类别中的3篇随机帖子。我的问题是,当前帖子一直出现在这一部分中,我想将其排除在外,但无法使用我当前的循环来解决此问题。这是我的循环:

<?php if ( in_category( \'artists\' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts(\'showposts=3&cat=1,-12&orderby=rand\'); ?>

        <?php } ?>

        <?php if ( in_category( \'people\' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts(\'showposts=3&cat=3&orderby=rand\'); ?>

        <?php } ?>

        <?php if ( in_category( \'development\' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts(\'showposts=3&cat=5&orderby=rand\'); ?>

        <?php } ?>

        <?php if ( in_category( \'offsite\' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts(\'showposts=3&cat=7&orderby=rand\'); ?>

        <?php } ?>

        <?php if ( in_category( \'projects\' ) ) {  ?>
                <h2>Similar articles</h2>
                <?php // Display the 3 random posts from page category
                query_posts(\'showposts=3&cat=6&orderby=rand\'); ?>

        <?php } ?>

        <?php if ( in_category( \'contact\' ) ) : // Display nothing ?>

            <?php // Else, show boxes
            else : ?>

                <?php while (have_posts()) : the_post(); ?>

                    <?php foreach( get_the_category() as $cat ) echo \'<div class="module \' . $cat->slug . \'" data-category="\' . $cat->slug . \'" >\'; ?>

                          <a href="<?php the_permalink()?>" title="<?php the_title(); ?>">
                          <div class="active">
                                <div class="hover">    </div>
                                <?php the_post_thumbnail(); ?>
                          </div>

                         <?php
                              $sub_title=get_post_meta($post->ID,\'subtitle\',true);
                              if($sub_title != \'\') {
                              echo \'<h1>\'. get_the_title() .\'<span> / \'. $sub_title .\'</span></h1>\';
                              } else {
                              echo \'<h1>\'. get_the_title() .\'</h1>\';
                              }
                              ?>

                              <?php
                              // Call in the contents of a custom field called Excerpt and if custom field in admin panel is empty don\'t display <p> tags otherwise wrap contents in <p> tags
                              $excerpt=get_post_meta($post->ID,\'Excerpt\',true);
                              if($excerpt != \'\') {
                              echo \'<p>\'. $excerpt .\'</p>\';
                              } else {
                              echo \' \';
                              }
                              ?>
                              <p class="date"><?php the_time(\'YdmHi\') ?></p>
                         </a>
                    </div>

                <?php endwhile;?>

        <?php endif; ?>

2 个回复
最合适的回答,由SO网友:Chip Bennett 整理而成

第一do not use query_posts() for secondary loops. 这个query_posts() 函数仅用于修改主循环查询。使用WP_Query()get_posts() 用于辅助循环查询。

而且showposts 已弃用。使用posts_per_page 相反

让我们使用WP_Query(), 因为它与您当前的实现最为相似:

<?php
// First, let\'s eliminate some DRY,
// by making an array of our categories
$random_posts_cat_array = array( \'people\', \'development\', \'offsite\', \'contact\' );

// Globalize $post,
// since we\'re outside the primary loop
global $post;
$post_cats = get_the_category( $post->ID );
// First array object
$post_cat = $post_cats[0];
// Current post category ID
$post_cat_id = $post_cat->term_id;
// Current post category slug
$post_cat_slug = $post_cat->slug;

// Now, let\'s find out if we\'re displaying
// the category index for one of our categories
if ( in_array( $post_cat_slug, $random_posts_cat_array ) ) {

    // Set up custom loop args
    $random_posts_query_args = array(
        // Only 3 posts
        \'posts_per_page\' => 3,
        // Ordered randomly
        \'orderby\'       => \'rand\',
        // Exclude current post
        \'post__not_in\'  => array( $post->ID )
    );
    // Add Cat ID to custom loop args
    foreach ( $random_posts_cat_array as $random_post_cat ) {
        if ( $post_cat_slug == $random_post_cat ) {
            // Add Cat ID
            $random_posts_query_args[\'cat\'] = $post_cat_id; 
        }
    }

    // Run random posts query
    $random_posts_query = new WP_Query( $random_posts_query_args );

    // Setup random posts query loop
    if ( $random_posts_query->have_posts() ) : while ( $random_posts_query->have_posts() ) : $random_posts_query->the_post();
        // Loop output goes here
    endwhile; endif;

    // Be kind; rewind
    wp_reset_postdata();

} else {
    // Alternate output goes here, if any
}

SO网友:Bruce Pearson

我想您需要使用“post\\uu not\\u in”参数。例如,您的第一个查询如下:

query_posts(array(\'showposts\'=>3,\'cat\'=>array(1,-12),\'orderby\'=>\'rand\',
\'post__not_in\' => array($id)));
您需要添加一个全局$id;也有。

结束

相关推荐

Quote rotator in the sidebar

我喜欢在帖子编辑器中设计一些帖子(加粗和链接)(简单而有趣),并将其归类为“引用”。然后在侧边栏中基于CSS显示它,随机旋转它,如每5秒显示1。你知道一个插件可以做到这一点吗?