计划书之家主题|ripro子主题导航菜单显示每日文章更新数量

如果你的WordPress主题网站每天都更新大量的文章,而主页列表又不是类似于教程网那种小标题的列表,用户看上去就不知道每天到底更新了没有,更新了多少,这里我们给WordPress条件一个每日文章更新数量显示的功能,可以显示在导航栏或者其他地方,提示访客每日更新的内容。这个在网上教程集成到ripro主题,并在有更新时才提醒!

 操作步骤:

菜单 – 显示选项,勾出 “图像描述”,在项目的 “图像描述” 中添加需要显示更新数目的分类 id。

 

网上教程:

// 注册菜单
register_nav_menus( array(
	'primary' => __( '我的菜单' ),
) );
// 强化菜单 调用代码 (php) wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new description_walker ) ); (/php)
// 强化菜单 结构
class description_walker extends Walker_Nav_Menu
{
	function start_el(&$output, $item, $depth, $args)
	{
		global $wp_query;
		$indent = ( $depth ) ? str_repeat( "t", $depth ) : '';
 
		$class_names = $value = '';
 
		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
 
		$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
		$class_names = ' class="'. esc_attr( $class_names ) . '"';
 
		$output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';
 
		$attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
		$attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
		$attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
		$attributes .= ! empty( $item->url )        ? ' href="'%20%20%20. esc_attr( $item->url        ) .'"' : '';
 
		$prepend = '<span>';
		$append = '</span>';
		$description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';
 
		if($depth != 0)
		{
			$description = $append = $prepend = "";
		}
 
		$item_output = $args->before;
		$item_output .= '<a'. $attributes .'>';
		$item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
		// seabye++
		if ( $item->description<=0 ) { $item_output .= $description; }
			else { $item_output .= '<span class="day">+'.get_this_week_post_count_by_category($item->description).'</span>'; }
		$item_output .= $args->link_after;
		// seabye++ end
		// $item_output .= $description.$args->link_after;
		$item_output .= '</a>';
		$item_output .= $args->after;
 
		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
	}
}
// 强化菜单 代表时间
// 一日 today
// 一周 1 week ago
// 一年 1 year ago
// 千年 1000 year ago
function get_this_week_post_count_by_category($id) {
	$date_query = array(
		array(
			'after' => 'today'
			)
		);
	$tax_query = array(
		array(
			'taxonomy' => 'category',
				'field' => 'id',
				'terms' => $id
			)
		);
	$args = array(
		'post_type' => 'post',
		'post_status' => 'publish',
		'tax_query' => $tax_query,
		'date_query' => $date_query,
		'no_found_rows' => true,
		'suppress_filters' => true,
		'fields' => 'ids',
		'posts_per_page' => -1
		);
	$query = new WP_Query( $args );
	return $query->post_count;
}

如何调用代码如下,放在想要显示的页面位置即可。

<?php wp_nav_menu( array( 'theme_location' => 'primary', 'walker' => new description_walker ) ); ?>

 

如果要条件在菜单导航里面,红色加粗为需要添加的内容。

菜单 – 显示选项,勾出 “图像描述”,在项目的 “图像描述” 中添加需要显示更新数目的分类 id。

 

版权声明:原创作品,未经允许不得转载,否则将追究法律责任。
本站资源有的自互联网收集整理,如果侵犯了您的合法权益,请联系本站我们会及时删除。
本站资源仅供研究、学习交流之用,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担。
本文链接:子佩工作室https://zptheme.com/3073.html
许可协议:《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权

本站分为普通会员,SVIP会员,永久会员。
SVIP会员新用户注册即送180天,到期后可邀请好友2位/180天进行无限续期。
永久会员支持微信支付在线开通。

积分是本站通用虚拟货币,可用于文章资源购买。
每天签到,评论或点赞文章,或者投稿都可免费获得积分
新用户注册免费赠送2积分 邀请用户注册2位/2积分

如果您已经成功付款但是网站没有弹出成功提示,请联系售后提供付款信息为您处理

本站资源属于虚拟商品,具有可复制性,可传播性,一旦授予,不接受任何形式的退款、换货要求。请您在购买获取之前确认好 是您所需要的资源

需要效果图的,可以联系售后(右侧核保售后,扫码添加或者点击 加入我们-联系客服)。或者微信添加企业微信:子佩工作室

计划书请提供费率条款(现价分红),100起。WP主题一般单个问题30起。主题二开500起,开发新主题3000起。低于1000须全款,其余先支付全款的60%,完成后支付余下的。

资源是指寿险计划书(定制除外),WP主题或插件不包含在永久会员之内!

发表回复

评论(1)