站长资源
中国站长网站

wordpress创建自定义文章类型及在模板中调用

什么是自定义文章类型?为什么要添加自定义文章类型?

我们可以看到wordpress的后台只有一个文章和一个页面选项,文章类型可以添加文章,编辑分类,页面类型就是直接创建一个页面,比较适合单页面像关于我们联系我们这些,而我们一般网站的需求是有产品、案例、客户、团队等等这些一系列板块,如果统一都放入文章里面显的既不专业也很杂乱,于是我们就需要用到自定义类型,即在后台添加一个产品、案例等等这样的直接在后台左侧导航上独立显示的分类。网上大部分的教程都比较零散没有系统的告诉大家要怎样完整的快速的制作出一个自定义类型,今天闲来无事咱就一次性把这事给说了。

创建一个新的自定义文章类型需要使用register_post_type函数来注册,在主题的 functions.php 文件下调用该函数:

register_post_type( $post_type, $args );

$post_type参数就是自定义文章类型的名称;$args参数用于自定义文章类型的功能。大体结构如下:

function my_custom_post_product() {
    $args = array();
    register_post_type( 'product', $args ); 
}
add_action( 'init', 'my_custom_post_product' );

创建一个函数,把args参数定义为一个数组(因为里面有很多参数可选,具体参数请查看官方文档),然后挂靠到 init 这个 action 上。这样 WordPress 在初始化的时候,就会执行这个函数注册一个自定义文章类型,因为调用register_post_type()的时候,必须要在 admin_menu action 之前,在 after_setup_theme action 之后,所以这里最好挂靠到 init action 上。

具体实例:

function my_custom_post_product() {
  $labels = array(
    'name'               => _x( 'products', 'post type 名称' ),
    'singular_name'      => _x( 'product', 'post type 单个 item 时的名称,因为英文有复数' ),
    'add_new'            => _x( '增加产品', '添加新内容的链接名称' ),
    'add_new_item'       => __( '增加一个产品' ),
    'edit_item'          => __( '编辑产品' ),
    'new_item'           => __( '新产品' ),
    'all_items'          => __( '所有产品' ),
    'view_item'          => __( '查看产品' ),
    'search_items'       => __( '搜索产品' ),
    'not_found'          => __( '没有找到有关产品' ),
    'not_found_in_trash' => __( '回收站里面没有相关产品' ),
    'parent_item_colon'  => '',
    'menu_name'          => '产品中心'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => '我们网站的产品信息',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true
  );
  register_post_type( 'product', $args );
}
add_action( 'init', 'my_custom_post_product' );

讲上面的代码复制到你模板的function.php文件中,再刷新你的后台,你会发现多处一个产品中心选项,如图:

大家可能添加之后发现,我的图上有产品分类,而你的并没有,这是因为添加自定义分类我们要用到另一个函数register_taxonomy( $taxonomy, $object_type, $args );

$taxonomy 字符串型,必需,自定义分类的名称,一般和上文中的name参数保持一致即可;

$object_type数组或字符串,必需,即上文的register_post_type( ‘product’, $args );中的product

$args配置参数,可选,跟register_post_type函数的$args参数类似

代码如下:

//		增加产品分类
function my_taxonomies_product() {
	$labels = array(
		'name'              => _x( '产品分类', 'taxonomy 名称' ),
		'singular_name'     => _x( '产品分类', 'taxonomy 单数名称' ),
		'search_items'      => __( '搜索产品分类' ),
		'all_items'         => __( '所有产品分类' ),
		'parent_item'       => __( '该产品分类的上级分类' ),
		'parent_item_colon' => __( '该产品分类的上级分类:' ),
		'edit_item'         => __( '编辑产品分类' ),
		'update_item'       => __( '更新产品分类' ),
		'add_new_item'      => __( '添加新的产品分类' ),
		'new_item_name'     => __( '新产品分类' ),
		'menu_name'         => __( '产品分类' ),
	);
	$args = array(
		'labels' => $labels,
		'public'            => true,
		'show_in_nav_menus' => true,
		'hierarchical' => true, //控制自定义分类法的格式,如果值是false,则将分类(category)转化成标签(tags)
		'show_ui'           => true,
		'query_var'         => true,
        'rewrite'           => true,
        'show_admin_column' => true
	);
	register_taxonomy( 'products', 'product', $args );//products是该自定义分类法的名称;product是对应的自定义文章类型名称
}
add_action( 'init', 'my_taxonomies_product', 0 );

我们再到后台刷新看下,是不是有了产品分类,OK,至此我们自定义文章类型已经完成,接下来我们来看在模板中如何调用

列表页如何调用

我们新建一个archive-product.php 或者taxonomy-product.php 只要你对wordpress的模板加载顺序了解,很容易明白为什么创建这样的文件, 代码如下:

<?php 
    $posts = get_posts(array(                           
        'numberposts' => '10', //输出的文章数量
        'post_type' => 'product',   //自定义文章类型名称     
        'tax_query'=>array(
            array(
                'taxonomy'=>'产品中心', //自定义分类法名称
                'terms'=>'2' //id为2的分类。也可是多个分类array(2,3)
            )
        ),
    )
    );
?>
<ul>
<?php if($posts): foreach($posts as $post): ?>          
    <li><a href="<?php%20the_permalink();%20?>" target="_blank" title="<?php the_title();?>"><?php the_title();?></a></li>
<?php wp_reset_postdata(); endforeach; endif;?>
</ul>

这面的ID改为你自己的分类ID

内容页如何调用

内容页的调用方法还是和正常的一样,很多种方式,随便你选择,我们新建一个single-product.php代码如下:

<?php echo $post->post_content;?>

到这里我们就完成了自定义类型的制作及模板输出,有什么问题可以留言和我讨论

注:上面的添加产品,肯定是需要特色图片来充当缩略图功能,如果你没有,请在function.php文件中添加如下代码来开启:

//开启特色图像支持
if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' );
}

另外说些题外话,其实很多插件都可以实现帮助我们分类,给大家推荐一款我一直用的pods插件,功能还是很强大的,不懂代码的人只要学会怎么用,也可以制作出很多自定义的扩展出来,抽空也给大家介绍一下

本文出处:来自互联网信息共享,请勿相信收费信息站长资源 » wordpress创建自定义文章类型及在模板中调用

评论 抢沙发

评论前必须登录!