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

时间:2016-08-01 作者:MikeL5799

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

下面是使其显示在小部件中的代码:

              <?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\' ); });

1 个回复
SO网友:jdm2112

听起来好像你只需要创建你的帖子类型模板。的存档storylist 帖子将查找名为archive-storylist.php 显示帖子。

单一类型的职位storylist 将尝试使用single-storylist.php 显示该帖子。

在任何一种情况下,如果这些文件不存在,WordPress都会按照模板层次结构流查找下一个合适的模板。

有关该过程的详细信息,请参见WP Codex:https://codex.wordpress.org/Post_Type_Templates

相关推荐

My widgets do not save

每次我保存我的小部件并离开页面时,我的小部件都会消失。侧边栏已完全清空,不会保存任何更改。控制台或PHP日志中没有任何错误。如果我将小部件直接复制并保存在数据库中widgets_text, 它们将被显示,但我仍然无法在侧边栏中添加或删除任何内容。这只发生在我的右侧边栏上,左侧边栏工作正常,但它们都以相同的方式注册。这是我注册侧边栏的方式:function my_widgets_init() { register_sidebar( array ( \'name\'