Php 查询每个类别的最新帖子
我知道有很多关于wp_查询的问题,但没有一个能完全回答我的问题 我正在绞尽脑汁研究如何从我的每个博客类别中查询1篇最新文章,但使用[]符号而不是数组:Php 查询每个类别的最新帖子,php,wordpress,Php,Wordpress,我知道有很多关于wp_查询的问题,但没有一个能完全回答我的问题 我正在绞尽脑汁研究如何从我的每个博客类别中查询1篇最新文章,但使用[]符号而不是数组: function get_latest_posts($query_args, $grid_name) { $categories = array(1593, 1594, 1595, 1596, 1597); if ($grid_name == 'get_latest_posts') { $query_args['posts_pe
function get_latest_posts($query_args, $grid_name) {
$categories = array(1593, 1594, 1595, 1596, 1597);
if ($grid_name == 'get_latest_posts') {
$query_args['posts_per_page'] = 1;
}
return $query_args;
}
add_filter('tg_wp_query_args', 'get_latest_posts', 10, 2);
// This returns one post but just one post, not one post per category
使用这种类型的查询参数而不是“传统”数组,是否可能有如下内容:
foreach($categories as $category) {
return $query_args;
}
// Something like this would be awesome ❥
提前谢谢你!对不起,我是个新手。试试这个:
有两种不同类型的数组,但都是数组<代码>[1593]
(数组)和['category'=>1593]
(关联数组)。你在说什么?这个['key']=>值一!很抱歉没有澄清。您使用foreach
循环关联数组,就像您发布的那样。如果要在输出之前生成每个类别的第一篇文章的列表,那么在使用return
之前,必须将所有内容放入自己的关联数组中。只有在foreach完成后,您才应该返回
,将返回
放在foreach
之后,循环遍历每个类别。您的返回值可以是您的关联数组。非常感谢您抽出时间。为了使用“网格”插件(添加了if-Grid-name语句),我对代码进行了一些修改,但它似乎不起作用,我有点困惑,我如何测试或获得一些关于我做错了什么的输出。