在返回的query_posts列表中添加特定帖子 - Wordpress

时间:2012-12-22 18:11:41

标签: php wordpress posts

我的Wordpress平台有一个精选的滑块,​​我希望在query_posts列表中首先显示一个特定的帖子。为了解释这一点,我在自定义分类top=billboard中的帖子的query_posts和我要显示的第一个帖子应该在代码中硬编码,假设它有ID 70。

以下是代码的相关部分:

<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
        $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");
        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()) : the_post();
        global $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 $i++; endwhile; endif; wp_reset_query(); ?>
<?php if ( $responsive ) { ?>
    </ul> <!-- end .slides -->
<?php } else { ?>
    </div> <!-- end #slides -->
<?php } ?>
</div> <!-- end #featured -->

我最接近的是添加此代码:

    <?php if (have_posts()) : while (have_posts()) : 
    global $post;
    if (!$first_time) // START custom first post
    {
        $post_id = 70; // 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
    ?>

部分有效。它首先显示了帖子ID 70这是好的,然而,它似乎扰乱了列表的其余部分。

所以,我希望返回的列表按此顺序排列:

  

帖子ID 70

     

从分类法发布top =广告牌

     

从分类法发布top =广告牌

     

从分类法发布top =广告牌

     

从分类法发布top =广告牌

     

...

如何正确实现这一目标?

1 个答案:

答案 0 :(得分:0)

我认为在rewind_posts();获取后应该使用ID=70,确保在循环中再次获取它时将其排除。也就是说,如果我理解得对。

<强>已更新

在这个问题上使用rewind_posts()wp_reset_query()是循环之后或之内的选项,但是可以使用get_post()在循环之外获取后70,因此可以获取它在开始循环之前。例如:

$PostID = 70;
$Post = get_post($PostID);
echo var_dump($Post); 

// Query
// Loop 
// Skip post 70
...  

您在循环中使用函数get_post(),这就是修改订单的原因。