您可以创建帖子模板以显示CPT帖子详细信息(即视频/我的视频),也可以创建存档模板以显示帖子列表(即视频/)。
要在插件中创建帖子模板,请向插件中添加以下内容:
function include_templates( $template_path )
{
if ( get_post_type() == \'videos\' )
{
if ( is_single() )
{
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array( \'single-videos.php\' ) ) )
{
$template_path = $theme_file;
}
else
{
$template_path = plugin_dir_path( __FILE__ ) . \'single-videos.php\';
}
}
}
return $template_path;
}
add_filter( \'template_include\', \'include_templates\', 1 );
然后创建一个名为“单个视频”的模板。插件文件夹中的php:
<?php get_header(); ?>
<?php while(have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
要在插件中创建存档模板,请将以下内容添加到插件中:
function include_archive_template($archive_template)
{
global $post;
if ( is_post_type_archive (\'videos\') )
{
$archive_template = plugin_dir_path( __FILE__ ) . \'archive-videos.php\';
}
return $archive_template;
}
add_filter( \'archive_template\', \'include_archive_template\' );
然后创建存档视频。插件文件夹中的php文件:
<?php get_header();
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;
$big = 999999999; // need an unlikely integer
// Get Video details
$args = array(
\'post_type\' => \'vdieos\',
\'order\' => \'ASC\',
\'orderby\' => \'title\',
\'posts_per_page\' => 10,
\'paged\' => $paged
);
$wp_query = new WP_Query($args);
$page_args = array(
\'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
\'format\' => \'?paged=%#%\',
\'total\' => $wp_query->max_num_pages,
\'current\' => $paged,
\'show_all\' => False,
\'end_size\' => 1,
\'mid_size\' => 2,
\'prev_next\' => True,
\'prev_text\' => __(\'«\'),
\'next_text\' => __(\'»\'),
\'type\' => \'list\',
\'add_args\' => False,
\'add_fragment\' => \'\'
);
echo paginate_links( $page_args );
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="video_row clearfix">
<h3><a href="<?php echo get_permalink( $post->ID ); ?>"><?php the_title() ?></a></h3>
<?php the_content(); ?>
<a href="<?php echo get_permalink( $post->ID ); ?>">More Details</a>
</div>
<?php
endwhile;
echo paginate_links( $page_args );
wp_reset_postdata();
get_sidebar();
get_footer(); ?>