WordPress的显示自定义帖子类型

我是WordPress的新手。我为视频创建了自定义帖子类型, 但是我不知道如何在页面中显示帖子类型。例如, 我希望当用户添加视频时, 他不必在发布视频时选择视频模板, 并且当他们打开已发布的视频时, 该页面将使用视频播放器打开, 而不是打开页面。我想要一个类似视频播放器的自定义页面, 我需要做的就是为视频播放器提供视频的网址。已经有了视频播放器的代码。我怎样才能做到这一点?
#1要为所有自定义帖子类型的帖子或页面创建默认模板文件, 可以将模板文件命名为” -{your-cpt-name-here} .php” 或” -archive- {your-cpt-name-here} .php” 和在查看这些帖子或页面时, 它将始终默认为此设置。
因此, 例如在single-video.php中, 你可以输入:

< ?php query_posts( 'post_type=my_post_type' ); ?>

或者改为进行自定义查询以调整所需的输出:
< ?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(); ?>

在你的自定义循环(如上面的示例)中, 在Wordpress中有很多可用的模板标记(例如the_content)可供选择。
#2在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'), ) );

现在在要显示的地方编写此代码
< ?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;

?>
#3创建CPT后, 请执行以下操作以显示CPT的单个帖子:
  • 复制模板中的single.php文件, 并将其重命名为single- {post_type} .php(例如single-movie.php)
  • 记住要清除WordPress的永久链接!
你可以从这篇文章中获得更多详细信息
  • 现在, 如果要显示CPT列表, 可以将get_posts()与args结合使用:
    【WordPress的显示自定义帖子类型】$ args = array(… ’ post_type’ => ’ 电影’ )
检查此帖子以获取更多详细信息。

    推荐阅读