0

I have a custom post type called storylist, which is done as a plugin for a sidebar widget.The custom post type story list compiles lists of other custom post types and displays them in the sidebar. The same way the posts are displaying in the sidebar, I need to have that same list, which in this case is "hot topics" display on a page when the "view more" button is clicked. The best I got was an archive of all the storylist posts but when you click on them, such as hot topics, it take you to the url http://site/storylist/hot-topics , which is the correct url, but it's blank and only shows the title of the page. Any suggestions on how to make those posts appear on hot topics?

Here's the code to make it display in the widget:

              <?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' ); });
2
  • Is "hot topics" a taxonomy for your 'storylist' custom post type or the title of a single storylist post?
    – jdm2112
    Commented Aug 1, 2016 at 20:02
  • Hi @jdm2112 "hot topics" is the title of a single storylist post
    – MikeL5799
    Commented Aug 1, 2016 at 20:03

1 Answer 1

0

It sounds as though you just need to create your post type templates. The archive of storylist posts will look for a template named archive-storylist.php to display posts.

A single post of type storylist will attempt to use single-storylist.php to display that post.

In either situation, if those files do not exist, WordPress follows the template hierarchy flow to find the next suitable template.

Good detail on the process can be found in WP Codex here: https://codex.wordpress.org/Post_Type_Templates

6
  • Thank you as I did create an archive-storylist but it didn't work for me. as for single-storylist.php, the only reason I didn't make that is because storylist actually pulls in posts from other post types and displays them in a sidebar widget, so it's not an actual stand alone post type per se.
    – MikeL5799
    Commented Aug 1, 2016 at 20:10
  • So what exactly is the question? I appear to have misunderstood the problem.
    – jdm2112
    Commented Aug 1, 2016 at 20:18
  • I'm sorry I probably didn't articulate it well. The custom post type story list compiles lists of other custom post types and displays them as a sidebar widget. The same way the posts are displaying in the sidebar, I need to have that same list, which in this case is "hot topics" display on a page when the "view more" button is clicked. I thought maybe making an archives page would do the trick, but it doesn't
    – MikeL5799
    Commented Aug 1, 2016 at 20:26
  • No worries! Sounds like a difficult one to explain. So your archive would not simply just loop through each post of type `storylist'. Your single (in this case 'hot topics') would loop through the posts to build a list. It sounds as though the plugin is using a post type for classifying other posts, as a taxonomy would? Is the plugin on the WP Plugins repo? If so I would reference that so other users can take a crack at answering this for you.
    – jdm2112
    Commented Aug 1, 2016 at 20:33
  • No but let me add the code into the question up top
    – MikeL5799
    Commented Aug 1, 2016 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.