我如何按特定标准(即随机、最近的帖子、自定义字段等)对我的 Wordpress 帖子进行排序?
How can I sort my Wordpress posts by certain criteria (i.e. random, recent posts, custom fields, ect.)?
我需要创建一个菜单来按最新、随机等方式对我的帖子进行排序。而且我还没有在网上找到任何适合我的东西。我想要这样的菜单:
<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'
),
),
我需要创建一个菜单来按最新、随机等方式对我的帖子进行排序。而且我还没有在网上找到任何适合我的东西。我想要这样的菜单:
<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'
),
),