我编写了一个简单的插件来创建一个名为“people”的自定义帖子类型:
add_action( \'init\', \'create_person\' );
function create_person() {
register_post_type( \'people\',
array(
\'labels\' => array(
\'name\' => \'People\',
\'singular_name\' => \'Person\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Person\',
\'edit\' => \'Edit\',
\'edit_item\' => \'Edit Person\',
\'new_item\' => \'New Person\',
\'view\' => \'View\',
\'view_item\' => \'View Person\',
\'search_items\' => \'Search People\',
\'not_found\' => \'No person found\',
\'not_found_in_trash\' => \'No people found in trash\',
\'parent\' => \'Parent Person\'
),
\'public\' => true,
\'menu_position\' => null,
\'supports\' => array( \'title\', \'editor\', \'comments\', \'thumbnail\', \'custom-fields\' ),
\'taxonomies\' => array( \'\' ),
\'menu_icon\' => plugins_url( \'images/people_icon.png\', __FILE__ ),
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'people\',\'with_front\' => FALSE),
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'has_archive\' => true
)
);
}
并添加了一个名为“单人”的模板。php显示自定义帖子类型。此筛选器与上面的代码位于同一文件中:
add_filter( \'template_include\', \'include_template_people\', 1 );
function include_template_people( $template_path ) {
if ( get_post_type() == \'people\' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( \'single-people.php\' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . \'/single-people.php\';
}
}
}
return $template_path;
}
然后这里有实际的模板:
<?php
/*Template Name: New Template
*/
get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
$mypost = array( \'post_type\' => \'people\', );
$loop = new WP_Query( $mypost );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="person">
<div class="person-header">
<div class="person-image">
<?php the_post_thumbnail( array( 300, 300 ) ); ?>
</div>
<div class="person-name"><?php the_title(); ?></div>
<div class="roles-list">
<?php echo get_the_term_list( $id, \'roles\', \'\', \', \', \'\' ); ?>
</div>
</div>
<div class="person-content"><?php the_content(); ?></div>
</div>
</article>
<?php endwhile; ?>
</div>
</div>
<?php wp_reset_query(); ?>
<?php get_footer(); ?>
更新日期:
我把问题改了一点。我删除了我添加的一段代码。这个模板的问题是,当我转到URL时:
mysite.com/people/firstname-lastname
我希望只得到“people”类型的帖子,上面有“firstname lastname”。相反,所有类型为“people”的帖子都会显示在页面上。如何更改此设置,以便仅显示单个“帖子”?
最合适的回答,由SO网友:gmazzap 整理而成
如果您发布的“实际模板”的代码位于single-people.php
...
you do not need any query at all!
当您调用url时mysite.com/people/firstname-lastname
wordpress已经知道你想查看那个人,只搜索一个显示它的文件。
通常,wordpress在本例中搜索文件single-people.php 在主题文件夹中,如果找不到,则搜索single.php
依此类推,如下Template Hierarchy.
你的插件修改了这个行为,所以如果没有single-people.php
文件是主题文件夹,而不是搜索single.php
在theme now wordpress中搜索single-people.php
在插件文件夹中。
同样,如果文件来自single-people.php
您不必在这里放置任何查询,就可以使用标准的主wp查询,在本例中,只显示帖子。
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php if ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="person">
<div class="person-header">
<div class="person-image">
<?php the_post_thumbnail( array( 300, 300 ) ); ?>
</div>
<div class="person-name"><?php the_title(); ?></div>
<div class="roles-list">
<?php echo get_the_term_list( get_the_ID(), \'roles\', \'\', \', \', \'\' ); ?>
</div>
</div>
<div class="person-content"><?php the_content(); ?></div>
</div>
</article>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>