为自定义帖子类型创建一个“单个”页面
4 个回答
- 投票数
-
- 2012-04-26
对单个模板使用
single-{posttype}.php
.另外,如果您在has_archive
参数设置为true
的情况下注册帖子类型,则可以将archive-{posttype}.php
用于您的存档模板,由于全局$wp_query
对象已经使用您的自定义帖子类型填充了,因此您可以跳过那里的查询.顺便说一句,在
post_type
参数中有一个空格,这将是一个问题.查看模板层次结构,并考虑使用代码在插件中注册您的CPT ,而不是使用CPT UI插件.
Use
single-{posttype}.php
for the single template. Also, if you register your post type with thehas_archive
argument set totrue
, then you can usearchive-{posttype}.php
for your archive template, which will allow you to skip that query that you have there, since the global$wp_query
object will already be populated with your custom post type.BTW, you have a space in your
post_type
argument, which will be a problem.Check out the Template Hierarchy, and consider registering your CPTs using code in a plugin rather than using a CPT UI plugin.
-
- 2015-04-10
没有必要,因为WordPress将使用默认页面模板,但是您可以创建自定义 single-cpt.php 文件,其中cpt是您的已注册帖子类型的名称.
><?php get_header(); ?> <div id="main-content" class="main-content"> <div id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <?php // Start the Loop. while ( have_posts() ) : the_post(); // Include the page content template. get_template_part( 'content', 'page' ); endwhile; ?> </div><!-- #content --> </div><!-- #primary --> </div><!-- #main-content --> <?php get_sidebar(); get_footer();
There's no need as WordPress will use the default page template however you can create a custom single-cpt.php file where cpt is the name of your registered post type.
<?php get_header(); ?> <div id="main-content" class="main-content"> <div id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <?php // Start the Loop. while ( have_posts() ) : the_post(); // Include the page content template. get_template_part( 'content', 'page' ); endwhile; ?> </div><!-- #content --> </div><!-- #primary --> </div><!-- #main-content --> <?php get_sidebar(); get_footer();
-
- 2012-04-26
您可以将其写到single.php文件中(在循环内),并在if语句中回显所需的任何字段.
if($post_type == 'case_studies') { // you may need this to be without spaces (machine name) echo '<h1>'.get_the_title().' flavors</h1>'; // post id $post_id = get_the_ID(); get_post_meta($post_id, 'custom_field_name', true); <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a> <?php endwhile; ?> }
另一个选择是t0创建页面模板.复制您的single.php文件,并将其重命名为case_studies.php ..在php标签顶部,添加:
<?php /* Template Name: Brand Output 04/12 */ ?>
,然后在single.php循环中添加与上述示例相同的if语句...
You could just write this into your single.php file (within the loop) and echo out whatever fields you need within the if statement.
if($post_type == 'case_studies') { // you may need this to be without spaces (machine name) echo '<h1>'.get_the_title().' flavors</h1>'; // post id $post_id = get_the_ID(); get_post_meta($post_id, 'custom_field_name', true); <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a> <?php endwhile; ?> }
Another option is t0 create a page template. Copy your single.php file and rename it case_studies.php .. at the top within php tags add:
<?php /* Template Name: Brand Output 04/12 */ ?>
and then add the same if statement within the single.php loop as the above example...
-
这行得通,但是这是不好的做法,不好的做法是,最应该接近的方法是get_template_part('stuff',$post->post_type);Thsi works, but it is bad, bad practice, the nearest you should ever get to this is `get_template_part('stuff',$post->post_type);`
- 0
- 2012-04-26
- Tom J Nowell
-
你能解释为什么这是不好的做法吗?can you explain why it is bad practice?
- 0
- 2012-04-27
- Starfs
-
因为它是不干净的代码,并且您有大量的ifelse语句和重复的代码.您最好创建一个类似于'content.php'的模板文件,并执行`get_template_part('content',$post_type);`并使用`content-case_studies.php`在每个帖子类型的基础上覆盖它Because it's unclean code, and you have a tonne of if else statements, and duplicated code. You would be better creating a template file like 'content.php', and doing `get_template_part('content',$post_type);` and using `content-case_studies.php` to override it on a per post type basis
- 0
- 2012-04-27
- Tom J Nowell
-
这样,您的single.php仍然可读.即使那样,最好还是以正确的方式使用并使用`single-case_studies.php`.That way your single.php remains readable. Even then it would eb better ot do it the proper way and use `single-case_studies.php`
- 0
- 2012-04-27
- Tom J Nowell
-
凉.我在主题中更改了代码,以反映出这种用于输出自定义帖子类型的新方法.感谢大家的注意cool. I changed the code in my theme to reflect this new method for outputting custom post types. thanks for the heads up
- 3
- 2012-04-30
- Starfs
-
- 2015-04-10
在wordpress中自定义帖子类型.基本四个步骤.第一步:在主题中输入文件路径:theme/function.php.在function.php中粘贴代码(注册自定义帖子类型)
<?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 ); } ?>
Step2:如何在wordpress模板页面中显示wordpress自定义帖子类型?
您可以在模板页面的任何位置显示这样的内容:
<?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步:为显示这样的单个帖子创建新模板
单个-{自定义帖子类型名称} .php 要么 single-services.php
Step4:将代码粘贴到single-services.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; ?>
这是具有单个帖子页面的自定义帖子类型示例.
Custom Post Type in wordpress.Basic four steps.Step1: 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 ); } ?>
Step2: how can show wordpress custom post type in wordpress template page ?
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; ?>
Step3: Create new template for show single post like this
single-{custom post type name}.php or single-services.php
Step4: Paste code in single-services.php file
<?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; ?>
This is custom post type example with single post page.
好的,我安装了Custom Post Type UI插件并创建了一个.然后,我向其添加了新帖子.在我的主题中,我有一段这样的代码:
现在,首先,如果我单击缩略图,则会在浏览器中收到错误消息,指出它处于重定向循环中,但是其次,我想确切地知道我需要创建哪些文件才能查看此自定义帖子的单个帖子类型.以及该文件的内容.