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

标签 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(); ?>

最佳答案

首先在函数文件的底部

<?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(); ?>

关于php - 自定义帖子类型WordPress模板如何在WordPress模板中添加自定义侧边栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41040152/

相关文章:

mysql - 通过 MySQL 函数反转字符串中的单词

C++ - 如何创建一个可以在类中与 _ 一起使用的属性?

c++ - ostream << 带有模板类的运算符,不能访问私有(private)成员

php - Yii Active Record COALESCE 不显示结果

java - Android HttpURLConnection POST 不工作

php - Ajax - 与在后台运行的 PHP 脚本进行通信?

wordpress - 在 WordPress 管理栏帖子/产品自定义帖子类型中显示 Slug

php - 在不使用 php 中的第三个变量的情况下交换两个变量值

ajax - 如何使用 "wp_get_nav_menu_items"在WordPress中显示3级分层菜单?

C++:尝试通过组合和帕斯卡三角来理解 constexpr