sam*_*avi 4 wordpress wordpress-theming custom-post-type
我是WordPress的新手.我为视频创建了自定义帖子类型,但我不知道如何在页面中显示帖子类型.例如,我希望当用户添加视频时,他不必在发布视频时选择视频模板,当他们打开已发布的视频时,页面会打开视频播放器而不是打开页面.我想要一个像视频播放器准备好的自定义页面,我所要做的就是给视频播放器提供视频的网址.已经有了视频播放器的代码.我怎样才能做到这一点?
要为所有自定义帖子类型的帖子或页面创建默认模板文件,您可以命名模板文件,single-{your-cpt-name-here}.php或者archive-{your-cpt-name-here}.php在查看这些帖子或页面时始终默认为此模板文件.
所以例如single-video.php你可以把:
<?php query_posts( 'post_type=my_post_type' ); ?>
Run Code Online (Sandbox Code Playgroud)
或者改为使用自定义查询来塑造您想要的输出:
<?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();
?>
Run Code Online (Sandbox Code Playgroud)
在您的自定义循环中,如上例所示,在Wordpress中有许多可用的模板标签(如the_content)可供选择.
小智 5
在 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'),
)
);
Run Code Online (Sandbox Code Playgroud)
现在在您想要显示的地方编写此代码
<?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;
Run Code Online (Sandbox Code Playgroud)
?>
| 归档时间: |
|
| 查看次数: |
33803 次 |
| 最近记录: |