wordpress模板制作代码段(二)

wordpress调用随机文章:

<?php$rand_posts = get_posts('numberposts=10&orderby=rand');
foreach( $rand_posts as $post ) :?>
<!--下面是你想自定义的Loop-->
<li>
	<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>

wordpress调用最新文章:

<?php get_archives('postbypost', 10); ?> (显示10篇最新更新文章)或者
<?php wp_get_archives(‘type=postbypost&limit=20&format=custom’); ?>
后面这个代码显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式。具体的参数和使用方法你可以参考官方的使用说明- wp_get_archvies。(fromat=custom也可以不要,默认以UL列表显示文章标题。)

显示最新文章:

<div id="newpost">
<h2> 最新文章</h2>
<?php
$previous_posts = get_posts('numberposts=15');
foreach($previous_posts as $post) :
setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php
the_title(); ?></a></li>
<?php /*?><?php the_content(); ?><?php */?>
<?php endforeach; ?>
</div>
<div id="newcomment">

相关文章代码:

<div id="newpost">
<ul>
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>10,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to
<?php the_title_attribute(); ?>"><?php the_title(); ?>
</a> </li>
<?php endwhile; } else {echo 'not realate post';} }else {echo 'not
realate post';} ?>

wordpress调用网站统计大全:

1、日志总数:
<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?>

2、草稿数目: <?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?> 

3、评论总数: 
<?php echo $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");?> 

4、成立时间: 
<?php echo floor((time()-strtotime("2008-8-18"))/86400); ?> 

5、标签总数: 
<?php echo $count_tags = wp_count_terms('post_tag'); ?> 

6、页面总数: 
<?php $count_pages = wp_count_posts('page'); echo $page_posts = $count_pages->publish; ?> 

7、分类总数: 
<?php echo $count_categories = wp_count_terms('category'); ?> 

8、链接总数: 
<?php $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); echo $link; ?> 

9、用户总数: 
<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users; ?> 

10、最后更新: 
<?php $last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");$last = date('Y-n-j', strtotime($last[0]->MAX_m));echo $last;

安全的调用Wordpress插件模板代码:

<?php if ( function_exists(’plugin_template_tag_function’) ) : ?>
<h2>部件标题</h2>
<?php plugin_template_tag_function(); ?>
<?php endif; ?>

改变WordPress后台控制面板底部信息:

如果不想让客户知道网站是由WordPress制作,则可以修改WordPress控制面板底部的信息,只需要把下面的代码添加到 functions.php文件即可。

function remove_footer_admin () {
echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | Designed by <a href="http://www.aaaaaaaaaa.net" target="_blank">Uzzz Productions</a> | WordPress Tutorials: <a href="http://www.wpbeginner.com" target="_blank">WPBeginner</a></p>';
}
add_filter('admin_footer_text', 'remove_footer_admin');

改变每个分类页面的日志显示数:

改变每个分类页面的日志显示数. (成功)
你将需要打开category.php 文件并且找到下面的这行,
在<?php if (have_posts()) : ?>
下面添加内容:
<?php if (is_category('1')) {$posts = query_posts ($query_string.'&showposts=1'); } ?>
<?php if (is_category('16')) {$posts = query_posts($query_string.'&showposts=2'); } ?>
“is_category()”就是分类ID,比如可以写成”is_category(’5)”, “showposts=4 里面的数值就
是你要显示的分类日志数。

改变默认的 Gravatar 头像:

WordPress程序默认的 Gravatar 头像很不咋地,而且到处都是千篇一律的默认头像一点都无法体现独特性。你可以把以下代码添加到functions.php文件里面,然后记得把自定义的Gravatar 头像上传到WordPress模板的images文件夹。
add_filter( 'avatar_defaults', 'newgravatar' );
function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo('template_directory') . '/images/gravatar.gif';
$avatar_defaults[$myavatar] = "WPBeginner";
return $avatar_defaults;
}

给WordPress控制面板添加自定义logo:

用WordPress给客户制作网站,如果给WordPress的控制面板后台添加一个自定义logo,则会让网站显的专业很多。要做到这一点,你只需要把代码添加到functions.php即可。
//hook the administrative header output
add_action('admin_head', 'my_custom_logo');
function my_custom_logo() {
echo '
<style type="text/css">
#header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
</style>
';
}
注:代码里面的html部分可以修改。

给读者投稿文章添加姓名,来源:

如果你的博客接受读者的投稿,想在该篇文章出现投稿者的姓名,同时又不想通过添加作者的这种繁琐而麻烦的方式来操作,则可以使用下面的代码。使用下面的代码后,只需要在撰写文章的时候在自定义区域填上投稿者的姓名即可。系统会自动将发布者的名称换成投稿者的名称。
这个代码对接受读者投稿较多的网站,或者是资讯型的网站非常有用(利用它来显示来源)。
add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
function guest_author_name( $name ) {
global $post;
$author = get_post_meta( $post->ID, 'guest-author', true );
if ( $author )
$name = $author;
return $name;
}

禁用wordpress定时任务:

WP-Cron的原理是这样的:虽然用户不能通过crond设置计划任务,但网站随时可能会有用户在点击,只要在用户点击的时候,判断当时是否有需要运行的后台任务,如果则在后台运行该任务,就可以实现类似crond的效果。虽然这么好用,不过占用这么高的系统资源我就不得不禁用它了。 

禁用WP-Cron很简单,编辑wp-config.php,加入一下一行: 
define('DISABLE_WP_CRON', true);

开启WordPress评论嵌套功能:

嵌套功能是WordPress自身带有的最好功能之一,只可惜很多WordPress模板都不支持。很多文章都有提到过修改的方法,但一般都涉及到修改comments文件和header文件。事实上,通过修改functions.php文件来修改是最简便的,而且一劳永逸。
// enable threaded comments 

function enable_threaded_comments(){ 
if (!is_admin()) 
{ 
if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1)) 
wp_enqueue_script('comment-reply');
} 
} 
add_action('get_header', 'enable_threaded_comments');

启用文章缩略图功能:

从WordPress2.9版本开始,可以给模板添加文章缩略图功能。操作方法很简单,只需要把下面的代码添加到functions.php里面。
add_theme_support( 'post-thumbnails' );
然后在要显示缩略图的地方放置下面的代码即可。
<?php the_post_thumbnail(); ?>

取消RSS输出:

对于某些博客而言,或者因为被太多人采集了,或者因为不想让别人通过RSS订阅,想取消RSS输出。WordPress默认是没有这个功能的,但你可以通过以下的代码来取消RSS输出。
function fb_disable_feed() 
{ wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') ); 
} 
add_action('do_feed', 'fb_disable_feed', 1); 
add_action('do_feed_rdf', 'fb_disable_feed', 1); 
add_action('do_feed_rss', 'fb_disable_feed', 1); 
add_action('do_feed_rss2', 'fb_disable_feed', 1); 
add_action('do_feed_atom', 'fb_disable_feed', 1);

添加Google Analytics 统计:

只需要把下面的代码添加到functions. php文件里面——注意把里面的中文部分替换成你的Google 统计代码,然后你就不用担心了。
<?php
add_action('wp_footer', 'add_googleanalytics');
function add_googleanalytics() { ?>
// 把Google 统计代码复制到这里
<?php } ?>

为特定分类文章创建不同外观:

假设你有两个分类,分别名为"News"和"Tutorials",然后你想让"News"分类里的单篇文章的
外观表层为style1.css,让"Tutorials"分类里的单篇文章外观表层为style2.css,Lorelle 提供
了以下这种比较简单的解决方法:
打开single.php 文件,并删除里面的所有代码,添加以下代码:
<?php
$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>
通常情况下,PHP会自动查询这段代码,如果日志的分类ID 为9,则会自动显示single2.php,
如果不是则会显示为single1.php。
注意:single1.php 和single2.php 里的样式自定义,系统会自动判断加载不同的样式显示

样式化不同分类:

如果你想给特定分类规定不同的样式表,你所需做的只是在header.php 文件的标签中加入
以下代码:
<?php if ( is_category('15') ) { ?>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/cat-15.css"
type="text/css" media="screen" />;
<?php } else { ?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css"
media="screen" />
<?php } ?>

友情链接:

在模版文件functions.php下添加如下代码开启友情链接:

add_filter('pre_option_link_manager_enabled','__return_true');

在WordPress 的第一篇文章中插入Google 广告:

在WordPress 的第一篇文章中插入Google 广告(成功)

<?php while ( have_posts() ): the_post(); $count++;?>
<?php if ($count == 1) : ?>
// Insert your Google AdSense code here
<?php endif; ?>
<?php endwhile; ?>

你同样可以改变count 值来让广告比如放在不同的日志后面,比如改成count == 2, 则把
Google 广告显示在第二篇日志后面.

在某一个页面单独调用某一个php 文件:

在某一个页面单独调用某一个php 文件;
如果你仅想在首页显示某个文件内容,可在index.php 文件中写入以下代码:
<div> <?php if ( is_home() ) { include (TEMPLATEPATH.'/test.php'); } ?> </div>
<div> <?php if ( is_category(1) ) { include (TEMPLATEPATH.'/test.php'); } ?> </div>
页面显示特定内容:(取名字到is_page(‘* ’))
<div> <?php if( is_page('contact')) { include (TEMPLATEPATH.'/test.php'); } ?> </div>//判
断是不contact 页面
/// is_posted()

在文章页显示相关文章:

<?php $tags = wp_get_post_tags($post->ID); 
if ($tags) { 
$first_tag = $tags[0]->term_id; 
$args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>10, 'caller_get_posts'=>1 ); 
$my_query = new WP_Query($args); 
if( $my_query->have_posts() ) { 
while ($my_query->have_posts()) : $my_query->the_post(); 
?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); 
?>">
<?php the_title();?> 
<?php comments_number(' ','(1)','(%)'); ?></a></li> 
<?php endwhile; 
} 
} 
wp_reset_query();
?>

自定义摘要输出的文字长度:

假如你比较懒,不想在撰写文章的时候每篇文章都输入摘要,就可以让系统自动截取一定长度的文字来作为摘要输出。下面的代码默认是100个字节,也就是50个汉字。你可以把数值修改成符合你需要的数字。
function new_excerpt_length($length) { return 100; } add_filter('excerpt_length', 'new_excerpt_length');

自定义摘要输出时的符号:

一般设定自动摘要输出,你会经常在WordPress博客的首页看到“[。..]”这样的符号。为了界面的美观,或者是个性化的需要,你可以把这个默认的符号改变为其他的符号。而以下的代码就是为了实现这个而写:
// custom excerpt ellipses for 2.9 
function custom_excerpt_more($more) 
{ return '…'; 
} 
add_filter('excerpt_more', 'custom_excerpt_more'); 

/* custom excerpt ellipses for 2.8

function custom_excerpt_more($excerpt) 
{ 
return str_replace('[...]', '…', $excerpt); } 
add_filter('wp_trim_excerpt', 'custom_excerpt_more'); 

*/