wordpress如何自动给图片加ALT信息、标签云等函数应用汇总

2018-11-02    来源:学做网站论坛

容器云强势上线!快速搭建集群,上万Linux镜像随意使用

我们在学做网站时,使用wordpress程序,它的主题除了index.php、header.php、footer.php等文件之外,还有一个强大的函数文件functions.php,这个文件主要是为wordpress主题开发者提供的一个自定义模板文件,可以设置菜单、小工具等等很多功能效果。

wordpress如何自动给图片加ALT信息、标签云等函数应用汇总

但是在网站制作时,例 如做北奔重卡汽车公司网站时,修改这个文件需要你有一定的php基础才可以自由编辑,如果修改不正确,严重的会直接导致网站打不开,提示错误,所以修改的时候不能马虎大意。

虽然不懂的php语言,但是如果你足够细心,也能够借助别人已经写好的,或是wordpress官方提供的一些函数文件实现我们需要的效果,这个帖子就是为大家征集这些常用的函数应用的,如果你在学习当中遇到过一些应用性比较强的函数代码,供学习做网站的学员们在网站制作时使用。

以下案例应用所有的函数代码需放置在函数文件functions.php文件中,如果没有此文件,可以直接创建一个文件,并放置以下代码:
<?php
//函数代码放置区
?>
wordpress函数应用案例一:
菜单函数注册代码:

//自定义菜单
register_nav_menus(
array(
'header-menu' => __( '导航自定义菜单' ),
)
);

调用代码:

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

wordpress函数应用案例二:
启用小工具函数代码:

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

调用代码:

<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
//可以放置在侧边栏的最上和最下
<?php endif; ?>

wordpress函数应用案例三:
扩展wordpress后台编辑器功能按钮函数:

//为编辑器增加按钮
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");

调用方式:无需调用。

wordpress函数应用案例四:
添加wordpress外观背景功能函数:

//背景
add_custom_background();

调用方式,修改<body>标签为以下代码:

<body class="custom-background">

wordpress函数应用案例五:
去除多余头部版权信息函数:

//去除头部多余信息
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');

调用方式:无需调用。

wordpress函数应用案例六:
禁止代码标点转换函数:

//禁止代码标点转换
remove_filter('the_content', 'wptexturize');

调用方式:无需调用。

wordpress函数应用案例七:
自动版权时间函数:

//自动生成版权时间
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;
}

调用方式:在footer.php合适位置添加以下代码:

<?php echo comicpress_copyright(); ?>

wordpress函数应用案例八:
评论支持贴图函数:

//评论贴图
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');

调用方式:在评论文件comments.php中合适位置添加以下代码:

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

wordpress函数应用案例九:
移除wordpress版本提示函数:

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

调用方式:无需调用。

wordpress函数应用案例十:
彩色标签云效果函数:

//彩色标签云
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>";
}

调用方式:无需调用。主题中有标签调用函数即可。

wordpress函数应用案例十一:
阅读全文添加nofollow标签函数:

//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);

调用方式:无需调用,文章添加阅读更多自然生成。

wordpress函数应用案例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(‘admin_init’, ‘_maybe_update_core’);
remove_action(‘admin_init’, ‘_maybe_update_plugins’);
remove_action(‘admin_init’, ‘_maybe_update_themes’);

wordpress函数应用案例13:
自动为网站图片添加alt标签(相关知识:什么是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函数应用案例14:
获取文章的评论人数:

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程序开发手册。

标签: 代码 开发者 网站制作

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。

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

下一篇:WordPress父分类调用子分类名称和文章列表