所有分类
  • 所有分类

  • Warning: Attempt to read property "name" on null in /www/wwwroot/loudigu.com/wp-content/themes/modown/header.php on line 289

wordpress分类和文章增加模板选择功能

我们使用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');

原文链接:https://loudigu.com/wordpress%e5%88%86%e7%b1%bb%e5%92%8c%e6%96%87%e7%ab%a0%e5%a2%9e%e5%8a%a0%e6%a8%a1%e6%9d%bf%e9%80%89%e6%8b%a9%e5%8a%9f%e8%83%bd.html,转载请注明出处~~~
0
广告位招租
本站愿景:打造一个更专业的更完善的WP华语交流互助平台,助力WP华语生态蓬勃发展
显示验证码
没有账号?注册  忘记密码?