wordpress开发:function.php

隐藏加载的JS/css版本号

[php]
function sb_remove_script_version( $src ){
$parts = explode( ‘?’, $src );
return $parts[0];
}
add_filter( ‘script_loader_src’, ‘sb_remove_script_version’, 15, 1 );
add_filter( ‘style_loader_src’, ‘sb_remove_script_version’, 15, 1 );
[/php]

 

移除WordPress文章图片的宽度和高度属性

[php]
add_filter( ‘post_thumbnail_html’, ‘remove_width_attribute’, 10 );
add_filter( ‘image_send_to_editor’, ‘remove_width_attribute’, 10 );
function remove_width_attribute( $html ) {
$html = preg_replace( ‘/(width|height)="\d*"\s/’, "", $html );
return $html;
}
[/php]

 

加入侧边栏左边菜单

[php]
register_nav_menus( array(
‘sidebar_menu’ => ‘内容左边菜单’
) );

register_nav_menus( array(
‘primary’ => ‘顶部菜单’
) );
[/php]

 

Post Thumbnail Support

[php]
add_theme_support( ‘post-thumbnails’ );
add_image_size( ‘dtimgs’, 500, 250, true ); //动态和常见问题右侧图
add_image_size( ‘indexso’, 400, 306, true ); //首页index展示
add_image_size( ‘indextab’, 280, 182, true ); //首页indexTAB分类切换展示
add_theme_support( ‘post-thumbnails’, array( ‘post’, ‘page’ ) );
[/php]

 

调用SSL头像链接

[php]
function get_ssl_avatar($avatar) {
$avatar = preg_replace(‘/.*\/avatar\/(.*)\?s=([\d]+)&.*/’,'<img src="https://secure.gravatar.com/avatar/$1?s=$2" class="avatar avatar-$2" height="$2" width="$2">’,$avatar);
return $avatar;
}
add_filter(‘get_avatar’, ‘get_ssl_avatar’);
[/php]

 

前台全局加入脚本

[php]
function my_enqueue_scripts_frontpage() {

wp_enqueue_script(
‘jquery-2-script’,
get_template_directory_uri() . ‘/js/jquery-2.1.1.min.js’,
array(‘jquery’),
‘2.1.1’,
false
);
wp_enqueue_script( ‘SuperSlide’, get_template_directory_uri() . ‘/js/jquery.SuperSlide.2.1.1.js’, array( ‘jquery’ ) ,’2.1.1′ , false);

wp_enqueue_script(
‘bootstrap-script’,
get_template_directory_uri() . ‘/js/bootstrap.min.js’,
array(‘jquery’),
‘3.3.1’,
true
);

wp_enqueue_script(
‘uptop’,
get_template_directory_uri() . ‘/js/top.js’,
array(‘jquery’),
‘1.3.1’,
true
);

}

add_action(‘wp_enqueue_scripts’, ‘my_enqueue_scripts_frontpage’);
[/php]

 

在首页和内容页加入JS

[php]
function enqueue_my_script()
{

if ( is_home() )
{
wp_enqueue_script( ‘SuperSlide’, get_template_directory_uri() . ‘/js/jquery.SuperSlide.2.1.1.js’, array( ‘jquery’ ) ,’2.1.1′ , false);
}
}
add_action(‘wp_enqueue_scripts’,’enqueue_my_script’);
[/php]

 

不同的文章分类调用不同的 single.php文件

[php]
function post_is_in_descendant_category( $cats, $_post = null )
{
foreach ( (array) $cats as $cat ) {
// get_term_children() accepts integer ID only
$descendants = get_term_children( (int) $cat, ‘category’);
if ( $descendants && in_category( $descendants, $_post ) )
return true;
}
return false;
}
[/php]

 

WordPress 添加面包屑导航

[php]
function taiji_breadcrumbs() {
$delimiter = ‘»’; // 分隔符
$before = ‘<span class="current">’; // 在当前链接前插入
$after = ‘</span>’; // 在当前链接后插入
if ( !is_home() && !is_front_page() || is_paged() ) {
echo ‘
<div itemscope itemtype="http://schema.org/WebPage" id="crumbs">’.__( ‘您在这里:’ , ‘taiji’ );
global $post;
$homeLink = home_url();
echo ‘ <a itemprop="breadcrumb" href="’ . $homeLink . ‘">’ . __( ‘首页’ , ‘taiji’ ) . ‘</a> ‘ . $delimiter . ‘ ‘;
if ( is_category() ) { // 分类 存档
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0){
$cat_code = get_category_parents($parentCat, TRUE, ‘ ‘ . $delimiter . ‘ ‘);
echo $cat_code = str_replace (‘<a’,'<a itemprop="breadcrumb"’, $cat_code );
}
echo $before . ” . single_cat_title(”, false) . ” . $after;
} elseif ( is_day() ) { // 天 存档
echo ‘<a itemprop="breadcrumb" href="’ . get_year_link(get_the_time(‘Y’)) . ‘">’ . get_the_time(‘Y’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo ‘<a itemprop="breadcrumb" href="’ . get_month_link(get_the_time(‘Y’),get_the_time(‘m’)) . ‘">’ . get_the_time(‘F’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $before . get_the_time(‘d’) . $after;
} elseif ( is_month() ) { // 月 存档
echo ‘<a itemprop="breadcrumb" href="’ . get_year_link(get_the_time(‘Y’)) . ‘">’ . get_the_time(‘Y’) . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $before . get_the_time(‘F’) . $after;
} elseif ( is_year() ) { // 年 存档
echo $before . get_the_time(‘Y’) . $after;
} elseif ( is_single() && !is_attachment() ) { // 文章
if ( get_post_type() != ‘post’ ) { // 自定义文章类型
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
echo ‘<a itemprop="breadcrumb" href="’ . $homeLink . ‘/’ . $slug[‘slug’] . ‘/">’ . $post_type->labels->singular_name . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $before . get_the_title() . $after;
} else { // 文章 post
$cat = get_the_category(); $cat = $cat[0];
$cat_code = get_category_parents($cat, TRUE, ‘ ‘ . $delimiter . ‘ ‘);
echo $cat_code = str_replace (‘<a’,'<a itemprop="breadcrumb"’, $cat_code ); echo $before . get_the_title() . $after; } } elseif ( !is_single() && !is_page() && get_post_type() != ‘post’ ) { $post_type = get_post_type_object(get_post_type()); echo $before . $post_type->labels->singular_name . $after;
} elseif ( is_attachment() ) { // 附件
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID); $cat = $cat[0];
echo ‘<a itemprop="breadcrumb" href="’ . get_permalink($parent) . ‘">’ . $parent->post_title . ‘</a> ‘ . $delimiter . ‘ ‘;
echo $before . get_the_title() . $after;
} elseif ( is_page() && !$post->post_parent ) { // 页面
echo $before . get_the_title() . $after;
} elseif ( is_page() && $post->post_parent ) { // 父级页面
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] = ‘<a itemprop="breadcrumb" href="’ . get_permalink($page->ID) . ‘">’ . get_the_title($page->ID) . ‘</a>’;
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb . ‘ ‘ . $delimiter . ‘ ‘;
echo $before . get_the_title() . $after;
} elseif ( is_search() ) { // 搜索结果
echo $before ;
printf( __( ‘Search Results for: %s’, ‘taiji’ ), get_search_query() );
echo $after;
} elseif ( is_tag() ) { //标签 存档
echo $before ;
printf( __( ‘Tag Archives: %s’, ‘taiji’ ), single_tag_title( ”, false ) );
echo $after;
} elseif ( is_author() ) { // 作者存档
global $author;
$userdata = get_userdata($author);
echo $before ;
printf( __( ‘Author Archives: %s’, ‘taiji’ ), $userdata->display_name );
echo $after;
} elseif ( is_404() ) { // 404 页面
echo $before;
_e( ‘Not Found’, ‘taiji’ );
echo $after;
}
if ( get_query_var(‘paged’) ) { // 分页
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
echo sprintf( __( ‘( Page %s )’, ‘taiji’ ), get_query_var(‘paged’) );
}
echo ‘</div>

‘;
}
}
[/php]

 

翻页导航

[php]
function par_pagenavi($range = 9){
global $paged, $wp_query;
if ( !$max_page ) {$max_page = $wp_query->max_num_pages;}
if($max_page > 1){if(!$paged){$paged = 1;}
if($paged != 1){echo "<a href=’" . get_pagenum_link(1) . "’ class=’extend’ title=’跳转到首页’> 返回首页 </a>";}
previous_posts_link(‘ 上一页 ‘);
if($max_page > $range){
if($paged < $range){for($i = 1; $i <= ($range + 1); $i++){echo "<a href=’" . get_pagenum_link($i) ."’"; if($i==$paged)echo " class=’current’";echo ">$i</a>";}}
elseif($paged >= ($max_page – ceil(($range/2)))){
for($i = $max_page – $range; $i <= $max_page; $i++){echo "<a href=’" . get_pagenum_link($i) ."’"; if($i==$paged)echo " class=’current’";echo ">$i</a>";}}
elseif($paged >= $range && $paged < ($max_page – ceil(($range/2)))){
for($i = ($paged – ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++){echo "<a href=’" . get_pagenum_link($i) ."’";if($i==$paged) echo " class=’current’";echo ">$i</a>";}}}
else{for($i = 1; $i <= $max_page; $i++){echo "<a href=’" . get_pagenum_link($i) ."’"; if($i==$paged)echo " class=’current’";echo ">$i</a>";}}
next_posts_link(‘ 下一页 ‘);
if($paged != $max_page){echo "<a href=’" . get_pagenum_link($max_page) . "’ class=’extend’ title=’跳转到最后一页’> 最后一页 </a>";}}
}
[/php]

 

屏蔽emoji

[php]
remove_action( ‘admin_print_scripts’, ‘print_emoji_detection_script’);
remove_action( ‘admin_print_styles’, ‘print_emoji_styles’);
remove_action( ‘wp_head’, ‘print_emoji_detection_script’, 7);
remove_action( ‘wp_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’);
[/php]

 

移除顶部多余信息

[php]
function wpbeginner_remove_version() {
return ;
} add_filter(‘the_generator’, ‘wpbeginner_remove_version’);//wordpress的版本号
remove_action(‘wp_head’, ‘feed_links’, 2);// 包含文章和评论的feed
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’, ‘rel_canonical’ );
remove_action(‘wp_head’, ‘wlwmanifest_link’); // 外部编辑器
remove_action( ‘wp_head’,’rsd_link’);//ML-RPC
[/php]

 

WordPress 后台禁用Google Open Sans字体,加速网站

[php]
add_filter( ‘gettext_with_context’, ‘wpdx_disable_open_sans’, 888, 4 );
function wpdx_disable_open_sans( $translations, $text, $context, $domain ) {
if ( ‘Open Sans font: on or off’ == $context && ‘on’ == $text ) {
$translations = ‘off’;
}
return $translations;
}
[/php]

 

 

发表评论

邮箱地址不会被公开。 必填项已用*标注