将特定帖子包括到QUERY_POSTS中,如果该帖子已在返回列表中则将其删除

时间:2012-12-21 作者:Christine Cooper

与我赤裸。首先,这是我的特色滑条文章的完整脚本:

<div id="featured" class="<?php if ( $responsive ) echo \'flexslider\' . $featured_auto_class; else echo \'et_cycle\'; ?>">
<a id="left-arrow" href="#"><?php esc_html_e(\'Previous\',\'Aggregate\'); ?></a>
<a id="right-arrow" href="#"><?php esc_html_e(\'Next\',\'Aggregate\'); ?></a>

<?php if ( $responsive ) { ?>
    <ul class="slides">
<?php } else { ?>
    <div id="slides">
<?php } ?>
<?php global $ids;
$ids = array();
$arr = array();
$i=0;

$featured_cat = get_option(\'aggregate_feat_cat\');
$featured_num = (int) get_option(\'aggregate_featured_num\');

if (get_option(\'aggregate_use_pages\') == \'false\') query_posts("showposts=$featured_num&top=billboard"); // set which slug in custom taxonomy TOP it should filter
else {
    global $pages_number;

    if (get_option(\'aggregate_feat_pages\') <> \'\') $featured_num = count(get_option(\'aggregate_feat_pages\'));
    else $featured_num = $pages_number;

    query_posts(array
    (\'post_type\' => \'page\',
        \'orderby\' => \'menu_order\',
        \'order\' => \'ASC\',
        \'post__in\' => (array) get_option(\'aggregate_feat_pages\'),
        \'showposts\' => (int) $featured_num
    ));
} ?>
<?php if (have_posts()) : while (have_posts()) :
    global $post;
    if (!$first_time) // START custom first post
    {
        $post_id = 105; // This is the ID of the first post to be displayed on slider
        $post = get_post($post_id);
        $first_time = 1;
    }
    else the_post(); // END custom first post
    ?>
    <?php if ( $responsive ) { ?>
            <li class="slide">
        <?php } else { ?>
            <div class="slide">
        <?php } ?>
    <?php
    $width = $responsive ? 960 : 958;
    $height = 340;
    $small_width = 95;
    $small_height = 54;
    $titletext = get_the_title();

    $thumbnail = get_thumbnail($width,$height,\'\',$titletext,$titletext,false,\'Featured\');

    $arr[$i][\'thumbnail\'] = get_thumbnail($small_width,$small_height,\'\',$titletext,$titletext,false,\'Small\');
    $arr[$i][\'titletext\'] = $titletext;

    $thumb = $thumbnail["thumb"];
    print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, \'\'); ?>
    <div class="featured-top-shadow"></div>
    <div class="featured-bottom-shadow feat<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"></div>
    <div class="featured-description">
        <div class="feat_desc">
            <h2 class="featured-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,20); ?></p>
        </div>
    </div> <!-- end .description -->
    <?php if ( $responsive ) { ?>
            </li> <!-- end .slide -->
    <?php } else { ?>
            </div> <!-- end .slide -->
    <?php } ?>
    <?php $ids[] = $post->ID; $i++; endwhile; endif; wp_reset_query(); ?>
<?php if ( $responsive ) { ?>
    </ul> <!-- end .slides -->
<?php } else { ?>
</div> <!-- end #slides -->
<?php } ?>
</div> <!-- end #featured -->
It部门query_posts 自定义分类法中的一组帖子top=billboard. 代码的以下部分:

<?php if (have_posts()) : while (have_posts()) :
    global $post;
    if (!$first_time) // START custom first post
    {
        $post_id = 105; // This is the ID of the first post to be displayed on slider
        $post = get_post($post_id);
        $first_time = 1;
    }
    else the_post(); // END custom first post
    ?>
添加帖子ID105 返回列表的第一个帖子。这允许我控制首先显示哪个帖子。The problem is: If post ID105 已包含在自定义分类中top=billboard, 然后它将在列表中出现两次。。。所以有一次,因为它是在上面添加的custom first post 一次,因为它是在自定义分类法中添加的top=billboard.

如何调整代码,以便在发布ID时105 存在于自定义分类中top=billboard, 那么它应该not 出现两次(仅作为第一篇帖子)。

2 个回复
SO网友:Tom J Nowell

好的,所以我尝试读取您的代码,但失败了,并将其重新格式化为当前无法读取,任何错误都会被这种无法读取性所掩盖。我无法强调正确缩进和遵循标准化格式的重要性,以使您的生活更加轻松:

<?php
$featured_class = \'et_cycle\';
$tag = \'ul\';
$attr = \'class\';
if($responsive){
    $featured_class = \'flexslider\' . $featured_auto_class;
    $tag = \'div\';
    $attr = \'id\';
}

?>
<div id="featured" class="<?php echo $featured_class; ?>">
    <a id="left-arrow" href="#"><?php esc_html_e( \'Previous\', \'Aggregate\' ); ?></a>
    <a id="right-arrow" href="#"><?php esc_html_e( \'Next\', \'Aggregate\' ); ?></a>
    <?php


    echo \'<\'.$tag.\' \'.$attr.\'="slides">\';

    global $ids;
    $ids = array();
    $arr = array();
    $i=0;

    $featured_cat = get_option( \'aggregate_feat_cat\' );
    $featured_num = (int) get_option( \'aggregate_featured_num\' );

    $query_args = array();
    if ( get_option( \'aggregate_use_pages\' ) == \'false\' ){
        // set which slug in custom taxonomy TOP it should filter
        $query_args = array(
            \'showposts\' => $featured_num,
            \'top\' => \'billboard\'
        );
    }else {
        global $pages_number;

        if ( get_option( \'aggregate_feat_pages\' ) <> \'\' ){
            $featured_num = count( get_option( \'aggregate_feat_pages\' ) );
        } else{
            $featured_num = $pages_number;
        }
        query_args = array(
                \'post_type\' => \'page\',
                \'orderby\' => \'menu_order\',
                \'order\' => \'ASC\',
                \'post__in\' => (array) get_option( \'aggregate_feat_pages\' ),
                \'showposts\' => (int) $featured_num
        );
    }

    // needs to die, use WP_Query instead
    query_posts($query_args);


    if ( have_posts() ) {
        while ( have_posts() ) {
            global $post;
            // START custom first post
            if ( !$first_time ) {
                $post_id = 105; // This is the ID of the first post to be displayed on slider
                $post = get_post( $post_id );
                $first_time = 1;
            }else{
                the_post(); // END custom first post
            }
            if ( $responsive ) {
                echo \'<li\';
            }else {
                echo \'<div\';
            }
            echo \' class="slide">\';
            $width = $responsive ? 960 : 958;
            $height = 340;
            $small_width = 95;
            $small_height = 54;
            $titletext = get_the_title();

            $thumbnail = get_thumbnail( $width, $height, \'\', $titletext, $titletext, false, \'Featured\' );

            $arr[$i][\'thumbnail\'] = get_thumbnail( $small_width, $small_height, \'\', $titletext, $titletext, false, \'Small\' );
            $arr[$i][\'titletext\'] = $titletext;

            $thumb = $thumbnail["thumb"];
            print_thumbnail( $thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, \'\' );
            ?>
            <div class="featured-top-shadow"></div>
            <div class="featured-bottom-shadow feat<?php $category = get_the_category(); echo $category[0]->category_nicename; ?>"></div>
            <div class="featured-description">
                <div class="feat_desc">
                    <h2 class="featured-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
                    <p><?php $excerpt = get_the_excerpt(); echo string_limit_words( $excerpt, 20 ); ?></p>
                </div>
            </div> <!-- end .description -->
                <?php if ( $responsive ) { ?>
                        </li> <!-- end .slide -->
                <?php } else { ?>
                        </div> <!-- end .slide -->
                <?php } ?>
                <?php $ids[] = $post->ID; $i++;
        }
    }

    wp_reset_query();
    echo \'</\'.$tag.\'>\';
    ?>
</div> <!-- end #featured -->
您会注意到,我做了一些简化,并替换了if语句,因此它们都使用{} 而不是一堆速记和标准。

这里有很多问题,最后一个问题是wp\\U reset\\u query,最大的一个问题是query_posts.

我强烈建议您不要使用query\\u帖子,而是使用WP_Queryget_posts.

罪魁祸首是你的循环:

        while ( have_posts() ) {
            global $post;
            // START custom first post
            if ( !$first_time ) {
                $post_id = 105; // This is the ID of the first post to be displayed on slider
                $post = get_post( $post_id );
                $first_time = 1;
            }else{
                the_post(); // END custom first post
            }
这里我们看到了post循环的开始,它检查是否有其他post,然后调用the_post 向前推进。

然而,当第一次发帖时,你不会打电话the_post, 因此循环无法前进。这不是循环应该如何工作,您应该always 呼叫the_post 不管

相反,我建议你在你的查询中指定不要包含105号帖子。(我也会避免硬编码post ID,它可能并不总是105)。

因此,首先,使用post__not_in 查询中的参数,例如:

$query_args[\'post__not_in\'] = array(105);

SO网友:T.Todua

我使用pre_get_posts. 示例:

add_action(\'pre_get_posts\',\'search_filterr\');function search_filterr($query) {
    if ( !is_admin() && $query->is_main_query() )   {
        if ( $query->is_search ) { //$query->is_category or etc...except is_page()
            $arrs[]=\'post\';
            $arrs[]=\'page\';
            $arrs[]=\'my_carss\';
            $query->set(\'post_type\',  $arrs );
        }
    }
}

结束

相关推荐

类别页面不使用Category y.php,而是重定向到主页

我当前正在修改一个主题。此主题没有类别。php我需要一个自定义类别页面。所以,我做了一个。它可以在我的本地服务器上运行,但当我将其上载到真正的服务器时,该页面无法运行。它总是重定向到frontpage。它有一个头版。php和其他页面包括Chive。php,索引。php,一些自定义页面模板,单页面模板,taxonomy-custom\\u cat。php等。我的意思是一切都很正常。其他一切都很好,但在主服务器类别上无论如何都不起作用。它是否违反了模板层次结构?顺便说一下,主服务器是wordpress mul