如何按日期对类别进行排序?

时间:2014-10-24 作者:breezy

这可能是一个快速简单的修复方法,但我希望能够按“ASC”或“DESC”排序

这不是我的标记,但这是我想要更改的循环,只是不确定如何更改。我读了wordpress文档的查询,但不知道放在哪里\'ORDER\' => \'ASC\'

是否需要重置WP查询?据我所知,如果有多个循环,只需重置查询。我说得对吗?

<?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?>
                    <article class="post">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <h3 class="post-title"><a href="<?php the_permalink() ?>" class="transicion"><?php the_title(); ?></a></h3>
                                <div class="row">
                                    <div class="col-lg-6">
                                        <?php echo \'<a href="\', get_permalink(), \'">\';
                                            if ( has_post_thumbnail() ) {
                                                the_post_thumbnail(\'blog_image\', array(\'class\' => "attachment-$size, imageborder img-responsive"));
                                            }
                                         echo \'</a>\'; 
                                        ?>
                                    </div>
                                    <div class="col-lg-6">
                                        <?php echo  excerpt(60); ?>
                                    </div>
                                </div>
                            </div>
                            <div class="panel-footer">
                                <div class="row">
                                    <div class="col-lg-10 col-md-9 col-sm-8">
                                        <i class="fa fa-clock-o"></i> <?php the_date(); ?> 
                                        <i class="fa fa-folder-open"></i> <?php the_category(\', \'); ?>.
                                    </div>
                                    <div class="col-lg-2 col-md-3 col-sm-4">
                                        <a href="<?php the_permalink() ?>" class="pull-right">Read more &raquo;</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </article> <!-- post -->
                <?php endwhile;?>
            <?php else : ?>
                <h2 class="post-title">No entries found</h2>
                <p>Not found anything that criteria. Try searching again or use the menu to navigate the site.</p>
                <?php get_search_form(); ?>
            <?php endif; ?>

1 个回复
SO网友:Bordoni

如果WP\\u查询的实例不是您正在处理的PHP文件上的变量,则它往往是“主”查询,要编辑WP\\u查询的此实例,您需要挂接到pre_get_posts 行动起来,改变你需要的东西。以下是一个示例:

<?php
add_action( \'pre_get_posts\', \'q166401_pre_get_posts\', 10, 1 );

/**
 * On this case I will filter only the Home and the main query
 * @param  WP_Query $query The query object
 */
function q166401_pre_query( $query ){
    // Stop this function when it\'s not Home or it\'s not the main query
    if ( ! $query->is_home() || ! $query->is_main_query() ) {
        return;
    }

    // Here you will set the filters you want
    $query->set( \'order\', \'ASC\' );
}
如果您有任何问题,请向codex page related to this hook.

结束

相关推荐

wordpress nested loop

我正在构建嵌套循环查询。我有一个元框,可以捕获邮件id(分类器和转发器)。到目前为止,我的代码while ( have_posts() ) : the_post(); ?> <div id=\"<?php echo $post->post_name; ?>\"> <h1><?php the_title(); ?></h1> <?php the_content