Worpress对帖子进行排序

时间:2018-04-17 03:19:13

标签: wordpress

在wordpress标签页面(tag.pgp)。我有这样的左菜单。

 <div class="well">
      <div>
          <a href="#"> ALphabatically</a> <br/>  
      </div>

       <div >
          <a href="#"> By category</a> <br/>  
      </div>


    </div> 

我在The循环中右侧有所有帖子。

<?php
  while(have_posts()) {
    the_post(); ?>
    <div >
      <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>

      <div>
        <p>Posted by <?php the_author_posts_link(); ?> on <?php the_time('n.j.y'); ?> in <?php echo get_the_category_list(', '); ?></p>
      </div>

      <div>
        <?php the_excerpt(); ?>
        <p><a  href="<?php the_permalink(); ?>">Continue reading &raquo;</a></p>
      </div>

    </div>
  <?php }

  echo paginate_links();
?>

如何通过点击左侧的链接按字母顺序和按类别对帖子进行排序。

2 个答案:

答案 0 :(得分:0)

EMR 5.13

答案 1 :(得分:0)

使用WordPress提供的pre_get_posts操作钩子。

然后,在您的functions.php中添加以下代码:

function custom_pre_get_posts($query) {
    // validate
    if(!is_admin() && $query->is_main_query()) {

        if(is_archive()) {
            $query->set('orderby', 'title'); // order posts by title
            $query->set('order', 'ASC'); // and in ascending order
        }
    }
}
add_action('pre_get_posts', 'custom_pre_get_posts');

有关更多信息,请访问https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

相关问题