10个足以取代插件的WordPress技巧

在Wordpress中越多的插件有时候意味着越慢的载入速度,而且我们往往只需要某些插件其中的某个功能,而不是全部。
所以我也建议在可以自己修改主题文件完成的功能,不要使用插件.下面10个技巧就是我从国外一些网站搜集而来的,如果你觉得需要就尽管拿去用吧,不用请千万要在修改之前备份自己的文件哦,呵呵。

1.轻松取得自定义字段(Custom field)的值

现在很多wordpress主题通过自定义字段(Custom field)来获取图片地址来生成文章的缩略图,那么最简单的方法是什么呢?
首先打开你的主题目录下的function.php,并把以下代码复制进去

function get_custom_field_value($szKey, $bPrint = false) {
	global $post;
	$szValue = get_post_meta($post->ID, $szKey, true);
	if ( $bPrint == false ) return $szValue; else echo $szValue;
}

接着就可以在index.php或其他模块文件中用以下代码,取得自定义字段的值了

<?php if ( function_exists('get_custom_field_value') ){
        get_custom_field_value('featured_image', true);
} ?>

2.显示不带链接的目录(Category)名称

由于常用的the_category()函数生成的目录(Category)是带有链接的,那假如我们只想要显示目录名称,但是不带有链接,例如本站就是利用这个方法生成目录名称作为每个目录的Class名.

<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>

上面的代码是 $category数组的第一个值

3.在边栏随机显示一篇文章

只要把以下代码复制进sidebar.php,搞定

<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
     while (have_posts()) : the_post();
         the_title();
         the_excerpt();
     endwhile;
endif; ?>

4.把网址中的/category去掉

默认情况下wordpress都会在自动在目录网址中加入/category/,
类似

http://www.ediyang.com/category/wordpress

假如你不喜欢这样,那就用下面的代码试试吧
打开你wordpress所在博客根目录的.htaccess文件,然后把下面代码加进去

RewriteRule ^category/(.+)$ http://www.yourblog.com/$1 [R=301,L]

结果显示如下

http://www.ediyang.com/wordpress

5.在RSS中加入广告

在function.php中加入以下代码

function insertRss($content) {
    if(is_feed()){
        $content = 'text before content'.$content.'<hr /><a href="http://www.ediyang.com">Did you visited ediyang today?</a><hr />';
    }
    return $content;
}
add_filter('the_content', 'insertRss');

请把部分替换成你的广告代码即可

6.如何禁止搜索引擎收录某个目录的文章

打开header.php文件并在标签前加入如下代码

<?php if ( is_category('8') || in_category('8') ) {
    echo '<meta name="robots" content="noindex">';
}

请把代码中的8修改成你要屏蔽搜索引擎的目录的ID

7.显示feedburner的订阅数量

把如下代码加入到你想要订阅数量显示出来的地方,例如边栏 sidebar.php

<?php
$fburl=”https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=YourURL“;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $fburl);
$stored = curl_exec($ch);
curl_close($ch);
$grid = new SimpleXMLElement($stored);
$rsscount = $grid->feed->entry['circulation'];
echo $rsscount;
?>

记得把$fburl= 后面的网址替换成你的feedburner地址

8.如何特殊化某篇文章的样式

首先你要在single.php找到你显示文章的div
例如

<div class="post">

然后把他修改成

<div class="post" id="post-<?php the_ID(); ?>">

这样修改后每篇文章就会有一个唯一的class(就是文章ID),所以我们可以打开style.css添加一下样式

#post-112 {
    background: #113355;
    color:#069;
    font-weight:bold;
}

记得把112修改成你那篇文章的ID

9.自动显示文章最后一张图片

打开function.php加入以下代码

function sc_postimage($atts, $content = null) {
	extract(shortcode_atts(array(
		"size" => 'thumbnail',
		"float" => 'none'
	), $atts));
	$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() );
	foreach( $images as $imageID => $imagePost )
	$fullimage = wp_get_attachment_image($imageID, $size, false);
	$imagedata = wp_get_attachment_image_src($imageID, $size, false);
	$width = ($imagedata[1]+2);
	$height = ($imagedata[2]+2);
	return '<div class="postimage" style="width: '.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.'</div>';
}
add_shortcode("postimage", "sc_postimage");

这段代码生成了一个shortcode短代码,接下来你可以在任何地方使用

[postimage]

就可以调用这个短代码生成图片

10.显示评论数最多的文章缩略图

把下面的代码加入到你想要缩略图显示的地方,例如Index.php或是sidebar.php等模块文件中

<ul>
<?php
//The name of custom field you'd like to get, don't forget to change it
$custom_field_name = "featuredimg";

//Cool sql request
$sql = "SELECT p.comment_count, p.ID, m.post_id, m.meta_key, m.meta_value
FROM wp_posts p, wp_postmeta m
WHERE p.ID = m.post_id
LIMIT 0,10;";

$result = $wpdb->get_results($sql);

foreach ($result as $topten) {
    $postid = $topten->ID;
    $title = $topten->post_title;
    $commentcount = $topten->comment_count;
    if ( $topten->meta_key == $custom_field_name) {
    $img = '<img src="'.$topten->meta_value.'" alt=""/>';
    ?>
         <li><a href="<?php echo get_permalink($postid); ?>"><?php echo $img; ?></a></li>
    <?php }
}
?>
</ul>

这段代码会生成10篇最多人评论的文章的缩略图,如果你想改成5篇,就把0,10改成0,5即可
注意:缩略图需要你在文章的自定义字段中定义过才行

转载请注明来自eddie yang’s creative

原创文章,作者:网贝WebBay,如若转载,请注明出处:https://www.webbay.cn/10-useful-wordpress-function-code-to-replace-plugin

发表回复

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

评论列表(2条)

error: Content is protected !!