修改当前文章

[php]
<?php edit_post_link(__('修改?','inove'),'<span class="editpost">','</span>'); ?>
[/php]

 

分页导航

[php]
<?php par_pagenavi(9); ?>
[/php]

 

文章日期

[php]
<?php the_time('Y.m.d'); ?>
[/php]

 

浏览次数

[php]
<?php post_views(' ', ' 次'); ?>
[/php]

 

作者显示

[php]
<?php the_author(); ?>
[/php]

 

文章链接

[php]
<?php the_permalink() ?>
[/php]

 

文章标题

[php]
<?php the_title(); ?>
[/php]

 

single输出内容

[php]

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile;?>
<?php endif; ?>

[/php]

 

访问次数统计

[php]
<?php post_views(' ', ' 次'); ?>
[/php]

 

在functions.php加入下面代码

[php]

function record_visitors()
{
if (is_singular())
{
global $post;
$post_ID = $post->ID;
if($post_ID)
{
$post_views = (int)get_post_meta($post_ID, 'views', true);
if(!update_post_meta($post_ID, 'views', ($post_views+1)))
{
add_post_meta($post_ID, 'views', 1, true);
}
}
}
}
add_action('wp_head', 'record_visitors');

/// 函数名称:post_views
/// 函数作用:取得文章的阅读次数
function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)
{
global $post;
$post_ID = $post->ID;
$views = (int)get_post_meta($post_ID, 'views', true);
if ($echo) echo $before, number_format($views), $after;
else return $views;
}

[/php]

 

如何调用不同single 方法二

首先在主题的根目录添加一个自定义的模版文件如single-test.php(名字随意),然后将以下代码相应修改后加到主题函数模版functions.php的最后一个?>前。当访问指定分类的文章时,就会使用自定义的模版。

[php]

add_action('template_include', 'load_single_template');
function load_single_template($template) {
$new_template = '';
if( is_single() ) {
global $post;
// 'exiang' 改为分类ID或别名
if( has_term('exiang', 'category', $post) ) {
// single-test.php 改为自定义的模版
$new_template = locate_template(array('single-test.php' ));
}
}
return ('' != $new_template) ? $new_template : $template;
}

[/php]

 

上面提到了通过single-{post_type}.php给指定的文章类型定义文章页模版,具体如下:

single-{post_type}.php – 如果文章类型是videos(即视频),WordPress就会去查找single-videos.php(WordPress 3.0及以上版本支持)为指定分类定义分类页模版:category-{slug}.php – 如果分类的别名为news,WordPress将会查找category-news.php(WordPress 2.9及以上版本支持)category-{id}.php -如果分类ID为6,WordPress将会查找category-6.php
先在single.php中加入下列内容

[php]

<?php if ( in_category('1') || post_is_in_descendant_category(1) ) {include(TEMPLATEPATH .'/single-media.php');} elseif( in_category('2') || post_is_in_descendant_category( 2 ) ) {include(TEMPLATEPATH . '/single-media.php');} else{include(TEMPLATEPATH . '/single-txt.php');} ?>

[/php]

 

然后在functions.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]

 

另一种方式
方法二:使用 in_category() 进行判断in_category() 函数可以通过分类别名或ID判断当前文章所属的分类,而且可以直接在循环(Loop)内部和外部使用。
如果是单个分类 ID ,比如ID 为 2 ,可以这样写

[php]
in_category(2)
[/php]

 

如果是单个分类别名,比如别名为 themes,可以这样写

[php]
in_category('themes')
[/php]

 

如果是多个ID,可以这样写

[php]
in_category( array( 2,3,7) )
[/php]

 

如果是多个别名,可以这样写

[php]
in_category( array( 'themes','plugins','develop') )
[/php]

 

然后我们结合 if 语句就可以很好地实现模板的选择。

比如我们可以在主题的根目录创建3个文章模板文件,分别命名为 single001.php , single002.php 和 single003.php,然后我们希望 ID 为 2 和 3 的分类使用 single001.php,ID为 7 的分类使用 single002.php ,其他分类使用 single003.php,那么,我们可以在 single.php 文件写入下面的代码:

[php]
<?php if ( in_category(array( 2,3 )) ) { get_template_part('single001' ); } elseif ( in_category( 7 )) { get_template_part('single002' ); } else { get_template_part('single003' ); } ?>
[/php]

 

上一篇下一篇
显示同分类下的上一篇、下一篇链接方法

[php]

<?php if (get_previous_post()) { previous_post_link('上一篇: %link','%title',true);} else { echo "没有了,已经是最后文章";} ?>
<?php if (get_next_post()) { next_post_link('上一篇: %link','%title',true);} else { echo "没有了,已经是最新文章";} ?>

[/php]

 

一般的添加上一篇、下一篇链接方法

[php]

<?php if (get_previous_post()) { previous_post_link('上一篇: %link');} else {echo "没有了,已经是最后文章";} ?>
<?php if (get_next_post()) { next_post_link('下一篇: %link');} else {echo "没有了,已经是最新文章";} ?>

[/php]

 

特殊调用,使用箭头等

[php]

<?php previous_post('%','« Previous', 'no'); ?>
<?php previous_post('%','<span class="glyphicon glyphicon-menu-left"></span>', 'no'); ?>
<?php next_post('%','<span class="glyphicon glyphicon-menu-right"></span>', 'no'); ?>

[/php]

 

wordpress每个页面下面调用不同的sidebar或者其他模块的方法

wordpress的默认博客模板是比较单一的,每个页面的sidebar都是一样的,如果要个性化的就需要用到不同的模块,那要如何实现呢?在页面中要显示不同模块的地方,可以用下面的代码:

[php]

<?php$post = $wp_query->post;if ( in_category('9') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>

[/php]

 

代码解读:在if ( in_category(‘9′) )中进行判断,如果是分类目录id号9的话,页面中就调用single2.php模块,如果是不是的话,就调用single1.php。这里只有两种情况的判断,如果分类很多要怎么办?就是添加判断语句elseif,代码如下:

[php]

<?php$post = $wp_query->post;if ( in_category('9') ) {
include(TEMPLATEPATH . '/single2.php');
} elseif( in_category('1')) {
include(TEMPLATEPATH . '/single1.php');
}elseif( in_category('2')) {
include(TEMPLATEPATH . '/single3.php');
}else {
include(TEMPLATEPATH . '/single4.php');
}
?>

[/php]

 

通过elseif的判断可以添加很多的情况.

在移动设备端,因为屏幕都比较小,如果要让图片自适应屏幕,我们应当把width和height属性去除,不然图片可能会比屏幕大:

为了达到此目的,在当前主题的functions.php中加入以下PHP代码即可:

[php]

// 自适应图片删除width和height,by Ludou
function ludou_remove_width_height_attribute($content){
preg_match_all('/<[img|IMG].*?src=[\'|"](.*?(?:[\.gif|\.jpg|\.png\.bmp]))[\'|"].*?[\/]?>/', $content, $images);
if(!empty($images)) {
foreach($images[0] as $index => $value){
$new_img = preg_replace('/(width|height)="\d*"\s/', "", $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
return $content;
}

// 判断是否是移动设备浏览
if(wp_is_mobile()) {
// 删除文章内容中img的width和height属性
add_filter('the_content', 'ludou_remove_width_height_attribute', 99);
}

[/php]

 

或者,使用另一种方法(来自于江水)【对 img 设置 max-width: 100%; height: auto; 这两条属性,才可以保证成比例拉伸。】

 

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。