昨天@jas帮助我在导航中显示了“按年归档”。它工作正常,但还有第二个小问题。
@jas的解决方案每年从所有类别中筛选我的帖子,我只需要在一个类别中添加过滤器。
为我的导航添加年份的功能是:
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;
}
是否有人帮助我将其扩展到“新闻”类别(如果需要ID,猫的ID为“4”)。
谢谢大家
罗马