```
<?php
/**
* wp_yiqi 函數和定義
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package wp_yiqi
*/
if ( ! defined( '_S_VERSION' ) ) {
// 在每個版本中替換主題的版本號。
define( '_S_VERSION', '1.0.0' );
}
if ( ! function_exists( 'wp_yiqi_setup' ) ) :
/**
* 設置默認主題并注冊對各種WordPress特性的支持。
*
* 注意,這個函數被連接到after_setup_theme鉤子中,它
* 在init鉤子之前運行。init鉤子對于某些特性來說太晚了,比如
* 表示支持post縮略圖。
*/
function wp_yiqi_setup() {
/*
* 去除頭部多余加載信息
* @link https://www.sucaihu.com/14471.html
*/
remove_action( 'wp_head', 'wp_generator' );//移除WordPress版本
remove_action( 'wp_head', 'rsd_link' );//移除離線編輯器開放接口
remove_action( 'wp_head', 'wlwmanifest_link' );//移除離線編輯器開放接口
remove_action( 'wp_head', 'index_rel_link' );//去除本頁唯一鏈接信息
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); //清除前后文信息
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );//清除前后文信息
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//清除前后文信息
remove_action( 'wp_head', 'feed_links', 2 );//移除文章和評論feed
remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除分類等feed
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); //移除wp-json
remove_action( 'wp_head', 'print_emoji_detection_script', 7 ); //頭部的JS代碼
// add_filter( 'show_admin_bar', '__return_false' );//移除wp-json鏈接
remove_action( 'wp_head', 'rel_canonical' ); //rel=canonical
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 ); //rel=shortlink
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );//移除emoji載入js
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );//emoji載入js
remove_action( 'wp_print_styles', 'print_emoji_styles' );//移除emoji載入css
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action('wp_head','wp_resource_hints',2);//移除dns-prefetch
// 禁用 REST API、移除 wp-json @link https://www.hack520.com/474.html
add_filter('rest_enabled', '_return_false');
add_filter('rest_jsonp_enabled', '_return_false');
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
//移除頭部多余.recentcomments 樣式
function Fanly_remove_recentcomments_style() {
global $wp_widget_factory;
remove_action( 'wp_head', array( $wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style' ) );
}
add_action( 'widgets_init', 'Fanly_remove_recentcomments_style' );
// 移除頭部多余block-library/style.min.css
add_action('wp_enqueue_scripts', function () {
wp_dequeue_style('wp-block-library');
});
// 移除底部wp-embed.min.js文件
function disable_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );
}
add_action( 'init', 'disable_emojis' );
/**
* Filter function used to remove the tinymce emoji plugin.
*/
function disable_emojis_tinymce( $plugins ) {
if ( is_array( $plugins ) ) {
return array_diff( $plugins, array( 'wpemoji' ) );
} else {
return array();
}
}
function disable_embeds_init() {
global $wp;
$wp->public_query_vars = array_diff( $wp->public_query_vars, array( 'embed', ) );
remove_action( 'rest_api_init', 'wp_oembed_register_route' );
add_filter( 'embed_oembed_discover', '__return_false' );
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );
add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
}
add_action( 'init', 'disable_embeds_init', 9999 );
function disable_embeds_tiny_mce_plugin( $plugins ) { return array_diff( $plugins, array( 'wpembed' ) ); }
function disable_embeds_rewrites( $rules ) {
foreach ( $rules as $rule => $rewrite ) { if ( false !== strpos( $rewrite, 'embed=true' ) ) { unset( $rules[ $rule ] ); } }
return $rules;
}
function disable_embeds_remove_rewrite_rules() { add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' ); flush_rewrite_rules(); }
register_activation_hook( __FILE__, 'disable_embeds_remove_rewrite_rules' );
function disable_embeds_flush_rewrite_rules() { remove_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' ); flush_rewrite_rules(); }
register_deactivation_hook( __FILE__, 'disable_embeds_flush_rewrite_rules' );
/*
* 為翻譯提供主題。
* 譯文可以在/languages/目錄中存檔。
* 如果您正在基于wp_yiqi構建主題,請使用find and replace選項
* 將'wp_yiqi'更改為所有模板文件中的主題名稱。
*/
load_theme_textdomain( 'wp_yiqi', get_template_directory() . '/languages' );
// 添加默認的文章和評論的RSS提要鏈接到頭部。
add_theme_support( 'automatic-feed-links' );
/*
* 讓WordPress管理文檔標題。
* 通過添加主題支持,我們聲明這個主題不使用
* 在文檔頭中使用ard編碼的<title>標簽,預計WordPress也會這樣做
* 為我們提供吧。
*/
/* add_theme_support( 'title-tag' );
* 這里通過注釋代碼,刪除了<title>標簽。
*/
/*
* 自定義文章類型標題顯示問題
* 使用setTitle();代替wp_title();
* 因為現在不打算搞自定義文章類型,所以注釋掉。
*/
// function setTitle(){
// $term = get_term_by('slug',get_query_var('term'),get_query_var('taxonomy'));
// echo $title = $term->name;
// }
/*
* 啟用對帖子和頁面的帖子縮略圖的支持。
*
* @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
*/
add_theme_support( 'post-thumbnails' );
/*
* 注冊自定義尺寸的縮略圖
*/
if (function_exists('add_image_size')){
add_image_size( 'news_list_size', 180, 125, true );
}
//設定摘要的長度
function new_excerpt_length($length) {
return 120;
}
add_filter('excerpt_length', 'new_excerpt_length');
// 把摘要默認的結尾[...]換成...
function new_excerpt_more(){
global $post;
return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
/*
* 注冊導航菜單
* 這里分別注冊了頭部導航、底部導航、內頁側欄導航,可根據需要增刪。
* 使用方法:在主題一個位置使用wp_nav_menu()
*/
register_nav_menus(
array(
'header-menu' => esc_html__( 'Primary', 'wp_yiqi' ),
'footer-menu' => esc_html__( 'Footnav', 'wp_yiqi' ),
// 'about-menu' => esc_html__( 'Aboutnav', 'wp_yiqi' ),
)
);
/*
* 刪除導航li的多余id和class
*/
// add_filter('nav_menu_css_class', 'rm_css_attributes_filter', 100, 1);
add_filter('nav_menu_item_id', 'rm_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'rm_css_attributes_filter', 100, 1);
function rm_css_attributes_filter($var) {
return is_array($var) ? array() : '';
}
/*
* WordPress實現不同分類顯示不同的文章數量分頁
* @link https://www.xuewangzhan.net/wpbbs/17057.html
*/
function filter_pre_get_posts( $query ){
if ( $query->is_main_query() ){
$num = '';
// if ( is_category(array('news')) ){ $num = 3; } // 為指定ID的欄目設置每頁顯示個數
// if ( is_taxonomy_hierarchical('product', 'img') ){ $num = 9; } // 為指定自定義分類法設置每頁顯示個數
//if ( is_category(array(10)) ){ $num = 14; }
//if ( is_category(array('questions')) ){ $num = 14; }
// if ( in_category(array('jhg','hjj','yjj','xxj','jlg')) ){ $num = 10; }
//if ( is_home() ){ $num = 10; }
// else if ( is_category() ){ $num = 10; }
// else if ( is_tag() ){ $num = 10; }
// else if ( is_date() ){ $num = 10; }
// else if ( is_author() ){ $num = 10; }
// else if ( is_search() ){ $num = 10; }
// else if ( is_archive() ){ $num = 10; }
if ( '' != $num ){ $query->set( 'posts_per_page', $num ); }
}
return $query;
}
add_action('pre_get_posts', 'filter_pre_get_posts');
/*
* 獲取文章所在的自定義分類法分類項目
* 本主題不使用自定義分類法,所以注釋掉
*/
// function custom_taxonomies_terms_links(){
// //根據當前文章ID獲取文章信息
// $post = get_post( $post->ID );
// //獲取當前文章的文章類型
// $post_type = $post->post_type;
// //獲取文章所在的自定義分類法
// $taxonomies = get_object_taxonomies( $post_type, 'objects' );
// $out = array();
// foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
// $term_list = wp_get_post_terms($post->ID, $taxonomy_slug, array("fields" => "all"));
// echo $term_list[0]->name; //顯示文章所處的分類中的第一個
// }
// return implode('', $out );
// }
/* 文章列表頁分頁
* 在文章列表循環結束后調用mo_paging()方法即可顯示
* @link https://www.daimadog.com/3272.html
*/
function mo_paging() {
$p = 3;
if ( is_singular() ) return;
global $wp_query, $paged;
$max_page = $wp_query->max_num_pages;
if ( $max_page == 1 ) return;
echo '<div class="pagination"><dl>';
if ( empty( $paged ) ) $paged = 1;
echo '<dd class="prev-page">'; previous_posts_link('上一頁'); echo '</dd>';
if ( $paged > $p + 1 ) _paging_link( 1, '<dd>第一頁</dd>' );
if ( $paged > $p + 2 ) echo "<dd><span>···</span></dd>";
for( $i = $paged - $p; $i <= $paged + $p; $i++ ) {
if ( $i > 0 && $i <= $max_page ) $i == $paged ? print "<dd class=\"active\"><span>{$i}</span></dd>" : _paging_link( $i );
}
if ( $paged < $max_page - $p - 1 ) echo "<dd><span> ... </span></dd>";
echo '<dd class="next-page">'; next_posts_link('下一頁'); echo '</dd>';
echo '<dd><span>共 '.$max_page.' 頁</span></dd>';
echo '</dl></div>';
}
function _paging_link( $i, $title = '' ) {
if ( $title == '' ) $title = "第 {$i} 頁";
echo "<dd><a href='", esc_html( get_pagenum_link( $i ) ), "'>{$i}</a></dd>";
}
/*
* WordPress文章自動調用縮略圖
*
* 1.判斷文章是否設置了特色圖像,若有則顯示特色圖像;
* 2.若沒有特色圖像時,查找文章中是否包含圖片,若有圖片,則調用第一張圖片;
*/
function autoset_featured() {
global $post;
$already_has_thumb = has_post_thumbnail($post->ID);
if (!$already_has_thumb) {
$attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
if ($attached_image) {
foreach ($attached_image as $attachment_id => $attachment) {
set_post_thumbnail($post->ID, $attachment_id);
}
}
}
}
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');
/*
* 為WordPress后臺文章列表添加縮略圖
*
* @link https://zmingcx.com/back-to-list-of-articles-to-add-thumbnail-wordpress.html
*/
if ( !function_exists('fb_AddThumbColumn') && function_exists('add_theme_support') ) {
// for post and page
add_theme_support('post-thumbnails', array( 'post', 'page' ) );
function fb_AddThumbColumn($cols) {
$cols['thumbnail'] = __('Thumbnail');
return $cols;
}
function fb_AddThumbValue($column_name, $post_id) {
$width = (int) 160;
$height = (int) 120;
if ( 'thumbnail' == $column_name ) {
// thumbnail of WP 2.9
$thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true );
// image from gallery
$attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') );
if ($thumbnail_id)
$thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true );
elseif ($attachments) {
foreach ( $attachments as $attachment_id => $attachment ) {
$thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true );
}
}
if ( isset($thumb) && $thumb ) {
echo $thumb;
} else {
echo __('None');
}
}
}
// for posts
add_filter( 'manage_posts_columns', 'fb_AddThumbColumn' );
add_action( 'manage_posts_custom_column', 'fb_AddThumbValue', 10, 2 );
// for pages
add_filter( 'manage_pages_columns', 'fb_AddThumbColumn' );
add_action( 'manage_pages_custom_column', 'fb_AddThumbValue', 10, 2 );
}
/* 如何獲得WordPress文章瀏覽次數的統計
* 在需要顯示該統計次數的地方使用下面的代碼調用即可: 文章被閱讀:<?php post_views(' ', ' 次'); ?>
*/
function record_visitors() {
if (is_singular())
{
global $post;
$post_ID = $post->ID;
if($post_ID)
{
$post_views = (int)get_post_meta($post_ID, 'views', true);
if(!update_post_meta($post_ID, 'views', ($post_views+1)))
{
add_post_meta($post_ID, 'views', 1, true);
}
}
}
}
add_action('wp_head', 'record_visitors');
/// 函數名稱:post_views
/// 函數作用:取得文章的閱讀次數
function post_views($before = '(點擊 ', $after = ' 次)', $echo = 1)
{
global $post;
$post_ID = $post->ID;
$views = (int)get_post_meta($post_ID, 'views', true);
if ($echo) echo $before, number_format($views), $after;
else return $views;
}
/*
* 在wordpress函數文件里告訴Contact Form 7忽略這個檢測
* 因為本主題暫不使用Contact Form 7插件,所以先注釋掉
*/
// add_filter( 'wpcf7_validate_configuration', '__return_false' );
// for posts
// add_filter( 'manage_posts_columns', 'fb_AddThumbColumn' );
// add_action( 'manage_posts_custom_column', 'fb_AddThumbValue', 10, 2 );
// for pages
// add_filter( 'manage_pages_columns', 'fb_AddThumbColumn' );
// add_action( 'manage_pages_custom_column', 'fb_AddThumbValue', 10, 2 );
/*
* 切換搜索表單、評論表單和評論的默認核心標記
* 輸出有效的HTML5。
*/
add_theme_support(
'html5',
array(
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script',
)
);
// 設置WordPress核心自定義背景特性。
add_theme_support(
'custom-background',
apply_filters(
'wp_yiqi_custom_background_args',
array(
'default-color' => 'ffffff',
'default-image' => '',
)
)
);
// 為小部件的選擇性刷新添加主題支持。
add_theme_support( 'customize-selective-refresh-widgets' );
/**
* 添加對核心自定義logo的支持。
*
* @link https://codex.wordpress.org/Theme_Logo
*/
add_theme_support(
'custom-logo',
array(
'height' => 250,
'width' => 250,
'flex-width' => true,
'flex-height' => true,
)
);
/**
* 啟用媒體文件上傳自動按年月日重命名
*/
function git_upload_filter($file) {
$time = date("YmdHis");
$file['name'] = $time . "" . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
return $file;
}
add_filter('wp_handle_upload_prefilter', 'git_upload_filter');
/*
* 在后臺“外觀”菜單中添加 【主題設置】 這個子菜單
* add_theme_page():給“外觀”導航創建子菜單;
* 參數解說:
* 參數1-----標題的內容
* 參數2-----顯示在后臺左邊菜單的標題
* 參數3-----訪問這個頁面需要的權限
* 參數4-----別名,需要獨一無二哦
* 參數5-----執行的函數(我們自定義的函數)
*
* add_action():這是一個添加勾子的函數,這里將“Themes_Set”函數添加到“admin_menu”后臺菜單的勾子中。
*
* @link http://wanlimm.com/77202006258703.html
*/
function Themes_Set(){
add_theme_page( 'wp_yiqi主題設置', 'wp_yiqi主題設置', 'administrator', 'wp_yiqi_slug','ssmay_set');
}
add_action('admin_menu', 'Themes_Set');
function ssmay_set(){ //主題設置函數
include("theme_set.php");//這里是自己創建的一個php文件,用來設置選項內容
}
}
endif;
add_action( 'after_setup_theme', 'wp_yiqi_setup' );
/**
* 根據主題的設計和樣式表設置內容寬度(以像素為單位)。
*
* 優先級為0,使其可用于低優先級的回調。
*
* @global int $content_width
*/
function wp_yiqi_content_width() {
$GLOBALS['content_width'] = apply_filters( 'wp_yiqi_content_width', 640 );
}
add_action( 'after_setup_theme', 'wp_yiqi_content_width', 0 );
/**
* 開啟wordpress友情鏈接管理
*
* 使用方法:
* <ul class="dhnblog_link"><center>
* <li>友情鏈接: <?php wp_list_bookmarks('title_li=&categorize=0&orderby=rand&limit=24'); ?></li>
* </ul>
*
* @link https://blog.csdn.net/qq_39086902/article/details/105523963
*/
add_filter( 'pre_option_link_manager_enabled', '__return_true' );
/**
* 注冊小部件。
*
* @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
*/
function wp_yiqi_widgets_init() {
register_sidebar(
array(
'name' => esc_html__( 'Sidebar', 'wp_yiqi' ),
'id' => 'sidebar-1',
'description' => esc_html__( '添加小部件。', 'wp_yiqi' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
)
);
}
add_action( 'widgets_init', 'wp_yiqi_widgets_init' );
/**
* 排隊引入css樣式和js腳本。
* 模板中通過使用wp_head()和wp_footer()分別來引入頁頭頁腳的css和js文件
* 在WordPress管理后臺正確加載js和css腳本
* @link https://www.wpdaxue.com/wordpress-admin-enqueue-scripts.html
*
* 使用 wp_enqueue_style() 函數引入css文件教程:
* wp_enqueue_style( $handle, $src, $deps, $ver, $media );
* --------------------------------------------------------
* $handle(字符串,必需)是你的樣式表唯一名稱。其他函數將使用這個“handle”來排隊并打印樣式表。
* $src(字符串,必需)指的是樣式表的URL。您可以使用函數,如 get_template_directory_uri() 來獲取主題目錄中的樣式文件。永遠不要去想硬編碼了!
* $deps (數組,可選)處理相關樣式的名稱。如果丟失某些其他樣式文件將導致你的樣式表將無法正常工作,你可以使用該參數設置“依賴關系”。
* $ver (字符串或布爾型,可選)版本號。你可以使用你的主題的版本號或任何一個你想要的。如果您不希望使用一個版本號,將其設置為null。默認為false,這使得WordPress的添加自己的版本號。
* $media (字符串,可選)是指CSS的媒體類型,比如“screen”或“handheld”或“print”。如果你不知道是否需要使用這個,那就不使用它。默認為“all”。
* @link https://www.wpdaxue.com/loading-css-into-wordpress.html
*
* 使用 wp_enqueue_script() 引入js文件
* wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
* --------------------------------------------------------
* $handle(字符串,必需)是你的樣式表唯一名稱。其他函數將使用這個“handle”來排隊并打印樣式表。
* $src(字符串,必需)指的是樣式表的URL。您可以使用函數,如 get_template_directory_uri() 來獲取主題目錄中的樣式文件。永遠不要去想硬編碼了!
* $deps (數組,可選)處理相關樣式的名稱。如果丟失某些其他樣式文件將導致你的樣式表將無法正常工作,你可以使用該參數設置“依賴關系”。
* $ver (字符串或布爾型,可選)版本號。你可以使用你的主題的版本號或任何一個你想要的。如果您不希望使用一個版本號,將其設置為null。默認為false,這使得WordPress的添加自己的版本號。
* $in_footer 是否在</body>之前而不是<head>中加載腳本。默認為'false'。
* @link https://www.wpdaxue.com/how-to-add-css-and-javascript-to-wordpress-theme.html
*
*/
function wp_yiqi_scripts() {
// 加載css
wp_enqueue_style( 'wp_yiqi-style', get_stylesheet_uri(), array(), _S_VERSION );
wp_enqueue_style( 'wp_yiqi-main', get_template_directory_uri() . '/assets/css/main.css', array(), _S_VERSION );
wp_enqueue_style( 'wp_yiqi-flexslider-css', '//cdn.bootcss.com/flexslider/2.6.3/flexslider.min.css', array(), _S_VERSION );
// 加載頁眉js
wp_enqueue_script('wp_yiqi-jquery', '//cdn.bootcdn.net/ajax/libs/jquery/1.12.4/jquery.min.js', array(), _S_VERSION, false );
wp_enqueue_script('wp_yiqi-flexslider-js', '//cdn.bootcss.com/flexslider/2.6.3/jquery.flexslider-min.js', array(), _S_VERSION, false );
wp_enqueue_script( 'wp_yiqi-header', get_template_directory_uri() . '/assets/js/header.js', array(), _S_VERSION, false );
// 加載頁腳js
if ( is_home() || is_front_page() ) { //判斷首頁與內頁
wp_enqueue_script( 'wp_yiqi-footer-index', get_template_directory_uri() . '/assets/js/footer-index.js', array(), _S_VERSION, true );
} else {
wp_enqueue_script( 'wp_yiqi-footer-sidebar', get_template_directory_uri() . '/assets/js/footer-sidebar.js', array(), _S_VERSION, true );
}
wp_enqueue_script( 'wp_yiqi-footer', get_template_directory_uri() . '/assets/js/footer.js', array(), _S_VERSION, true );
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
add_action( 'wp_enqueue_scripts', 'wp_yiqi_scripts' );
/**
* 實現自定義頭特性。
*/
require get_template_directory() . '/inc/custom-header.php';
/**
* 此主題的自定義模板標記。
*/
require get_template_directory() . '/inc/template-tags.php';
/**
* 通過連接到WordPress來增強主題的功能。
*/
require get_template_directory() . '/inc/template-functions.php';
/**
* 編輯器添加。
*/
require get_template_directory() . '/inc/customizer.php';
/**
* 加載Jetpack兼容文件。
*/
if ( defined( 'JETPACK__VERSION' ) ) {
require get_template_directory() . '/inc/jetpack.php';
}
```