wordpress主题支持内置文章形式的方法

WordPress教程 4413

文章形式是wordpress内置的功能,提供包括日志、图像、视频等多种文章格式,博主发布文章的时候可以根据文章的类型,在编辑页面的右侧选择对应的文章形式。这个功能让博主划分不同类型文章的同时便于博主给不同形式的文章单独设计显示形式,而且不受分类限制。

wordpress主题支持内置文章形式的方法

给主题添加文章形式功能:

要使用wordpress文章形式,需要让当前使用的主题支持文章形式,在主题的functions.php文件,添加以下代码:

1
add_theme_support( 'post-formats', array( 'aside', 'chat','gallery','image','link', 'quote', 'status', 'video', 'audio' ) );

参数说明:

  • aside:日志,不显示标题的标准文章
  • image:图像,单张图像。文章中的首个<img />标记将会被认为是该图片。
  • video:视频,单一视频。
  • quote:引语,引用他人的一段话。
  • link:链接,链接到其它网站的链接。
  • gallery:相册,图像陈列厅。
  • status:状态,简短更新,通常最多 140 个字符。类似于微博
  • audio:音频,一个音频文件
  • chat:聊天,聊天记录
不同形式文章调用不同文章模板

默认情况下,wordpress文章统一调用single.php模板,那么怎么给不同的文章格式调用不同的文章模板?

方法一:

1、把当前主题的single.php文件命名为content.php,然后重新创建一个single.php文件,添加以下代码:

1
<?php get_template_part( 'content', get_post_format() ); ?>

2、把各个文章形式要调用的模板命名为content-{post-format}.php格式,如

  • Standard:content.php
  • Aside:content-aside.php
  • Link:content-link.php
  • Image:content-image.php
  • Quote:content-quote.php
  • Status:content-status.php
  • Video:content-video.php
  • Audio:content-audio.php
  • Chat:content-chat.php

方法二:

1、在主题的functions.php文件添加以下代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
add_action('template_include', 'load_single_template');   
function load_single_template($template) {   
  $new_template = '';   
  if( is_single() ) {   
    global $post;  
    if ( has_post_format( 'aside' )) { 
		$new_template = locate_template(array('single-aside.php' ));   
    }elseif(has_post_format( 'link' )){
		$new_template = locate_template(array('single-link.php' ));  
	} elseif(has_post_format( 'image' )){
		$new_template = locate_template(array('single-image.php' ));  
	} elseif(has_post_format( 'quote' )){
		$new_template = locate_template(array('single-quote.php' ));  
	} elseif(has_post_format( 'status' )){
		$new_template = locate_template(array('single-status.php' ));  
	} elseif(has_post_format( 'video' )){
		$new_template = locate_template(array('single-video.php' ));  
	} elseif(has_post_format( 'audio' )){
		$new_template = locate_template(array('single-audio.php' ));  
	} elseif(has_post_format( 'chat' )){
		$new_template = locate_template(array('single-chat.php' ));  
	} else{
		$new_template = locate_template(array('single.php' ));  
	}
 
  }   
  return ('' != $new_template) ? $new_template : $template;   
}

2、创建以下php文件:

  • Standard:single.php
  • Aside:single-aside.php
  • Link:single-link.php
  • Image:single-image.php
  • Quote:single-quote.php
  • Status:single-status.php
  • Video:single-video.php
  • Audio:single-audio.php
  • Chat:single-chat.php
文章形式判断代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php if( has_post_format( 'status' )) { //状态 ?>
状态样式
<?php } else if ( has_post_format( 'aside' )) { //日志 ?>
日志样式
<?php } else if ( has_post_format( 'gallery' )) { //相册 ?>
相册样式
<?php } else if ( has_post_format( 'video' )) { //视频 ?>
视频样式
<?php } else if ( has_post_format( 'audio' )) { //音乐 ?>
音乐样式
//....
<?php } else{ //标准 ?>
常规样式
<?php } ?>
扩展:让页面和自定义文章类型支持文章形式

在主题的functions.php文件添加以下代码,把my_custom_post_type改为自定义文章类型名称:

1
2
add_post_type_support( 'page', 'post-formats' );   
add_post_type_support( 'my_custom_post_type', 'post-formats' );

最后可以根据自己的需要设计不同的模板了!

精品推荐: