2012-09-20 247 views
2

我是WordPress的新手。我为视频创建了自定义帖子类型,但我不知道如何在页面中显示帖子类型。例如,我希望当用户添加视频时,他不必在发布视频时选择视频模板,并且在他们打开已发布的视频时,页面会以视频播放器打开,而不是打开页面。我想要一个像视频播放器一样的自定义页面,我只需要为视频播放器提供视频的网址。已经有视频播放器的代码。我怎样才能做到这一点?Wordpress显示自定义帖子类型

回答

5

为了使默认template file你所有的自定义后类型的文章或网页,你能说出你的模板文件single-{your-cpt-name-here}.phparchive-{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(); 
?> 

在类似的例子自定义环以上,有很多可用的template tags(如the_content)在Wordpress中选择。

+0

酷!有用。谢啦。 – sammyukavi

1

编写代码的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; 

?>

1

您创建的CPT后,做这显示您的彩管的单个职位:

  • 复制的single.php文件在你的模板,并像 single-{post_type}.php将其重命名(例如: single-movie.php
  • 记得刷新WordPress的永久链接!

您可以从this post

  • 得到更多的细节现在,如果你想显示CPT的列表,你可以使用get_posts()与ARGS:

    $args = array( ... 'post_type' => 'movie' )

检查this post了解更多详情。

相关问题