如何从侧边栏的单页自定义 post 类型获取 posts 以显示在页面中?

How do I get the posts from single page custom post type from a sidebar to display in a page?

我有一个名为故事列表的自定义 post 类型,它作为侧边栏的插件完成 widget.The 自定义 post 类型故事列表编译其他自定义列表 post 类型并将它们显示在侧边栏中。与 post 在侧边栏中的显示方式相同,我需要具有相同的列表,在本例中是 "hot topics" 在单击 "view more" 按钮时显示在页面上。我得到的最好的是所有故事列表 post 的存档,但是当你点击它们时,比如热门话题,它会带你到 url http://site/storylist/hot-topics ,这是正确的 url,但它是空白的,只显示页面的标题。关于如何让那些 post 出现在热门话题上有什么建议吗?

下面是让它显示在小部件中的代码:

              <?php

             // Prevent loading this file directly
            defined( 'ABSPATH' ) || exit;


    class ChemTabs extends WP_Widget {

function __construct() {
    parent::__construct(
        'tabs', _('Tabs Widget'),
        array(
            'description' => _('A tabs widget.'),
        ));
}

private function ce_getPostClass($style, $index) {
    switch ($style) {
        case 'first-up':
            if ( $index == 0 ) return 'first-up';
            else return 'list-type';
            break;
        default:
            return $style;
    }
}

private function ce_has($listStyle, $index, $post) {
    $class = $this->ce_getPostClass($listStyle, $index);

    $has = array(
        'thumb'         => false,
        'terms'         => true,
        'date'          => true,
        'content'       => false,
        'excerpt'       => false,
        'title'         => true,
        'video'         => false,
        'webinarDate'   => false
    );
    switch ($class) {
        case 'first-up':
            $has['thumb'] = true;
            $has['excerpt'] = true;
            break;
        case 'list-type':
            $has['terms'] = false;
            break;
        case 'thumblist':
            $has['thumb'] = true;
            break;
        case 'single': // no need for this anymore probably
            $has['content'] = true;
            break;
    }

    switch ($post->post_type) {
        case 'video':
            $has['video'] = true;
            break;
        case 'webinar':
            $has['webinarDate'] = true;
            break;
    }
    return $has;
}

public function widget( $args, $instance ) {
    global $post;

    $instance['tab_1_title']    = isset($instance['tab_1_title'])?$instance['tab_1_title']:'Tab 1';
    $instance['tab_2_title']    = isset($instance['tab_2_title'])?$instance['tab_2_title']:'Tab 2';

    $instance['storylist_id_1'] = isset($instance['storylist_id_1']) ? $instance['storylist_id_1'] : '';
    $instance['storylist_id_2'] = isset($instance['storylist_id_2']) ? $instance['storylist_id_2'] : '';

    $instance['n']              = isset($instance['n']) ? $instance['n'] : 4;

    $title      = isset($instance['title'])?$instance['title']:'';
    $type       = isset($instance['type'])?$instance['type']:'color';
    $class      = isset($instance['class'])?$instance['class']:'brand-primary';

    // Post list configuration
    // Note: posts temporarily rendered with ajax
    $list_style     = isset($instance['listStyle'])?$instance['listStyle']:'thumblist';
    $list_post_type = isset($instance['post_type'])?$instance['post_type']:'post';
    $list_filter    = isset($instance['filter'])?$instance['filter']:'{}';
    $list_display   = isset($instance['display'])?$instance['display']:5;
    $list_channel   = isset($instance['channel'])?$instance['channel']:0;
    $list_division  = isset($instance['division'])?$instance['division']:0;
    $readmore       = isset($instance['readmore'])?$instance['readmore'] : '';

    $list_args1 = array( "post_type" => "any", "post__in" => get_post_meta($instance['storylist_id_1'], 'storylist', true), "orderby" => "post__in" );
    $posts1 = new WP_Query();
    $posts1 = $posts1->query($list_args1);

    $list_args2 = array( "post_type" => "any", "post__in" => get_post_meta($instance['storylist_id_2'], 'storylist', true), "orderby" => "post__in" );
    $posts2 = new WP_Query();
    $posts2 = $posts2->query($list_args2);              

    // Note: posts temporarily rendered with ajax
    echo $args['before_widget'];

    $instance['tab_2_title'] = get_the_title($instance['storylist_id_2']);

    //@TODO: Use get_template_part('content/post', $modifier); on tabs too.
    ?>
    <div class="module tabs">

        <div class="striped striped-gray">
            <!-- Nav tabs -->
            <ul class="nav nav-tabs nav-justified" role="tablist">
                <li class="active">
                    <a href="#tab-pane-1" role="tab" data-toggle="tab">
                        <h2 class="h4"><?= $instance['tab_1_title'] ?></h2>
                    </a>
                </li>
                <li>
                    <a href="#tab-pane-2" role="tab" data-toggle="tab">
                        <h2 class="h4"><?= $instance['tab_2_title'] ?></h2>
                    </a>
                </li>
            </ul>

            <!-- Tab panes -->
            <div class="tab-content">
                <div class="tab-pane active" id="tab-pane-1">
                    <div class="post-list no-c-paginator" max="<?= $instance['n'] ?>">
                        <?php
                        $n = 0;
                        foreach((array)$posts1 as $post): setup_postdata($post);
                            $n++;
                            if ( $n % 3 == 1) {
                                ?> <div class="no-c-container<?php echo $n == 1 ? ' active' : '';?>"> <?php
                            }
                            ?>
                            <article class="article thumblist no-c-paginator-item">
                                <!-- thumbnail -->
                                <?php echo the_post_thumbnail('cm_thumb', array('class' => 'img-thumbnail pull-left', 'style' => 'margin-right: 0.5em;')) ?>

                                <header>
                                    <!-- terms -->
                                    <?php if (!empty($terms) ): ?>
                                        <p class="terms">
                                            <a href="<?= $terms[0]['link'] ?>"><?= $terms[0]['name'] ?></a>
                                        </p>
                                    <?php endif; ?>
                                    <!-- title -->
                                    <h3 class="title"><a href="<?php echo the_permalink(); ?>"><?php echo apply_filters('the_title', the_title()); ?></a></h3>

                                    <!-- Time -->
                                    <div class="date-author">
                                        <?php include(locate_template('UI/byline.php')); ?>
                                        <span class="topic"><?php echo cm_get_single_topic() ?></span>
                                    </div>
                                </header>

                                <div class="clearfix"></div><!-- to clear the thumbnail's left floating -->
                            </article>
                            <?php

                        if ( $n % 3 == 0) {
                            ?> </div> <?php
                        }
                        endforeach; wp_reset_postdata();
                        if ( $n % 3 != 0) {
                                ?> </div> <?php
                        }
                        ?>
                        <?php if($readmore): ?>
                            <div class="actions">
                                <button href="<?php echo $readmore; ?>" class="btn btn-primary btn-xs">View More</button>
                            </div>
                        <?php endif; ?>
                    </div>

                    <div class="actions">
                        <button class="btn btn-primary prev-item" disabled="disabled"><i class="fa fa-chevron-circle-left"></i> Prev</button>
                        <button class="btn btn-primary next-item">Next <i class="fa fa-chevron-circle-right"></i></button>
                    </div>

                </div>
                <div class="tab-pane" id="tab-pane-2">
                    <div class="post-list no-c-paginator" max="<?= $instance['n'] ?>">
                        <?php
                        $n = 0;
                        foreach((array)$posts2 as $post):
                            $n++;
                            if ( $n % 3 == 1) {
                                ?> <div class="no-c-container<?php echo $n == 1 ? ' active' : '';?> "> <?php
                            }

                            ?>
                            <article class="article thumblist no-c-paginator-item">
                                <!-- thumbnail -->
                                <?php echo get_the_post_thumbnail($post->ID, 'cm_thumb', array('class' => 'img-thumbnail pull-left', 'style' => 'margin-right: 0.5em;')) ?>

                                <header>
                                    <!-- terms -->
                                    <?php if (!empty($terms) ): ?>
                                        <p class="terms">
                                            <a href="<?= $terms[0]['link'] ?>"><?= $terms[0]['name'] ?></a>
                                        </p>
                                    <?php endif; ?>
                                    <!-- title -->
                                    <h3 class="title"><a href="<?php echo get_permalink($post->ID); ?>"><?php echo apply_filters('the_title', $post->post_title); ?></a></h3>

                                    <!-- Time -->
                                    <div class="date-author">
                                        <time datetime="<?php the_time('c'); ?>" pubdate="pubdate" ><?php the_time('F j, Y'); ?></time>
                                        <?php include(locate_template('UI/byline.php')); ?>
                                        <span class="topic"><?php echo cm_get_single_topic() ?></span>
                                    </div>
                                </header>

                                <div class="clearfix"></div><!-- to clear the thumbnail's left floating -->
                            </article>
                        <?php
                        if ( $n % 3 == 0) {
                            ?> </div> <?php
                        }
                        endforeach;

                        if ( $n % 3 != 0) {
                            ?> </div> <?php
                        }


                        ?>


                        <?php //if($readmore): ?>
                            <div class="actions">
                                <a href="<?php echo CFS()->get('view_more_link', 210639); ?>" class="btn btn-primary">View More</a>
                            </div>
                        <?php //endif; ?>

                        <!--<div class="actions">
                            <button class="btn btn-primary prev-item" disabled="disabled"><i class="fa fa-chevron-circle-left"></i> Prev</button>
                            <button class="btn btn-primary next-item">Next <i class="fa fa-chevron-circle-right"></i></button>
                        </div>-->

                    </div>
                </div>
            </div>
        </div>
    </div>
<?php
    echo $args['after_widget'];
}

public function form( $instance ) {
    $instance['tab_1_title']    = isset($instance['tab_1_title'])?$instance['tab_1_title']:'Tab 1';
    $instance['tab_2_title']    = isset($instance['tab_2_title'])?$instance['tab_2_title']:'Tab 2';
    $instance['n']              = isset($instance['n'])?$instance['n']:4;

    $sl_posts = get_posts(array(
        'posts_per_page'    => -1,
        'post_type'         => 'storylist'
    ));
    ?>
    <h5>Tab 1</h5>
    <p>
        <label for="<?php echo $this->get_field_id( 'tab_1_title' ); ?>"><?php _e( 'Title:' ); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'tab_1_title' ); ?>" name="<?php echo $this->get_field_name( 'tab_1_title' ); ?>" type="text" value="<?php echo esc_attr( $instance['tab_1_title'] ); ?>">
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'storylist_id_1' ); ?>"><?php _e( 'Story List:' ); ?></label>
        <select id="<?php echo $this->get_field_id( 'storylist_id_1' ); ?>" name="<?php echo $this->get_field_name( 'storylist_id_1' ); ?>">
            <?php
            if (count($sl_posts)) {
                foreach($sl_posts as $storyList) {
                    $selected = ($instance['storylist_id_1'] == $storyList->ID ? "selected='selected'" : "");
                    echo "<option value='{$storyList->ID}' {$selected}>{$storyList->post_title}</option>";
                }
            } else {
                echo "<option value='0'>"._('No Story Lists Found')."</option>";
            }
            ?>
        </select>
    </p>
    <!-- Number of items to show -->
    <p>
        <label for="<?php echo $this->get_field_id( 'n' ); ?>"><?php _e( 'Number of items to show at once:' ); ?></label>
        <input id="<?php echo $this->get_field_id( 'n' ); ?>" name="<?php echo $this->get_field_name( 'n' ); ?>" type="number" min="0" step="1" value="<?php echo intval($instance['n']) ?>">
    </p>

    <h5>Tab 2</h5>
    <p>
        <label for="<?php echo $this->get_field_id( 'tab_2_title' ); ?>"><?php _e( 'Title:' ); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id( 'tab_2_title' ); ?>" name="<?php echo $this->get_field_name( 'tab_2_title' ); ?>" type="text" value="<?php echo esc_attr( $instance['tab_2_title'] ); ?>">
    </p>
    <p>
        <label for="<?php echo $this->get_field_id( 'storylist_id_2' ); ?>"><?php _e( 'Story List:' ); ?></label>
        <select id="<?php echo $this->get_field_id( 'storylist_id_2' ); ?>" name="<?php echo $this->get_field_name( 'storylist_id_2' ); ?>">
            <?php
            if (count($sl_posts)) {
                foreach($sl_posts as $storyList) {
                    $selected = ($instance['storylist_id_2'] == $storyList->ID ? "selected='selected'" : "");
                    echo "<option value='{$storyList->ID}' {$selected}>{$storyList->post_title}</option>";
                }
            } else {
                echo "<option value='0'>"._('No Story Lists Found')."</option>";
            }
            ?>
        </select>
    </p>

<?php
}

public function update( $new_instance, $old_instance ) {
    $instance = array();

    $instance['tab_1_title']    = isset($new_instance['tab_1_title'])?$new_instance['tab_1_title']:'Tab 1';
    $instance['tab_2_title']    = isset($new_instance['tab_2_title'])?$new_instance['tab_2_title']:'Tab 2';

    $instance['storylist_id_1'] = isset($new_instance['storylist_id_1']) ? (int)$new_instance['storylist_id_1'] : 0;
    $instance['storylist_id_2'] = isset($new_instance['storylist_id_2']) ? (int)$new_instance['storylist_id_2'] : 0;

    $instance['n'] = isset($new_instance['n']) ? (int)$new_instance['n'] : 4;

    return $instance;
   }
 }

  add_action( 'widgets_init', function(){ register_widget( 'ChemTabs' ); });

有什么方法可以制作显示页面 ID 的页面模板吗?我试图这样做,但它没有显示任何内容。

我通过添加这段代码解决了这个问题$list_args2 = array( "post_type" => "any", "post__in" => get_post_meta( 210639, 'storylist', true), "orderby" => "post__in", 'post_status' => 'publish' );