一篇文章解决所有——WordPress 主题模板开发 常用语句/函数 及问题解决

常用调用函数

页面信息

代码功能
<? bloginfo('name');?>网站名称
<? bloginfo('description');?>网站描述
<? get_header();?>引入header.php文件
<? get_header('xxx');?>引入header-xxx.php文件
<? get_footer();?>引入footer.php文件
<? get_footer('xxx');?>引入footer-xxx.php文件

文章调用

<?php 
    $arclist = get_posts("numberposts=6&category=1"); 
    //numberposts 调用数量 category 分类ID
    //var_dump($arclist);
    foreach ($arclist as $post):
?>

//HTML循环代码

<?php
    endforeach;
?>

get_posts() 参数列表

属性名功能
numberposts提取的文章数量整数
offset以第几篇文章为起始位置整数
category指定栏目整数
orderby排序规则author’ ‘category’ ‘content’ ‘date’ ‘ID’ ‘menu_order’ ‘mime_type’ ‘modified’ ‘name’ ‘parent’ ‘password’ ‘rand’ ‘status’ ‘title’‘type’
order升序、降序‘ASC’ 升序 (低到高) ‘DESC’ 降序 (高到底)
include要显示文章的ID(多个ID以,分割)整数
exclude要排除文章的ID(多个ID以,分割)整数
meta_key自定义字段名称
meta_value自定义字段的值
post_type文章类型post(日志)默认 ,page(页面),attachment(附件),any (所有)
post_mime_type文章的 mime 类型
post_parent要显示文章的父级 ID整数
post_status文章状态publish

循环中的直接输出函数

在foreach循环输出文章中 可以使用 参数名->字段名 输出函数 如$post->title 也可以使用系统自带的函数如下:

代码功能
the_title()获取当前文章的标题
the_permalink()获取当前文章的链接
the_content()获取当前文章的文章内容
the_excerpt()显示文章的一部分内容
the_category()当前文章所属的分类
the_author()当前文章的作者
the_time()获取当前文章的发布时间 the_time('Y-m-d H:m:s')




首页菜单栏调用

调用所有

<?php 
	 $cat_list = get_categories("exclude=$exclude_cat");
	 //var_dump($cat_list);
	 foreach ($cat_list as $cat)?>
 
 <li><a href="<?=get_category_link($cat->term_id);?>"><?=$cat->name;?></a></li>
 
<?php
	endforeach;
?>

只调用父级栏目

在functions.php 中添加如下代码

function get_all_parent_cats($cats){
	$new_ararry = array();
	foreach ($cats as $cat){
		if($cat->parent == null ){
			array_push($new_ararry,$cat);
		}
	}
	return $new_ararry;
}

页面代码

<?php 
	 $cat_list = get_categories("exclude=$exclude_cat");
	 //var_dump($cat_list);
	 $cat_list = get_all_parent_cats($cat_list);
	 foreach ($cat_list as $cat)?>
 
 <li><a href="<?=get_category_link($cat->term_id);?>"><?=$cat->name;?></a></li>
 
<?php
	endforeach;
?>





列表页常用代码及调用文章

<?php
    $cats = get_categories(); //获取所有分类
    $now_cat_name = single_cat_title('', false); //获取当前分类名
    $now_cat_id = get_cat_ID($now_cat_name);  //根据分类名查找ID
    $now_cat_array = get_term($now_cat_id,'category'); //根据ID查询分类所有参数(返回数组)
    $new_cat_url = get_category_link($now_cat_id);  //获取分类链接
    $now_cat_parent_id = $now_cat_array->parent;    //获取父级分类ID
?>

html头部

    <title><?=$now_cat_name?> - <?bloginfo('name');?></title>
    <meta name="keywords" content="<?=$now_cat_name?>,<?bloginfo('name');?>" />
    <meta name="description" content="<?=$now_cat_name?>-<?bloginfo('description');?> " />



栏目路径
在这里插入图片描述

 <a href="<?bloginfo('url');?>">首页</a> 
	 <?php
	 if($now_cat_parent_id != null){
	 ?>
 &gt; <a href="<?=get_category_link($now_cat_parent_id)?>"><?=get_cat_name($now_cat_parent_id);?></a>
	 <?php
	 }
	 ?>
 &gt; <a href="<?=$new_cat_url?>"><?=$now_cat_name?></a>

文章循环

<?php if (have_posts()) : ?>

	<?php while (have_posts()) : the_post(); ?>

	//html代码

    <?php endwhile; ?>

<?php endif; ?>

获取当前分类的父类和其所有子类
wordpress 获取当前分类的父类和其所有子类
functions.php 里添加如下代码

//获取父类ID的所有子类
//参数 $cats所有分类(array) $parentid父类ID(int)
function get_child_cats($cats,$parentid){
	$new_ararry = array();
	foreach ($cats as $cat){
		if($cat->parent == $parentid){
			array_push($new_ararry,$cat);
		}
	}
	return $new_ararry;
}

//获取当前分类的父类和所有子类
//$cat当前分类(数组) $cats所有分类
//$cat = get_term($now_cat_id,'category');
//$cats = get_categories();
function get_cats_parent_child($cat,$cats){
    $new_ararry = array();
    if($cat->parent != 0){
        array_push($new_ararry,get_term($cat->parent,'category'));
        $child_cats = get_child_cats($cats,$cat->parent);
        if(count($child_cats) != 0){
            foreach ($child_cats as $cat){
                array_push($new_ararry,$cat);
            }
        }
    }else{
        array_push($new_ararry,$cat);
        $child_cats = get_child_cats($cats,$cat->term_id);
        foreach ($child_cats as $cat){
                array_push($new_ararry,$cat);
        }
    }
    return $new_ararry;
}

页面代码

<?php
	$now_cat_name = single_cat_title('', false); //获取当前分类名
    $now_cat_id = get_cat_ID($now_cat_name);  //根据分类名查找ID
	$cat = get_term($now_cat_id,'category');
	$cats = get_categories();
	$cats_list_data = get_cats_parent_child($cat,$cats);
	foreach ($cats_list_data as $cat):
?>

<li class="<?if($now_cat_id == $cat->term_id){echo 'on';}?>">
	<a href="<?=get_category_link($cat->term_id);?>">
		<?=$cat->name;?> 
	</a>
</li>

<?php
	endforeach;
?>




文章页常用代码

代码功能
the_title()获取当前文章的标题
the_permalink()获取当前文章的链接
the_content()获取当前文章的文章内容
the_excerpt()显示文章的一部分内容
the_category()当前文章所属的分类
the_author()获取当前文章的发布时间the_time(‘Y-m-d’)
<?php
    $now_cat = get_the_category()[0]; //获取文章分类
    $now_cat_name = $now_cat->name;   //获取分类名
    $now_cat_id = $now_cat->cat_ID;   //获取分类ID
    $now_cat_array = get_term($now_cat_id,'category');   //查询分类信息
    $new_cat_url = get_category_link($now_cat_id);	     //获取分类链接
    $now_cat_parent_id = $now_cat->parent;               //获取父类ID
?>

html头部

 <title><?the_title()?> - <?bloginfo('name');?></title>
 <meta name="keywords" content="<?the_title()?>,<?bloginfo('name');?>" />
 <meta name="description" content="<?the_excerpt()?>" />

栏目路径栏目路径

<a href="<?bloginfo('url');?>">首页</a> 

<?php
	if($now_cat_parent_id != null){
?>

&gt; <a href="<?=get_category_link($now_cat_parent_id)?>"><?=get_cat_name($now_cat_parent_id);?></a>

<?php
	}
?>

&gt; <a href="<?=$new_cat_url?>"><?=$now_cat_name?></a>




其他问题


子类分类模板继承父类分类模板

在functions.php中添加如下代码

/**
 *使子分类的category页面渲染父category页面的模板
 */
add_filter('category_template', 'f_category_template');
    function f_category_template($template){
    	$category = get_queried_object();
    	if($category->parent !='0'){
    		while($category->parent !='0'){
    			$category = get_category($category->parent);
    		}
    	}
    	
    	$templates = array();
     
    	if ( $category ) {
    		$templates[] = "category-{$category->slug}.php";
    		$templates[] = "category-{$category->term_id}.php";
    	}
    	$templates[] = 'category.php';
    	return locate_template( $templates );
    }


子类文章模板继承父类文章模板 及 文章模板跟随分类别名

在functions.php中添加如下代码

//获取文章页分类的别名 若多个类目取第一个 子目录取父目录别名
function get_article_cat_slug($cats){
    if(count($cats) == 0){
        return "";
    }else{
        if($cats[0]->parent != 0){
            return get_category($cats[0]->parent)->slug;
        }else{
            return $cats[0]->slug;
        }
    }
}

将single.php 清空并添加如下代码:

<?php
	$cats = get_the_category();
	$cat_slug = get_article_cat_slug($cats);
	get_template_part("article",$cat_slug);
?>

新建article.php 为文章默认模板
article-{slug}.php 为文章所在分类别名指定模板文件



获取文章内容第一张图片地址为缩略图

在functions.php中添加如下代码

function the_first_image() {
	global $post, $posts;
	$first_img = '';
	ob_start();
	ob_end_clean();
	$output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
	$first_img = $matches[1][0];

	if(empty($first_img)) {
	  $first_img = "/path/to/default.png";
	}
	return $first_img;
}

运用

<?php 
    $arclist = get_posts("numberposts=6&category=1"); 
    foreach ($arclist as $post):
?>

<img src="the_first_image()"/>

<?php
    endforeach;
?>



指定文章取值范围(从第几篇文章到第几篇文章)减少重复查询

在functions.php中添加如下代码

//$posts 文章数据(数组)
//$start 开始位置 数组下标(整型)
//$plus 取值数量(取多少个)(整型)
//$start = 0 $plus = 2  输出第 0,1 个文章
function get_limit_posts($posts,$start,$plus){
    $new_ararry = array();
    $end = $start+$plus;
    for($start;$start<$end;$start++){
        //echo $start;
        if($posts[$start] != null){
            array_push($new_ararry,$posts[$start]);
        }
        
    }
    return $new_ararry;
}

运用

<?php 
$arclist = get_posts("numberposts=10&category=14");
$limit_arc = get_limit_posts($arclist,0,2);//取 0,1
foreach ($limit_arc as $post):
?>


<?php
    endforeach;
?>

<?php 
$limit_arc = get_limit_posts($arclist,2,3);//取 2,3,4
foreach ($limit_arc as $post):
?>


<?php
    endforeach;
?>




functions.php 整体所有 复制专用

<?php

    
    function the_first_image() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
      $first_img = $matches[1][0];
     
      if(empty($first_img)) {
        $first_img = "/path/to/default.png";
      }
      return $first_img;
    }
    
    function get_first_image($post) {
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $post->post_content, $matches);
      $first_img = $matches[1][0];
     
      if(empty($first_img)) {
        $first_img = "/path/to/default.png";
      }
      return $first_img;
    }
    
    function get_limit_posts($posts,$start,$plus){
        $new_ararry = array();
        $end = $start+$plus;
        for($start;$start<$end;$start++){
            //echo $start;
            if($posts[$start] != null){
                array_push($new_ararry,$posts[$start]);
            }
            
        }
        return $new_ararry;
    }
    
    
    function get_child_cats($cats,$parentid){
    $new_ararry = array();
    foreach ($cats as $cat){
        if($cat->parent == $parentid){
            array_push($new_ararry,$cat);
        }
    }
    return $new_ararry;
}

    function get_all_parent_cats($cats){
    $new_ararry = array();
    foreach ($cats as $cat){
        if($cat->parent == null ){
            array_push($new_ararry,$cat);
        }
    }
    return $new_ararry;
}

    
/**
 *使子分类的category页面渲染父category页面的模板
 */
add_filter('category_template', 'f_category_template');
    function f_category_template($template){
    	$category = get_queried_object();
    	if($category->parent !='0'){
    		while($category->parent !='0'){
    			$category = get_category($category->parent);
    		}
    	}
    	
    	$templates = array();
     
    	if ( $category ) {
    		$templates[] = "category-{$category->slug}.php";
    		$templates[] = "category-{$category->term_id}.php";
    	}
    	$templates[] = 'category.php';
    	return locate_template( $templates );
    }


//获取文章页分类的别名 若多个类目取第一个 子目录取父目录别名
function get_article_cat_slug($cats){
    if(count($cats) == 0){
        return "";
    }else{
        if($cats[0]->parent != 0){
            return get_category($cats[0]->parent)->slug;
        }else{
            return $cats[0]->slug;
        }
    }
}

function get_cats_parent_child($cat,$cats){
    $new_ararry = array();
    if($cat->parent != 0){
        array_push($new_ararry,get_term($cat->parent,'category'));
        $child_cats = get_child_cats($cats,$cat->parent);
        if(count($child_cats) != 0){
            foreach ($child_cats as $cat){
                array_push($new_ararry,$cat);
            }
        }
    }else{
        array_push($new_ararry,$cat);
        $child_cats = get_child_cats($cats,$cat->term_id);
        foreach ($child_cats as $cat){
                array_push($new_ararry,$cat);
        }
    }
    return $new_ararry;
}

?>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值