wordpress中自定义帖子类型的模板有问题。这是我的功能。hp:
add_action( 'init', 'register_cpt_door' );
function register_cpt_door() {
$args = array(
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'door'),
'capability_type' => 'post'
);
register_post_type( 'door', $args );
}
我使用singledoor.php作为单页,它很有效。但我尝试使用category-door.php和archive-door.php作为category,它们都不起作用,wordpress返回index.php.
我应该使用什么模板名称?
它不起作用,因为category-door.php
是带有段塞door
的类别的模板。archive-door.php
是自定义帖子类型归档的模板,而不是特定类别中具有类型门的帖子的模板。
https://codex.wordpress.org/Template_Hierarchy
分类术语可以属于多个帖子类型,因此无法为特定帖子类型范围内的分类创建模板。
这里的解决方案是为门创建一个自定义分类法:
https://codex.wordpress.org/Function_Reference/register_taxonomy
一旦创建了自定义分类法(我称之为door-categories
),就可以在taxonomy-door-categories.php
文件中创建该分类法中的模板术语。
请查看模板层次结构。
文件category-door.php
将用于具有slugdoor的类别。
文件archive-door.php
用于您已使用选项'has_archive' => true
启用的自定义文章类型的主存档。它不适用于类别索引。
对于您的自定义文章,您使用了Wordpress中默认存在的分类法category
,因此您可以使用模板文件category.php
。在循环中,你可以检查帖子类型,并给它一个不同的类别/风格:
<?php while ( have_posts() ) : the_post(); ?>
<?php if( 'door' == get_post_type() ): ?>
<div class="door">
This is a door
</div>
<?php else: ?>
<div class="post">
This is a post
</div>
<?php endif; ?>
<?php endwhile; ?>
否则,如果您想要自定义帖子类型的不同类别,则需要注册一个新的分类法。
Custom Post Type in wordpress. Basic four steps
Step 1 : File Path location : theme/function.php in your theme
Paste code in function.php (register custom post type )
<?php
add_action( 'init', 'custom_post_type_func' );
function custom_post_type_func() {
//posttypename = services
$labels = array(
'name' => _x( 'Services', 'services' ),
'singular_name' => _x( 'services', 'services' ),
'add_new' => _x( 'Add New', 'services' ),
'add_new_item' => _x( 'Add New services', 'services' ),
'edit_item' => _x( 'Edit services', 'services' ),
'new_item' => _x( 'New services', 'services' ),
'view_item' => _x( 'View services', 'services' ),
'search_items' => _x( 'Search services', 'services' ),
'not_found' => _x( 'No services found', 'services' ),
'not_found_in_trash' => _x( 'No services found in Trash', 'services' ),
'parent_item_colon' => _x( 'Parent services:', 'services' ),
'menu_name' => _x( 'Services', 'services' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'description' => 'Hi, this is my custom post type.',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'category', 'post_tag', 'page-category' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'services', $args );
}
?>
步骤2:如何在wordpress模板页面中显示wordpress自定义帖子类型?
: you can show anywhere in template page like this :
<?php $args = array( 'post_type' => 'services', 'posts_per_page' => 20 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="services-items">
<?php the_title();
if ( has_post_thumbnail( $post->ID ) ) {
echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">';
echo get_the_post_thumbnail( $post->ID, 'thumbnail' );
echo '</a>'; }
?>
</div>
<?php endwhile; ?>
步骤3:创建一个新的模板显示像这样的
single-{自定义帖子类型名称}.php或单服务.pp
步骤4:在singleservices.php文件中粘贴代码
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="main-post-div">
<div class="single-page-post-heading">
<h1><?php the_title(); ?></h1>
</div>
<div class="content-here">
<?php the_content(); ?>
</div>
<div class="comment-section-here"
<?php //comments_template(); ?>
</div>
</div>
<?php endwhile; ?>
这是一个自定义的帖子类型的例子,带有单个帖子页面。