【发布时间】:2020-05-15 06:17:15
【问题描述】:
我正在尝试将最新帖子添加到我的页脚菜单中,因此我使用了wp_get_nav_menu_items 过滤器。
我将跳过过滤器本身,就在函数结束时我得到一个帖子数组(2)。我已经向它们添加了所有附加数据(如 menu_parent_id 等)。问题是最终菜单仅包含最后一篇文章。即使我设置$items[0] = $items[1]; 以便两个项目相同。它不依赖于特定的菜单或帖子数量 - 总是仅最后一项。我将以可快速重现的格式保留代码:只是一个过滤器和 JSON 编码的项目:
add_filter('wp_get_nav_menu_items', function ($items, $menu, $args) {
return json_decode('[{
"ID": 2576,
"post_author": "3",
"post_date": "2020-05-11 23:46:15",
"post_date_gmt": "2020-05-11 23:46:15",
"post_content": "",
"post_title": "Testr",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "open",
"ping_status": "open",
"post_password": "",
"post_name": "testr",
"to_ping": "",
"pinged": "",
"post_modified": "2020-05-11 23:46:15",
"post_modified_gmt": "2020-05-11 23:46:15",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http:\/\/mysite.local\/?p=2576",
"menu_order": 0,
"post_type": "post",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 2576,
"object_id": "",
"menu_item_parent": "0",
"url": "http:\/\/mysite.local\/?p=2576",
"title": "Testr",
"target": "",
"classes": [],
"type": "",
"object": "",
"attr_title": "",
"description": "",
"xfn": "",
"status": ""
}, {
"ID": 3439,
"post_author": "3",
"post_date": "2020-05-11 23:28:49",
"post_date_gmt": "2020-05-11 23:28:49",
"post_content": "",
"post_title": "Test",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "open",
"ping_status": "open",
"post_password": "",
"post_name": "test-2",
"to_ping": "",
"pinged": "",
"post_modified": "2020-05-11 23:28:49",
"post_modified_gmt": "2020-05-11 23:28:49",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http:\/\/mysite.local\/?p=3439",
"menu_order": 0,
"post_type": "post",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 3439,
"object_id": "",
"menu_item_parent": "0",
"url": "http:\/\/mysite.local\/?p=3439",
"title": "Test",
"target": "",
"classes": [],
"type": "",
"object": "",
"attr_title": "",
"description": "",
"xfn": "",
"status": ""
}]');
});
以前有没有人遇到过这个问题,请帮忙,这让我发疯了:)
【问题讨论】: