我如何根据某些标准(例如随机的,最近的,自定义字段等)对我的Wordpress帖子进行排序



我需要创建一个菜单来排序我的帖子最近,随机等。我在网上还没找到适合我的。我想要一个这样的菜单:

<div class="sort">
   <a class="recent" href="?sort=recent">Recent</a>
   <a class="random" href="?sort=random">Random</a>
</div>

然后可以通过点击菜单链接来改变页面的输出

我搜索了又搜索,找不到任何适合我的项目的东西;然后我明白了:switch语句。

创建简单菜单:

<div class="sort">
   <a class="recent" href="?sort=recent">Recent</a>
   <a class="random" href="?sort=random">Random</a>
</div>

并添加switch语句:

$order = 'ASC';
$orderby = '';
switch ($_GET['sort']) {
    case 'recent':
        $order = 'DESC';
        $orderby = '';
        break;
    case 'random':
        $orderby = 'rand';
        break;
}
$args = array(
    'post_type' => 'entry',
    'order' => $order,
    'posts_per_page' => 20, // limit of posts
    'orderby' => $orderby,
);

然后按正常方式循环。这也可以用于自定义字段!

'meta_key' => $metakey,    
'meta_query' => array(
        array(
            'value' => 'customfield'
        ),
    ),

最新更新