Wordpress 显示自定义帖子类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12515421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress Display Custom post types
提问by sammyukavi
I'm a newbie in WordPress. I have created a custom post type for videos but I do not know how to display the post type in a page. For example I want when a user adds a video, he won't have to select a video template when publishing the video and when they open the published video, the page opens up with a video player instead of opening a page. I want something like a custom page with the video player ready that all I have to do is give the video player the url for the video.Already have the code for the video player. How can I do this?
我是 WordPress 的新手。我为视频创建了自定义帖子类型,但我不知道如何在页面中显示帖子类型。例如我希望当用户添加视频时,他在发布视频时不必选择视频模板,并且当他们打开发布的视频时,页面会通过视频播放器打开,而不是打开页面。我想要一个像视频播放器准备好的自定义页面,我所要做的就是给视频播放器提供视频的 url。已经有了视频播放器的代码。我怎样才能做到这一点?
回答by Cody MacPixelface
To make a default template filefor all your custom post type posts or pages, you can name your template file single-{your-cpt-name-here}.php
or archive-{your-cpt-name-here}.php
and it will always default to this when viewing these posts or pages.
要为所有自定义帖子类型的帖子或页面创建默认模板文件,您可以命名模板文件,single-{your-cpt-name-here}.php
或者archive-{your-cpt-name-here}.php
在查看这些帖子或页面时,它将始终默认为该文件。
So for example in single-video.php
you could put:
例如,single-video.php
您可以输入:
<?php query_posts( 'post_type=my_post_type' ); ?>
or instead make a custom query to shape your desired output:
或者进行自定义查询来塑造您想要的输出:
<?php
$args = array(
'post_type' => 'my_post_type',
'post_status' => 'publish',
'posts_per_page' => -1
);
$posts = new WP_Query( $args );
if ( $posts -> have_posts() ) {
while ( $posts -> have_posts() ) {
the_content();
// Or your video player code here
}
}
wp_reset_query();
?>
In your custom loop like the example above, there's lots of available template tags(like the_content
) to choose from in Wordpress.
在上面示例中的自定义循环中,Wordpress 中有许多可用的模板标签(如the_content
)可供选择。
回答by naveed
Write Code In Functions.php
在 Functions.php 中编写代码
function create_post_type() {
register_post_type( 'Movies',
array(
'labels' => array(
'name' => __( 'Movies' ),
'singular_name' => __( 'Movie' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'Movies'),
)
);
Now Write This Code Where You Want To show
现在在你想展示的地方写这个代码
<?php
$args = array( 'post_type' => 'Movies', '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;
?>
?>
回答by Fred K
After you created the CPT, do this for showing single posts of your CPT:
创建 CPT 后,执行此操作以显示 CPT 的单个帖子:
- Duplicate the
single.php
file in your template and rename it likesingle-{post_type}.php
(eg.single-movie.php
) - Remember to flush the permalinks from WordPress!
- 复制
single.php
模板中的文件并将其重命名为single-{post_type}.php
(例如single-movie.php
) - 记得从 WordPress 刷新永久链接!
You can get more details from this post
您可以从这篇文章中获得更多详细信息
Now if you want to show a list of CPT you can use get_posts()with args:
$args = array( ... 'post_type' => 'movie' )
现在,如果您想显示 CPT 列表,您可以将get_posts()与 args 一起使用:
$args = array( ... 'post_type' => 'movie' )
Check this postfor further details.
查看此帖子以获取更多详细信息。