中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

wordpress如何自動給圖片加ALT信息、標簽云等函數(shù)應用匯總

2018-11-02    來源:學做網(wǎng)站論壇

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

我們在學做網(wǎng)站時,使用wordpress程序,它的主題除了index.php、header.php、footer.php等文件之外,還有一個強大的函數(shù)文件functions.php,這個文件主要是為wordpress主題開發(fā)者提供的一個自定義模板文件,可以設(shè)置菜單、小工具等等很多功能效果。

wordpress如何自動給圖片加ALT信息、標簽云等函數(shù)應用匯總

但是在網(wǎng)站制作時,例 如做北奔重卡汽車公司網(wǎng)站時,修改這個文件需要你有一定的php基礎(chǔ)才可以自由編輯,如果修改不正確,嚴重的會直接導致網(wǎng)站打不開,提示錯誤,所以修改的時候不能馬虎大意。

雖然不懂的php語言,但是如果你足夠細心,也能夠借助別人已經(jīng)寫好的,或是wordpress官方提供的一些函數(shù)文件實現(xiàn)我們需要的效果,這個帖子就是為大家征集這些常用的函數(shù)應用的,如果你在學習當中遇到過一些應用性比較強的函數(shù)代碼,供學習做網(wǎng)站的學員們在網(wǎng)站制作時使用。

以下案例應用所有的函數(shù)代碼需放置在函數(shù)文件functions.php文件中,如果沒有此文件,可以直接創(chuàng)建一個文件,并放置以下代碼:
<?php
//函數(shù)代碼放置區(qū)
?>
wordpress函數(shù)應用案例一:
菜單函數(shù)注冊代碼:

//自定義菜單
register_nav_menus(
array(
'header-menu' => __( '導航自定義菜單' ),
)
);

調(diào)用代碼:

<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>

wordpress函數(shù)應用案例二:
啟用小工具函數(shù)代碼:

//小工具
if ( function_exists('register_sidebar') )
register_sidebar(array(
'before_widget' => '<div class="sidebox"> ',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
));

調(diào)用代碼:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
//可以放置在側(cè)邊欄的最上和最下
<?php endif; ?>

wordpress函數(shù)應用案例三:
擴展wordpress后臺編輯器功能按鈕函數(shù):

//為編輯器增加按鈕
function add_more_buttons($buttons) {
$buttons[] = 'hr';
$buttons[] = 'sub';
$buttons[] = 'sup';
$buttons[] = 'fontselect';
$buttons[] = 'fontsizeselect';
$buttons[] = 'cleanup';
$buttons[] = 'styleselect';
return $buttons;
}
add_filter("mce_buttons_3", "add_more_buttons");

調(diào)用方式:無需調(diào)用。

wordpress函數(shù)應用案例四:
添加wordpress外觀背景功能函數(shù):

//背景
add_custom_background();

調(diào)用方式,修改<body>標簽為以下代碼:

<body class="custom-background">

wordpress函數(shù)應用案例五:
去除多余頭部版權(quán)信息函數(shù):

//去除頭部多余信息
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_generator');
//remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'feed_links_extra', 3);
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_filter('the_content', 'wptexturize');

調(diào)用方式:無需調(diào)用。

wordpress函數(shù)應用案例六:
禁止代碼標點轉(zhuǎn)換函數(shù):

//禁止代碼標點轉(zhuǎn)換
remove_filter('the_content', 'wptexturize');

調(diào)用方式:無需調(diào)用。

wordpress函數(shù)應用案例七:
自動版權(quán)時間函數(shù):

//自動生成版權(quán)時間
function comicpress_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "? " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}

調(diào)用方式:在footer.php合適位置添加以下代碼:

<?php echo comicpress_copyright(); ?>

wordpress函數(shù)應用案例八:
評論支持貼圖函數(shù):

//評論貼圖
function embed_images($content) {
$content = preg_replace('/\[img=?\]*(.*?)(\[\/img)?\]/e', '"<img src=\"$1\" alt=\"" . basename("$1") . "\" />"', $content);
return $content;
}
add_filter('comment_text', 'embed_images');

調(diào)用方式:在評論文件comments.php中合適位置添加以下代碼:

<a href='javascript:embedImage();' >插入圖片</a>

wordpress函數(shù)應用案例九:
移除wordpress版本提示函數(shù):

//移除wordpress版本
function wpbeginner_remove_version() {return'';}
add_filter('the_generator', 'wpbeginner_remove_version');

調(diào)用方式:無需調(diào)用。

wordpress函數(shù)應用案例十:
彩色標簽云效果函數(shù):

//彩色標簽云
function colorCloud($text) {
$text = preg_replace_callback('|<a (.+?)>|i', 'colorCloudCallback', $text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
$color = dechex(rand(0,16777215));
$pattern = '/style=(\'|\")(.*)(\'|\")/i';
$text = preg_replace($pattern, "style=\"color:#{$color};$2;\"", $text);
return "<a $text>";
}

調(diào)用方式:無需調(diào)用。主題中有標簽調(diào)用函數(shù)即可。

wordpress函數(shù)應用案例十一:
閱讀全文添加nofollow標簽函數(shù):

//nofollow//
function add_nofollow_to_link($link) {
return str_replace('<a', '<a rel="nofollow"', $link);
}
add_filter('the_content_more_link','add_nofollow_to_link', 0);

調(diào)用方式:無需調(diào)用,文章添加閱讀更多自然生成。

wordpress函數(shù)應用案例12:
禁止wordpress程序版本和插件升級更新提示:

//去除升級提醒
add_filter(‘pre_site_transient_update_core’, create_function(‘$a’, “return null;”));
add_filter(‘pre_site_transient_update_plugins’, create_function(‘$a’, “return null;”));
add_filter(‘pre_site_transient_update_themes’, create_function(‘$a’, “return null;”));
//禁止檢測程序版本
remove_action(‘a(chǎn)dmin_init’, ‘_maybe_update_core’);
remove_action(‘a(chǎn)dmin_init’, ‘_maybe_update_plugins’);
remove_action(‘a(chǎn)dmin_init’, ‘_maybe_update_themes’);

wordpress函數(shù)應用案例13:
自動為網(wǎng)站圖片添加alt標簽(相關(guān)知識:什么是alt標簽):

<div>function image_alt_tag($content){</div><div> global $post;preg_match_all('/<img (.*?)\/>/', $content, $images);</div><div> if(!is_null($images)) {</div><div> foreach($images[1] as $index => $value){</div><div> if(!preg_match('/alt=/', $value)){</div><div> $new_img = str_replace('<img', '<img alt="'.get_the_title().'"', $images[0][$index]);</div><div> $content = str_replace($images[0][$index], $new_img, $content);}</div><div> }</div><div> }</div><div> return $content;</div><div>}</div><div>add_filter('the_content', 'image_alt_tag', 99999);</div>

wordpress函數(shù)應用案例14:
獲取文章的評論人數(shù):

function get_number_of_participants($post_id=0){
$comments_authors = array();
$comments = get_comments('post_id='.$post_id);
foreach($comments as $comment) :
array_push($comments_authors, $comment->comment_author);
endforeach;

//remove duplicates
$unique_comment_authors = array_unique($comments_authors);

return count($unique_comment_authors);
}

更多wordpress常用代碼,請參考wordpress程序開發(fā)手冊。

標簽: 代碼 開發(fā)者 網(wǎng)站制作

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:WordPress禁用gravatar 頭像本地化加速

下一篇:WordPress父分類調(diào)用子分類名稱和文章列表