Products
GG网络技术分享 2025-03-18 16:12 0
在前面的章节中,我们介绍了wordpress网站模板开发中,使用wp_list_pages()和wp_list_categories()这2个函数来创建基于page页面的导航菜单和基于分类目录的导航菜单。但是,这2种方式创建出来的菜单不够灵活,基于页面的导航菜单就添加不了分类目录,基于分类目录的导航菜单也添加不了page页面作为菜单。那么,wordpress有没有提供灵活一点的创建导航菜单的方法,能包含page页面、分类目录,甚至其它的选项。答案是肯定的。通过wordpress提供的方法,我们可以为wordpress网站模板创建自定义导航菜单。下面,就随我一起来看看吧。
wordpress网站后台默认情况下,没有提供菜单选项,需要我们在wordpress主题模板中来添加这个功能。这个,我们可以进入到wordpress后台的外观去看看,如下图:
所以,我们要先在wordpress主题的functions.php文件中注册菜单,代码如下:
添加完上面的代码,我们再到wordpress网站后台的外观中看一下,这时,我们会发现多了一个“菜单”,这就是我们需要的自定义菜单。如下图:
给wordpress网站后台添加了菜单功能后,我们就可以在wordpress后台的菜单中创建前台网页的自定义导航菜单了。点击“外观”中的“菜单”,右侧内容栏进入到菜单界面,如下图:
点击上图中的“创建新菜单”,进入到创建界面,如下图:
填写菜单名,点击“创建菜单”,进入到菜单结构界面,如下图:
在这个菜单结构界面,我们可以选择左侧的“添加菜单项”中的页面、分类目录、文章、自定义链接这几个选项中的子项,然后点击“添加到菜单”,就添加到右侧的菜单结构中,想在wordpress网站前台导航菜单中显示什么,就选择什么。如下图:
然后,勾选“显示位置”,再点击保存菜单按钮,就可以了。
wordpress后台创建好了菜单后,我们要想在wordpress网站前台显示,还需要我们在wordpress网站模板中调用它。在给functions.php文件添加菜单功能时,我们设置了2个菜单位置:头部菜单和底部菜单。这里,我们以头部菜单为例,在wordpress网站模板的头部模板header.php中添加如下代码:
这样,我们就可以在wordpress网站的前台头部看到我们添加的导航菜单,如下图:
有点难看,是吧,我们可以给这个菜单添加CSS样式,来让它好看一点,这里就不演示了。上面的代码中,我们用到了wordpress的一个函数——wp_nav_menu(),这个函数的功能,就是把后台创建好的菜单在前台打印出来。这个函数的参数跟wp_list_pages()和wp_list_categories()这2个函数的参数类似,可以是字符串类型,也可以是数组类型。
wp_nav_menu()函数的参数如下:
可见,wp_nav_menu()函数的参数还是挺多的,但实际使用中,不需要全部设置,往往我们只需要使用其它的几个常规参数就可以了,如上例中,我们就只用了3个参数,就打印出了wordpress网站的头部导航菜单。
通过以上这几步,我们就为wordpress网站模板添加好了自定义导航菜单的功能,看起来有点复杂,是吧。的确,跟wp_list_pages()和wp_list_categories()这2导航菜单相比,是要复杂一点。但是,多做几次后,你会发现,这种自定义导航菜单使用起来更加灵活多变。不管怎样,这3种添加导航菜单的方式我们都要掌握,可以在不同的需求中使用不同的方式。
我们使用wordpress网站中,在页面里面可以选择模板,如果要在分类和文章里面也增加这个功能可以用下面方法来实现。
在主题文件里面增加template.php文件,里面加入下面代码:
<?phpfunctioncat_temp_menus(){add_menu_page('模板选择器','模板选择器','manage_options',basename(__FILE__),'cat_temp_options_page');}functioncat_temp_meta(){
global$wpdb,$post_ID;
$templates=cat_temp_get_page_templates();
$current=cat_temp_post_template($post_ID);
$out='<selectname="cat_temp_template"style="width:100%">';
$out.='<optionvalue="none"';
if($post_ID==0||!$current)$out.='selected="selected"';
$out.='>请选择</option>';
$out.='<optionvalue="/single.php"';
if($current=="/single.php")$out.='selected="selected"';
$out.='>默认模板</option>';
foreach($templatesas$template=>$file){
$out.='<optionvalue="'.$file.'"';
if($current==$file)$out.='selected="selected"';
$out.='>'.$template.'</option>';
}
$out.="</select>";
$out.="<p>选择文章模板</p>";
echo$out;}functioncat_temp_post_submit($post_ID){
global$wpdb;
if($_POST['cat_temp_template']){
$templates=(get_option("cat_temp_post"));
if($_POST['cat_temp_template']!='none'){
$templates[$post_ID]=$_POST['cat_temp_template'];
}else{
if($templates[$post_ID]){
unset($templates[$post_ID]);
}
}
update_option("cat_temp_post",($templates));
}}functioncat_temp_post_template($ID){
$templates=(get_option("cat_temp_post"));
return$templates[$ID];}functioncat_temp($template){
global$wp_query;
$post_obj=$wp_query->get_queried_object();
if(is_single()){
$data=cat_temp_get_data();
$categories=get_the_category($post_obj->ID);
}elseif(is_category()){
$data=cat_temp_get_data(true);
$categories[0]=&get_category($post_obj->cat_ID);
}
$temp_data;
foreach((array)$categoriesas$category){
if($data[$category->term_id]['template']!='0'){
$temp_data[$data[$category->term_id]['template']]=$data[$category->term_id]['priority']+($category->term_id/80000);
}
}
foreach((array)$dataas$key=>$cat){
if($cat['all']=="all"&&$cat['template']!="0"){
$id=(is_single())?(int)$cat['id']:$key;
$descendants=get_term_children($id,'category');
if($descendants&&in_category($descendants)){
$temp_data[$cat['template']]=$cat['priority']+($cat['id']/80000);
}
}
}
if(is_array($temp_data)){
asort($temp_data);
$template=array_shift(array_keys($temp_data));
}
if(is_single()){
$overRule=cat_temp_post_template($post_obj->ID);
if($overRule)$template=$overRule;
}
if(!empty($template)){
if(file_exists(TEMPLATEPATH.$template)){
include(TEMPLATEPATH.$template);
exit;
}
}}functioncat_temp_is_cat($cat,$_post=null){
if(in_category($cat,$_post)){
returntrue;
}else{
$descendants=get_term_children((int)$cat,'category');
if($descendants&&in_category($descendants,$_post))returntrue;
}
returnfalse;}functioncat_temp_get_page_templates($str="TemplateName"){
$themes=get_themes();
$theme=get_current_theme();
$templates=$themes[$theme]['TemplateFiles'];
$page_templates=array();
if(is_array($templates)){
foreach((array)$templatesas$template){
if(!file_exists($template))$template=WP_CONTENT_DIR.$template;
$template_data=implode('',file($template));
$name='';
if(preg_match('|'.$str.':(.*)$|mi',$template_data,$name))
$name=$name[1];
if(!empty($name)){
$page_templates[trim($name)]=str_replace($themes[$theme]['TemplateDir'],"",$template);
}
}
}
return$page_templates;}functioncat_temp_cats($item,$current,$archive=false){
if($archive){
$templates=cat_temp_get_page_templates('ArchiveTemplate');
$default='/archive.php';
}else{
$templates=cat_temp_get_page_templates();
$default='/single.php';
}
$out='<selecttitle="Template"class="ct_template"name="data';
if($archive)$out.='[archive]';
$out.='['.$item.'][template]">';
$out.='<optionvalue="0"';
if($current=="0")$out.='selected="selected"';
$out.='>未指定</option>';
$out.='<optionvalue="'.$default.'"';
if($current==$default)$out.='selected="selected"';
$out.='>默认模板</option>';
foreach($templatesas$template=>$file){
$out.='<optionvalue="'.$file.'"';
if($current==$file)$out.='selected="selected"';
$out.='>'.$template.'</option>';
}
$out.="</select>";
return$out;}functioncat_temp_categories($child=0){
$data=array(
"hide_empty"=>false,
"child_of"=>$child,
"pad_count"=>false,
);
$categories=get_categories($data);
$list=array();
foreach((array)$categoriesas$cat){
if($cat->parent==$child){
$list[]=array(
"name"=>$cat->name,
"id"=>$cat->cat_ID,
"count"=>cat_temp_getALLposts($cat->cat_ID),
"acount"=>$cat->category_count,
"child"=>cat_temp_categories($cat->cat_ID),
);
}
}
return$list;}functioncat_temp_li_fun($data){
$out="<ulclass=\\"cat-temp\\">";
foreach((array)$dataas$root){
$out.="<li>".$root['name']."(".$root['count'].")</li>";
if(count($root['child'])>0){
$out.=cat_temp_li_fun($root['child']);
}
}
$out.="</ul>";
return$out;}functioncat_temp_priority($item,$current,$archive){
$pri=array("最低","低","中等","高","最高");
$out='<selectclass="ct_priority"title="TemplatePriority"name="data';
if($archive)$out.='[archive]';
$out.='['.$item.'][priority]">';
$t=0;
for($i=10;$i>=1;$i=$i-2){
$out.='<optionvalue="'.$i.'"';
if(intval($current)==$i)$out.='selected="selected"';
$out.='>'.$pri[$t].'</option>';
$t++;
}
$out.="</select>";
return$out;}functioncat_temp_getALLposts($ID){
$td=array(
'numberposts'=>-1,
'category'=>$ID,
);
returncount(get_posts($td));}functioncat_temp_get_data($archive=false,$id=false){
$t=(!$archive)?(get_option('cat_temp_data')):(get_option('cat_arch_data'));
return(!$id)?$t:$t[$id];}functioncat_temp_update($data){
$archive=$data['archive'];
unset($data['archive']);
update_option('cat_temp_data',($data));
update_option('cat_arch_data',($archive));}functioncat_temp_delete(){
delete_option('cat_temp_data');
delete_option('cat_temp_post');
delete_option('cat_arch_data');}functioncat_temp_sub_cats($id,$data,$archive=false){
$out.='<inputtype="checkbox"';
if($data=="all")$out.='checked="checked"';
$out.='name="data';
if($archive)$out.="[archive]";
$out.='['.$id.'][all]"value="all"title="Applytosub-categories"/><small>用于子分类</small>';
return$out;}functioncat_temp_templates($id,$archive){
if($archive){
$title="分类";
$class="class=\\"noborder\\"";
$class2="noborder";
}else{
$title="文章";
}
$data=cat_temp_get_data($archive,$id);
$out.="<tdclass=\\"r$class2\\">$title:</td>";
$out.="<td$class><div>".cat_temp_cats($id,$data['template'],$archive);
$out.=cat_temp_sub_cats($id,$data['all'],$archive).'</div></td>';
$out.="<td$class><div>".cat_temp_priority($id,$data['priority'],$archive)."</div></td>";
return$out;}functioncat_temp_td_fun($data,$padding=5){
$out="";
foreach((array)$dataas$root){
$out.='<tr>';
$out.='<tdclass="c"rowspan="2">'.$root['id'];
$out.='<inputtype="hidden"name="data['.$root['id'].'][id]"value="'.$root['id'].'"/>';
$out.='</td>';
$out.='<tdclass="wide"style="padding-left:'.$padding.'px;"rowspan="2">'.$root['name'].'</td>';
$out.=cat_temp_templates($root['id'],true);
$out.='<tdrowspan="2">'.$root['acount'].'('.$root['count'].')</td>';
$out.="</tr><tr>";
$out.=cat_temp_templates($root['id'],false);
$out.='</tr>';
if(count($root['child'])>0){
$out.=cat_temp_td_fun($root['child'],$padding+10);
}
}
return$out;}functioncat_temp_options_page(){
$_GET['lang']='all';
;echo'
<divclass="wrapcat-template">
<divclass="icon32"id="icon-themes"><br/>
</div>
<h2>模板选择器</h2>
';
if($_POST['update_theme']){
cat_temp_update($_POST['data']);
echo'<divid="message"class="updated"><p>成功保存.</p></div>';
}
;echo'<p>请选择分类和页面模板</p>
<formmethod="post"action="';echo$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];;echo'">
';
if(function_exists('settings_fields')){
settings_fields('cat-temp-options');
}else{
wp_nonce_field('update-options');
;echo'<inputtype="hidden"name="action"value="update"/>
';
}
;echo'<table>
<tr>
<tdstyle="vertical-align:top"width="70%"><tablewidth="100%"class="widefat"id="cat_temps">
<thead>
<tr>
<thwidth="2%"class="c">ID</th>
<th>分类</th>
<thcolspan="2">模板</th>
<thwidth="4%">等级</th>
<th>文章数</th>
</tr>
</thead>
<tbody>
';echocat_temp_td_fun(cat_temp_categories());;echo'</tbody>
</table></td>
<tdstyle="vertical-align:top"><tablewidth="100%"class="widefat">
<thead>
<tr>
<th>说明</th>
</tr>
<tr>
<td>按照以下说明制作模板:</td>
</tr>
<tr>
<td>
分类模板:
<prestyle="direction:ltr">
<?php
/**
ArchiveTemplate:分类模板名
**/
?>
</pre>
</td>
</tr>
<tr>
<td>
文章模板:
<prestyle="direction:ltr">
<?php
/**
TemplateName:文章模板名
**/
?>
</pre>
</td>
</tr>
</thead>
</table></td>
</tr>
</table>
<pclass="submit">
<inputtype="submit"class="button-primaryautowidth"name="update_theme"value="保存"/>
</p>
</form>
</div>
';}add_action('admin_menu','cat_temp_menus');add_action('save_post','cat_temp_post_submit');add_filter('template_redirect','cat_temp');?>
然后在主题函数functions.php里面引入该文件,就可以实现了。
include(TEMPLATEPATH.'/template.php');
Demand feedback