自定义查询:如何先获取粘帖?

时间:2011-12-12 作者:Luca Reghellin

我注意到,如果我编写一个自定义WP\\U查询,粘性帖子不会首先显示在列表中。所以我一直在想如何做到这一点。我已经完成了3个后续查询,但我想一定有一个更优雅的解决方案。

我认为至少有两个问题:

首先构建一个简单的函数,以便重用以输出html

第二步仅查询stiky帖子,并输出html(根据预设的最大项目数)

第三,根据最大项目数,是否还有新项目的空间,进行查询,排除粘性帖子,然后再次输出html。

所以,一切都是这样开始的(然后继续):

            $max_items = 5;
        $sticky_ids = get_option(\'sticky_posts\');

        if(count($sticky_ids) != 0){
            //query degli sticky
            $sticky_soon_query = new WP_Query();
            $sticky_soon_query->query(array(
                \'category_name\' => \'coming-soon\',
                \'posts_per_page\' => $max_items,
                \'post__in\' => $sticky_ids
            ));
        } 
还有更优雅的解决方案吗?

3 个回复
SO网友:Luca Reghellin

进一步升级:只构建了一个类来放置函数。php或插件。我这样做主要是因为我希望几乎总是有粘性帖子,可能每页都有更多的循环。所以有很多重复。因此,这里有一个基本类,当然可以通过某种方式进行扩展/完善,但对于我的需求来说,它只起作用:

class Posts_With_Sticky{

public $max_items_amount;
public $normal_items_amount;
public $category;
public $sticky_ids;
public $sticky_query;
public $normal_query;


function __construct($category = null, $max_items_amount = 5){

    $this->max_items_amount = $max_items_amount;
    $this->sticky_ids = get_option(\'sticky_posts\');
    $this->category = $category;

    $this->get_query();
}

public function get_query(){

    //sticky query
    if(count($this->sticky_ids) != 0){
        $this->sticky_query = new WP_Query();
        $this->sticky_query->query(array(
            \'category_name\' => $this->category,
            \'posts_per_page\' => $this->max_items_amount,
            \'post__in\' => $this->sticky_ids
        ));
    }

    //check amount of stikies and room for normals
    if(isset($this->sticky_query)){
        $stikies = count($this->sticky_query->posts);
        if($stikies == $this->max_items_amount) return;
        else $this->normal_items_amount = $this->max_items_amount - $stikies;
    }

    //normal posts query
    $this->normal_query = new WP_Query();
    $this->normal_query->query(array(
        \'category_name\' => $this->category,
        \'posts_per_page\' => $this->normal_items_amount,
        \'post__not_in\' => $this->sticky_ids
    ));

}//end get_query

public function render_posts($sticky_loop_function, 
                                      $normal_loop_function = "", 
                                      $before_markup = "", 
                                      $after_markup = ""){

    if(!$sticky_loop_function) return;

    //shortcut
    $sticky_setted = isset($this->sticky_query);
    $normal_setted = isset($this->normal_query);    

    if(!$sticky_setted && !$normal_setted) return;

    $sticky_loop = $sticky_loop_function;
    $normal_loop = ($normal_loop_function !== "") ? $normal_loop_function : $sticky_loop_function;

    echo $before_markup;

    if($sticky_setted) $sticky_loop($this->sticky_query);
    if($normal_setted) $normal_loop($this->normal_query);

    echo $after_markup;
}//end render_posts

}//end Posts_With_Sticky
然后,在html页面中,我将为每个部分编写不同的循环函数,然后将其传递给render\\u posts()。

类似这样:

        <?php

    //html output function
    function write_soon_items($query){

        while($query->have_posts()){
            $query->the_post();
            $date = get_the_date(\'d.m.Y\');
            $title = get_the_title();
            $excerpt = get_the_excerpt();
            $link = get_permalink();

            echo <<< ITEM_BLOCK
            <article>
                <h4>
                    $date - $title
                </h4>
                <p>
                    <a href="$link" title="$title">$excerpt</a>
                </p>
                <div class="float-reset"></div>
            </article>
ITEM_BLOCK;
        }
    }//end write_sub_items


    $soon_posts = new Posts_With_Sticky(\'coming-soon\');

            $soon_posts->render_posts(\'write_soon_items\')
    ?>
因此,每当我需要一个循环来纪念某个类别的粘性帖子时,我只需使用\\u sticky实例创建另一个posts\\u,然后调用其render\\u posts()方法来传递自定义输出循环函数。

请注意,可以传递render\\u posts()一个用于粘滞的函数、一个用于法线的函数(如果不传递,将使用第一个)、一个“before\\u markup”和一个“after\\u markup”。

希望它能帮助别人。

再见

SO网友:Mecha

我在计算与粘滞物相关的正常帖子数量时发现了一个逻辑错误。这是对我有用的代码@Stratboy,非常感谢您的分享。干杯

<?php

//html output function
function write_sub_items($query){

    while($query->have_posts()){
        $query->the_post();
        $date = get_the_date(\'d.m.Y\');
        $title = get_the_title();
        $excerpt = get_the_excerpt();

        echo <<< ITEM_BLOCK
        <li>
            <h4>
                $date - $title
            </h4>
            <p>
                $excerpt
            </p>
        </li>
ITEM_BLOCK;
    }
}//end write_sub_items


    $max_items_amount = 5;
    $normal_items_amount = $max_items_amount;

    $sticky_ids = get_option(\'sticky_posts\');

    //sticky query
    if(count($sticky_ids) != 0){
        $sticky_soon_query = new WP_Query();
        $sticky_soon_query->query(array(
            \'category_name\' => \'coming-soon\',
            \'posts_per_page\' => 5,
            \'post__in\' => $sticky_ids
        ));
    }

    //check amount of stikies and room for normals
    if(isset($sticky_soon_query)){
        $stikies = count($sticky_soon_query->posts);
        if($stikies <= $max_items_amount) {
            $normal_items_amount = $max_items_amount - $stikies;
        }
    }

    //normal posts query
    if($normal_items_amount > 0){
        $normal_soon_query = new WP_Query();
        $normal_soon_query->query(array(
            \'category_name\' => \'coming-soon\',
            \'posts_per_page\' => $normal_items_amount,
            \'post__not_in\' => $sticky_ids
        ));
    }

    //shortcut
    $stiky_setted = isset($sticky_soon_query);
    $normal_setted = isset($normal_soon_query);

?>

<?php if($stiky_setted || $normal_setted) : ?>
<ul>
    <?php if($stiky_setted){ write_sub_items($sticky_soon_query); } ?>
    <?php if($normal_setted){ write_sub_items($normal_soon_query); } ?>
</ul>
<?php endif; ?>

SO网友:Luca Reghellin

好的,没有答案。无论如何,我只是想分享一下,目前我的(工作)解决方案是这样的:

        <?php

    //html output function
    function write_sub_items($query){

        while($query->have_posts()){
            $query->the_post();
            $date = get_the_date(\'d.m.Y\');
            $title = get_the_title();
            $excerpt = get_the_excerpt();

            echo <<< ITEM_BLOCK
            <li>
                <h4>
                    $date - $title
                </h4>
                <p>
                    $excerpt
                </p>
            </li>
ITEM_BLOCK;
        }
    }//end write_sub_items


        $max_items_amount = 5;
        $normal_items_amount = $max_items_amount;

        $sticky_ids = get_option(\'sticky_posts\');

        //sticky query
        if(count($sticky_ids) != 0){
            $sticky_soon_query = new WP_Query();
            $sticky_soon_query->query(array(
                \'category_name\' => \'coming-soon\',
                \'posts_per_page\' => 5,
                \'post__in\' => $sticky_ids
            ));
        }

        //check amount of stikies and room for normals
        if(isset($sticky_soon_query)){
            $stikies = count($sticky_soon_query->posts);
            if($stikies == $max_items_amount) return;
            else $normal_items_amount = $max_items_amount - $stikies;
        }

        //normal posts query
        $normal_soon_query = new WP_Query();
        $normal_soon_query->query(array(
            \'category_name\' => \'coming-soon\',
            \'posts_per_page\' => $normal_items_amount,
            \'post__not_in\' => $sticky_ids
        ));

        //shortcut
        $stiky_setted = isset($sticky_soon_query);
        $normal_setted = isset($normal_soon_query);

    ?>

    <?php if($stiky_setted || $normal_setted) : ?>
    <ul>
        <?php if($stiky_setted){ write_sub_items($sticky_soon_query); } ?>
        <?php if($normal_setted){ write_sub_items($normal_soon_query); } ?>
    </ul>
    <?php endif; ?>
vars名称中的“soon”一词并不重要。在我的项目中,我正在为《即将到来》新闻编写代码,仅此而已。

结束

相关推荐