我是Wordpress的新手,尽管我读了很多文章,但我能够把归档年份放在我的主菜单上。
菜单包含页面,我需要添加存档。
我的当前菜单:
- Home (page)
- About (page)
- News (Category)
- Contact (page)
我需要
- Home (page)
- About (page)
- News (Category) - link to the newest year (2015)
- - 2015 (not category, just year when posts were added)
- - 2014
- - 2013
- Contact (page)
当我从2014年删除最后一个类别时,今年应该会从我的导航中消失。
当我在2016年1月添加新帖子时,2016年将自动出现在我的下拉列表中,并且“新闻”将不包含直接链接到2016
.
知道怎么做吗<顺便说一句,我真的读了很多文章,第一次看到WP(我是个程序员)。我可以用很多方法来做到这一点,但要寻找最好的方法,寻找最好的wordpress方法。
谢谢
最合适的回答,由SO网友:jas 整理而成
您好,请按照下面的代码在您想要的子菜单下动态添加年度帖子,我已经在中添加了下面的代码functions.php
add_filter( \'wp_nav_menu_objects\', \'ravs_add_menu_parent_class\' );
function ravs_add_menu_parent_class( $items ) {
foreach ( $items as $item ) {
//print_r($item);//print each menu item an get your parent menu item-id
// get your menu item ID use that ID in below code and you can remove this code after getting ID
}
GLOBAL $wpdb;
$years = $wpdb->get_results( "SELECT YEAR(post_date) AS year FROM wp_posts WHERE post_type = \'post\' AND post_status = \'publish\' GROUP BY year DESC" );
foreach($years as $year){
$link = array (
\'title\' => $year->year,
// \'title\' =>($year->year == date(\'Y\') ) ? \'News (Category)\' : $year->year, // this is how you want to print latest year as "News (Category)"
\'menu_item_parent\' => \'13\', // my menu id is 13 ie: ID of menu name test under which years links are displayed
\'ID\' => \'\',
\'db_id\' => \'\',
\'url\' => \'/\'.$year->year // to create url of menu item
);
$items[] = (object) $link;
}
return $items;
}
RESULT:
Other Case like you want : 使用方法如下:
\'title\' =>($year->year == date(\'Y\') ) ? \'News (Category)\' : $year->year,
谢谢