自定义帖子类型WordPress模板如何在WordPress模板中添加自定义侧边栏

时间:2016-12-08 13:00:08

标签: php wordpress templates plugins custom-wordpress-pages

我创建了一个模板,并添加了显示自定义帖子类型的代码行,但我无法获得侧边栏。

以下是我添加到page-movie-reviews.php文件中的一些代码:

<?php
/*<?php Template Name: Movie Reviews */ ?>
<?php get_header();?>
<?php
 $query = new WP_Query( array('post_type' => 'movie-reviews', 'posts_per_page' => 5 ) );
 while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="cusomt-title">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
</div>
<?php the_post_thumbnail( array( 100, 100 ) ); // Other resolutions (height, width) ?>
<?php the_content(); ?>
<?php the_excerpt(); ?>
<?php echo apply_filters('the_content',$mypost->post_content); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

1 个答案:

答案 0 :(得分:3)

首先在功能文件的底部

<?php
/**
 * Register our sidebars and widgetized areas.
 *
 */
function movie_widgets_init() {

    register_sidebar( array(
        'name'          => 'Movie right sidebar',
        'id'            => 'mome_right_1',
        'before_widget' => '<div>',
        'after_widget'  => '</div>',
        'before_title'  => '<h2>',
        'after_title'   => '</h2>',
    ) );

}
add_action( 'widgets_init', 'movie_widgets_init' );
?>

https://codex.wordpress.org/Widgetizing_Themes

然后显示侧边栏

<?php
/*<?php Template Name: Movie Reviews */ ?>
<?php get_header();?>
<?php
 $query = new WP_Query( array('post_type' => 'movie-reviews', 'posts_per_page' => 5 ) );
 while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="cusomt-title">
<h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
</div>
<?php the_post_thumbnail( array( 100, 100 ) ); // Other resolutions (height, width) ?>
<?php the_content(); ?>
<?php the_excerpt(); ?>
<?php echo apply_filters('the_content',$mypost->post_content); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php if ( is_active_sidebar( 'mome_right_1' ) ) : ?>
    <?php dynamic_sidebar( 'mome_right_1' ); ?>
<?php endif; ?>
<?php get_footer(); ?>