更改二级循环以将帖子从当前页面类别中排除

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

下面的代码取自侧栏。php,它从多个类别中输出随机帖子,并将当前帖子排除在外。这很好,但我如何改变这个循环,将所有帖子从当前页面类别中排除?因此,例如,如果一篇文章当前正在显示,而其类别是艺术家,我希望排除所有艺术家的文章,但显示所有其他类别(在下面的代码中说明)。

这是我的循环:

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

        // 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(
                \'posts_per_page\' => 3,
                \'orderby\'       => \'rand\',
                \'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();?>

                    <?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; endif;

            // Be kind; rewind
            wp_reset_postdata();

        } else { // display nothing ?>


        <?php }?>

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

这个更简单。请参阅WP_Query() category parameters. 我想你会想用\'category__not_in\'.

foreach ( $random_posts_cat_array as $random_post_cat ) {
    if ( $post_cat_slug == $random_post_cat ) {
        // No posts from this category!
        $random_posts_query_args[\'category__not_in\'] = $post_cat_id;
    }
}
编辑,例如,如果一篇文章当前正在显示,其类别是艺术家,我想排除所有艺术家的文章,但显示所有其他类别(在下面的代码中说明)。

好吧,这实际上有点不同,我想你应该\'category__in\' 而不是\'category__not_in\':

// Array to hold categories to include
$cats_to_include = array();

// Loop through category slugs, and
// add their IDs to this array
foreach ( $random_posts_cats_array as $random_post_cat ) {
    $random_post_cat_obj = get_category_by_slug( $random_post_cat );
    $cats_to_include[] = $random_post_cat_obj->term_id;
}

// unset current cat
// call this after you define $post_cat_id
if ( in_array( $post_cat_id, $cats_to_include ) ) {
    unset( $cats_to_include[$post_cat_id] );
}

// Now build custom query args
$random_posts_query_args[\'category__in\'] = $cats_to_include;
编辑2完整的代码(为了清晰起见,稍微清理一下):

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

// 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(
        \'posts_per_page\' => 3,
        \'orderby\'       => \'rand\',
        \'post__not_in\'  => array( $post->ID )
    );

    // Array to hold categories to include
    $cats_to_include = array();

    // Loop through category slugs, and
    // add their IDs to this array
    foreach ( $random_posts_cats_array as $random_post_cat ) {
        $random_post_cat_obj = get_category_by_slug( $random_post_cat );
        $cats_to_include[] = $random_post_cat_obj->term_id;
    }

    // unset current cat
    // call this after you define $post_cat_id
    if ( in_array( $post_cat_id, $cats_to_include ) ) {
        unset( $cats_to_include[$post_cat_id] );
    }

    // Now build custom query args
    $random_posts_query_args[\'category__in\'] = $cats_to_include;

    // 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();

        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>\';
                }

                // 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; endif;

    // Be kind; rewind
    wp_reset_postdata();

} 
?>
编辑3好的,这就是为什么没有从category__in 阵列:

if ( in_array( $post_cat_id, $cats_to_include ) ) {
    unset( $cats_to_include[$post_cat_id] );
}
但这里是数组结构:

["category__in"]=> array(5) { 
    [0]=> int(1) 
    [1]=> int(6) 
    [2]=> int(3) 
    [3]=> int(5) 
    [4]=> int(7) 
}
看到问题了吗?我们查看的是键,而不是值。因此,请尝试以下方法:

foreach ( $cats_to_include as $cat_key => $cat_value ) {
    if ( $cat_value == $post_cat_id ) {
        unset( $cats_to_include[$cat_key] );
    }
}
试试看,然后报告var_dump() 再一次,我们可以看看$cats_to_include 现在可以正确更新。

结束

相关推荐

WooCommerce-通过JS或PHP以编程方式将产品添加到购物车

我使用Woocommerce插件来促进网站的一小部分电子商务,需要通过一些调用或功能将产品添加到购物车,而不是使用自己的“添加到购物车”按钮。我的基本意思是向Woocommerce发送SKU和数量,并更新购物车。sendToCart(\'123456\', 55); 等我查看了文档,似乎找不到这类事情的参考。有人能建议我如何做到这一点吗?