65.9K
CodeProject 正在变化。 阅读更多。
Home

创建自定义文章类型的操作指南

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1投票)

2016年2月12日

CPOL

4分钟阅读

viewsIcon

9887

对于过去6年里没有与世隔绝的人来说,我们知道自从WordPress 3.0发布以来,我们现在可以自由创建自己的文章类型。 基本上,您可以通过自定义文章类型设置一个标准的“格式”来显示特定“类型”的内容。 本文将指导您逐步操作 -

对于过去6年里没有与世隔绝的人来说,我们知道自从WordPress 3.0发布以来,我们现在可以自由创建自己的文章类型。 基本上,您可以通过自定义文章类型设置一个标准的“格式”来显示特定“类型”的内容。

本文将逐步指导您完成该过程。 但首先,让我们注意一些事项,以复习一下我们的WordPress知识。

默认情况下,WordPress上有五种 文章类型 
  • 文章 (post_type: 'post') 用于创建feed,文章是大多数博客一次显示一篇已发布文章的类型。
  • 页面 (post_type: 'page') 与文章 相同,但有一些区别。 页面类型使用模板,不受基于时间的列表的约束(如典型博客文章的情况),不能分配类别/标签,并且可以显示在分层结构中。
  • 附件 (post_type: 'attachment') 保存通过WordPress媒体上传系统上传的文件的信息(包括图像和相关元数据)。
  • 修订 (post_type: 'revision') 用于保存草稿文章以及已发布文章的任何过去修订版。 
  • 导航菜单 (post_type: 'nav_menu_item') 保存有关WordPress导航菜单系统中的每个项目的详细信息。

自定义文章类型

这些是您可以创建的新的 post_type。 必须通过register_post_type()函数将自定义文章类型添加到WordPress:它定义了文章类型的具体细节,如标签、功能、可用性等。

Codex提示:请注意,您必须在 admin_menu 之前和 after_setup_theme 操作 hooks之后调用 register_post_type()。 您可以在此处使用init hook。

这是一个添加 自定义文章类型的基本示例

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'selsior_product',
    array(
      'labels' => array(
        'name' => __( 'Products' ),
        'singular_name' => __( 'Product' )
      ),
      'public' => true,
      'has_archive' => true,
    )
  );
}

来源

这会创建一个名为 Product 的 post_type,被标识为 selsior_product

register_post_type() 函数有两个主要参数:labels 定义文章类型名称(单数和复数形式); public 是一个标志,用于在管理屏幕和公共场合(在网站上)显示文章类型,如果它有存档等。

您可以向您的第一个标签数组添加更多选项,以进一步详细说明和定义它。 这是一个不同的例子

/*
* Creating a function to create our CPT
*/

function custom_post_type() {

// Set UI labels for Custom Post Type
	$labels = array(
		'name'                => _x( 'Movies', 'Post Type General Name', 'twentythirteen' ),
		'singular_name'       => _x( 'Movie', 'Post Type Singular Name', 'twentythirteen' ),
		'menu_name'           => __( 'Movies', 'twentythirteen' ),
		'parent_item_colon'   => __( 'Parent Movie', 'twentythirteen' ),
		'all_items'           => __( 'All Movies', 'twentythirteen' ),
		'view_item'           => __( 'View Movie', 'twentythirteen' ),
		'add_new_item'        => __( 'Add New Movie', 'twentythirteen' ),
		'add_new'             => __( 'Add New', 'twentythirteen' ),
		'edit_item'           => __( 'Edit Movie', 'twentythirteen' ),
		'update_item'         => __( 'Update Movie', 'twentythirteen' ),
		'search_items'        => __( 'Search Movie', 'twentythirteen' ),
		'not_found'           => __( 'Not Found', 'twentythirteen' ),
		'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
	);
	
// Set other options for Custom Post Type
	
	$args = array(
		'label'               => __( 'movies', 'twentythirteen' ),
		'description'         => __( 'Movie news and reviews', 'twentythirteen' ),
		'labels'              => $labels,
		// Features this CPT supports in Post Editor
		'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
		// You can associate this CPT with a taxonomy or custom taxonomy. 
		'taxonomies'          => array( 'genres' ),
		/* A hierarchical CPT is like Pages and can have
		* Parent and child items. A non-hierarchical CPT
		* is like Posts.
		*/	
		'hierarchical'        => false,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 5,
		'can_export'          => true,
		'has_archive'         => true,
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'page',
	);
	
	// Registering your Custom Post Type
	register_post_type( 'movies', $args );

}

/* Hook into the 'init' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( 'init', 'custom_post_type', 0 );

来源

现在我们创建了一个文章类型电影,它将被归类为流派,并通过文章编辑器支持摘要、作者、评论、自定义字段等的配置。

显示您的自定义文章类型

如果您没有为您的文章类型创建自定义模板,WordPress将使用您主题中的默认模板(archive.php和single.php)来显示您新的文章类型。 如果您坚持这样做,您将需要更新永久链接结构。

转到外观 >> 菜单,并向您的菜单添加一个新的自定义链接(例如,产品、电影)。 默认情况下,它应该看起来像这样:www.your-website.com/?post_type=products

写下您自己的域名,并将“产品”替换为您的自定义文章类型的名称。

您还可以创建一个自定义模板来显示您的文章类型存档或单篇文章。 最简单(也合理)的方法是将主题的archive.php中的所有内容复制到一个名为archive-{post_type name}.php的新文件中,然后开始修改它。 您可以对您的single-{post-type name}.php文件执行相同的操作。

还有一些做法需要牢记

  • 为了避免在切换到另一个主题时破坏网站或丢失自定义文章类型,最好将自定义文章类型定义为插件。
  • 不要使用通用的自定义文章类型名称(如上面示例中使用的“产品”或“电影”),而应使用带有文章类型名称的前缀。 这样可以最大限度地减少与其他插件或主题创建的文章类型发生冲突的可能性。 字符长度不要超过 20 个字符,否则它会变得太大而无法放入 VARCHAR 字段,并且不要使用wp_
  • 通过添加适当的控件,让您的客户/管理员/自己控制文章类型配置。 例如,这是您编辑自定义文章类型的概览页面列的方式
// Change the columns for the edit CPT screen
function change_columns( $cols ) {
  $cols = array(
    'cb'       => '<input type="checkbox" />',
    'url'      => __( 'URL',      'trans' ),
    'referrer' => __( 'Referrer', 'trans' ),
    'host'     => __( 'Host', 'trans' ),
  );
  return $cols;
}
add_filter( "manage_<CPT>_posts_columns", "change_columns" );

来源

按文章类型查询

您可以通过在任何模板中使用WP_Query的post_type参数创建新查询,以显示来自特定文章类型的文章。 这是一个循环示例,用于显示类型为“product”的 10 篇最新文章及其标题和内容

$args = array( 'post_type' => 'product', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
  the_title();
  echo '<div class="entry-content">';
  the_content();
  echo '</div>';
endwhile;

来源:Codex

主查询中的自定义文章类型

创建和注册 自定义文章类型 并不意味着它会自行添加到主查询中。 要使您的自定义文章类型的文章与其他自定义和默认文章类型一起显示在默认存档或主页上,您需要此代码

// Show posts of 'post', 'page' and 'movie' post types on home page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
  if ( is_home() && $query->is_main_query() )
    $query->set( 'post_type', array( 'post', 'page', 'movie' ) );
  return $query;
}

来源:Codex

关键要点

现在您已经掌握了自定义文章类型,您可以做很多很棒的事情。 基本上,这是用您自己的双手来满足您需求的WordPress的方式。

请记住查看WordPress Codex和其他资源,如果这是您第一次尝试摆弄代码,请确保您正在使用子主题。

© . All rights reserved.