主题代码
<?php
require get_parent_theme_file_path( '/inc/template-tags.php' );
// ---------- 初始化 加载主样式表 ----------
if(!function_exists('koaya_theme_init')){
function koaya_theme_init(){
wp_enqueue_style('main_style',get_stylesheet_directory_uri().'/css/style.css');
}
}
add_action('wp_enqueue_scripts','koaya_theme_init');
// ---------- 后台样式 ----------
function koaya_enqueue_options_style() {
wp_enqueue_style( 'koaya-options-style', get_stylesheet_directory_uri().'/css/background_style.css' );
}
add_action( 'admin_enqueue_scripts', 'koaya_enqueue_options_style' );
//删除帮助
add_action('in_admin_header', function(){
global $current_screen;
$current_screen->remove_help_tabs();
});
// ---------- 缩略图支持 自定义尺寸 ----------
add_theme_support('post-thumbnails');
add_image_size( 'thumb-case', 562, 400, true );//产品详情
add_image_size( 'thumb-pro', 380, 268, true );//产品列表
add_image_size( 'thumb-news', 343, 426, true );//内刊
// ---------- 添加相册可选自定义尺寸 ----------
function custom_image_sizes_choose( $sizes ) {
$custom_sizes = array(
'thumb-post' => '轮播图'
);
return array_merge( $sizes, $custom_sizes );
}
add_filter( 'image_size_names_choose', 'custom_image_sizes_choose' );
// ---------- 菜单支持 菜单展示位 ----------
add_theme_support('menus');
register_nav_menus(array(
'main'=>'main',
'head'=>'head',
'footer'=>'footer',
));
// ---------- 为page添加摘要 ----------
function my_init() {
add_post_type_support('page', array('excerpt'));
}
add_action('init', 'my_init');
// ---------- 摘要自定义更多标记 ----------
function xz_excerpt_more( $more ) {
return '…';
}
add_filter( 'excerpt_more', 'xz_excerpt_more' );
// ---------- 为不同分类列表设置不同单页条目 ----------
$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);
function my_modify_posts_per_page() {
add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}
if(!function_exists('my_option_posts_per_page')){
function my_option_posts_per_page( $value ) {
global $option_posts_per_page;
//企业新闻5、专题报道5、行业资讯10、常见问题10、制浆知识10、案例9、产品9、内刊18
if(is_category(array(6,8,10))) {return 5;}
else if(is_category(array(9,35,36,37,38,39,42))) {return 10;}
else if(is_category(array(7,12,13,14,15))) {return 9;}
else if(is_category(array(11))) {return 12;}
else if(is_post_type_archive( 'product' )) {return 9;}
else if(is_tax('pro_cat')) {return 9;}
else if(is_post_type_archive('fitting')) {return 12;}
else if(is_tax('fit_cat')) {return 12;}
else {return $option_posts_per_page;}
}
}
// ---------- 隐藏前台admin_bar ----------
show_admin_bar(false);
// ---------- 摘要去掉p标签 ----------
remove_filter( 'the_excerpt', 'wpautop' );
// ---------- 移除顶部多余信息 ----------
remove_action('wp_head', 'index_rel_link');//当前文章的索引
remove_action('wp_head', 'feed_links_extra', 3);// 额外的feed,例如category, tag页
remove_action('wp_head', 'start_post_rel_link', 10, 0);// 开始篇
remove_action('wp_head', 'parent_post_rel_link', 10, 0);// 父篇
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // 上、下篇.
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//rel=pre
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );//rel=shortlink
remove_action('wp_head','rsd_link');//移除head中的rel="EditURI"
remove_action('wp_head','wlwmanifest_link');//移除head中的rel="wlwmanifest"
remove_action('wp_head','rsd_link');//rsd_link移除XML-RPC
remove_filter('the_content', 'wptexturize');//禁用半角符号自动转换为全角
remove_action('wp_head', 'feed_links', 2 );
remove_action('wp_head', 'locale_stylesheet' );
remove_action('wp_head', 'noindex', 1 );
remove_action('wp_head', 'wp_generator' );//显示WP版本
remove_action('wp_head', 'print_emoji_detection_script', 7);
// ---------- 后台添加页内分页按钮 ----------
add_filter('mce_buttons','wysiwyg_editor');
function wysiwyg_editor($mce_buttons) {
$pos = array_search('wp_more',$mce_buttons,true);
if ($pos !== false) {
$tmp_buttons = array_slice($mce_buttons, 0, $pos+1);
$tmp_buttons[] = 'wp_page';
$mce_buttons = array_merge($tmp_buttons, array_slice($mce_buttons, $pos+1));
}
return $mce_buttons;
}
// 设置后台登录界面
add_filter('login_headerurl', create_function(false,"return get_bloginfo( 'siteurl' );"));
add_action('login_head', 'xz_login_logo');
function xz_login_logo() {
echo '<style type="text/css">
.login h1 a {
background-image:url('.get_bloginfo('template_directory').'/img/xz-logo.png);
width:auto;
background-size:auto 75%;
background-position:center;
}
body.login {
background-image:url('.get_bloginfo('template_directory').'/img/xz-bg.jpg);
background-color:#feffff;
background-position:center bottom;
background-size:100% auto;
background-repeat:no-repeat;
}
</style>';
}
//隐藏后台Logo
function xz_admin_bar_remove() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu('wp-logo');
}
add_action('wp_before_admin_bar_render', 'xz_admin_bar_remove', 0);
//移除后台无用的菜单
add_action( 'admin_menu', function(){
//仪表盘 index.php | 多媒体 upload.php | 页面 edit.php?post_type=page | 插件 plugins.php | 工具 tools.php | 设置 options-general.php
remove_menu_page( 'edit-comments.php' ); //评论
});
//404页面
function sk_404_redirect() {
$sk_404_delay = 3; //页面跳转前的延时时间,单位是秒
$sk_rewrite_url = "https://www.zzyunda.com/"; //指定要跳转到的页面地址,可以是你的网站首页或任意页面
$sk_404_file = "silian.txt"; //指定需要写入的死链txt文件名
$sk_404_url = "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; //当前页面的url
$sk_404_content = file_get_contents($sk_404_file); //读取死链文件
if(strpos($sk_404_content, $sk_404_url) === false){ //如果当前url不在死链文件中,则把当前url写入死链文件
$fp = fopen($sk_404_file,"a");
flock($fp, LOCK_EX) ;
fwrite($fp, $sk_404_url."\n");
flock($fp, LOCK_UN);
fclose($fp);
}
header("Refresh:".$sk_404_delay.";url=".$sk_rewrite_url); //延时后跳转到指定页面
}
/**
* 自定义Loop循环
* $args: the query array
* $loop_temp: the loop template name
*/
function xizhitbu_diy_loop( $args, $loop_temp ) {
$myposts = new WP_Query( $args );
if($myposts -> have_posts()):
while( $myposts -> have_posts() ) : $myposts -> the_post();
get_template_part( 'template-parts/loop', $loop_temp );
endwhile;
endif;
wp_reset_postdata();//重置请求数据
}
/**
* 输出子terms列表
* $cat: the category object
* $tax: default pro_cat
*/
function xizhitbu_subcat_list( $cat, $tax='pro_cat' ) {
$subcats = get_term_children( $cat->term_id, $tax );
if(is_array($subcats)){
foreach ($subcats as $subcat_id) {
echo "<li class='cat_".get_term_field('term_id', $subcat_id, $tax)."'><a title='";
echo get_term_field( 'name', $subcat_id, $tax );
echo "' href='";
echo get_term_link( $subcat_id, $tax );
echo "'>";
echo get_term_field( 'name', $subcat_id, $tax );
echo "</a></li> ";
}
}
}
/**
* 获取最末层级的terms
* $tax: the taxonomy in which to find
*/
function xizhitbu_get_the_last_term( $tax ) {
//Get all terms associated with post in taxonomy 'category'
$terms = get_the_terms(get_the_ID(),$tax);
//Get an array of their IDs
$term_ids = wp_list_pluck($terms,'term_id');
//Get array of parents - 0 is not a parent
$parents = array_filter(wp_list_pluck($terms,'parent'));
//Get array of IDs of terms which are not parents.
$term_ids_not_parents = array_diff($term_ids, $parents);
//Get corresponding term objects
$terms_not_parents = array_values(array_intersect_key($terms, $term_ids_not_parents));
return $terms_not_parents;
}
/**
* 获取缩略图,若没有设置,则显示默认图片
* $thumb_size: the thumbnail size name
*/
function xizhitbu_get_thumbnail( $thumb_size ) {
if ( get_the_post_thumbnail() ) {
the_post_thumbnail( $thumb_size );
} else {
echo "<img src='";
bloginfo('stylesheet_directory');
echo "/img/default-thumb.jpg' alt='";
the_title();
echo "'>";
}
}
/**
* 在列表页输出指定分类法的terms菜单
* 默认输出一级类别,可指定父分类id
*
* $tax: the taxonomy
* $parent: default as 0
* $loop_temp: 选填单项模版id,不填直接输出链接
* 模版项中使用变量:$term, $term_link
*/
function xizhitbu_show_tax_terms( $tax, $parent=0, $loop_temp ) {
$args = array(
'taxonomy' => $tax,
'hide_empty' => false,
'parent' => $parent
);
$terms = get_terms($args);
$term_now = get_queried_object()->term_id;
if(is_array($terms)){
foreach ($terms as $term) {
$term_link = get_term_link($term->term_id, $tax);
if(!$loop_temp){
echo '<li class="menu-item cat_';
echo get_term_field('term_id', $term, $tax);
if($term->term_id == $term_now){
echo ' current-menu-item';
}else if($term->term_id == wp_get_term_taxonomy_parent_id($term_now,$tax) || $term->term_id == wp_get_term_taxonomy_parent_id(wp_get_term_taxonomy_parent_id($term_now,$tax),$tax) ){
echo ' current-menu-item';
}
echo '"><a href="'.$term_link.'">';
echo $term->name;
echo '</a></li> ';
}else{
include get_theme_file_path( 'template-parts/loop-'.$loop_temp.'.php' );
}
}
}
}
/**
* 输出指定分类法的所有terms菜单
* 输出一级及二级类别
*
* $tax: the taxonomy
*/
function xizhitbu_show_tax_all_terms( $tax ) {
$args = array(
'taxonomy' => $tax,
'hide_empty' => false,
'parent' => 0
);
$terms = get_terms($args);
$term_now = get_queried_object()->term_id;
if(is_array($terms)){
foreach ($terms as $term) {
$term_link = get_term_link($term->term_id, $tax);
echo '<li class="parent-item cat_';
echo get_term_field('term_id', $term, $tax);
if($term->term_id == $term_now){
echo ' current-menu-item';
}
echo '"><a href="'.$term_link.'">';
echo $term->name;
echo '</a> <ul>';
xizhitbu_show_tax_terms( $tax, $term->term_id, '' );
echo '</ul> </li> ';
}
}
}
/**
* 输出相关推荐列表项
* 不包含包裹元素
*
* $template: 模版ID字符串
* $selection: 可供勾选相关内容的ACF字段
* $taxonomy: 推荐同类内容所使用的自定义分类法
* $query_args: 查询数组,array( 'post_type' => xx,
* 'order' => xx,
* 'orderby' => xx,
* 'posts_per_page' => xx );
*/
function xizhitbu_related_items( $template, $selection, $taxonomy, $query_args ) {
if( $selection ) {
$rel_posts = get_field($selection);
}
$term_now = xizhitbu_get_the_last_term($taxonomy);
if( $rel_posts[0] ) {
$query_args['post__in'] = $rel_posts;
} else {
$query_args['post__not_in'] = array( get_the_ID() );
$query_args['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'field' => 'slug',
'terms' => $term_now[0]->slug,
'operator' => 'IN'
)
);
}
xizhitbu_diy_loop( $query_args, $template);
}
/**
* 详情页上输出前一页后一页链接
*
* $taxonomy : 不填写则默认为category
*/
function xizhitbu_prev_next( $taxonomy = 'category' ){
echo '<div class="single-nav"> <p class="page-pre">';
previous_post_link('<span class="link">%link</span>','%title',true,'',$taxonomy);
echo '</p> <p class="page-next">';
next_post_link('<span class="link">%link</span>','%title',true,'',$taxonomy);
echo '</p> </div>';
}
/**
* 循环输出包含acf调用的模版内容
*
* $max: 最多循环次数
* $template: 位于/template-parts/下的模版名称,不包含“loop-前缀”及后缀
* $loop_item1: 事先使用acf注册好的xx1~xxn这样的一组字段的公共部分xx
* 根据第一项来判定是否进行循环,因此本项为必填项
* $loop_item2: 后几项为选填字断,根据模版中需要的acf字段选填。预留至第五项
*/
function xizhitbu_loop_acf( $max, $template, $loop_item1, $loop_item2='', $loop_item3='', $loop_item4='', $loop_item5='' ){
for($i=0;$i<=$max;$i++){
if(get_field($loop_item1.$i)){
include( get_stylesheet_directory().'/template-parts/loop-'.$template.'.php' );
}
}
}