2016-08-03 63 views
0

好的,我有一個側欄小部件可以從一個自定義帖子類型的帖子列表中提取,該自定義帖子類型本身從所有其他自定義帖子類型中拉入。我需要在頁面的側邊欄中顯示來自標籤2的帖子列表,以列表形式存檔。這是拉的職位側邊欄的代碼:如何從側邊欄獲取帖子以顯示爲常規帖子頁面

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

我創建了一個頁面模板專門針對該網頁,但它只是出現空白。這是該頁的代碼:

<?php 
    /* 
    Template Name: Custom StoryList Archives 
    */ 
    ?> 

    <?php get_header(); ?> 
    <?php 
     if (is_single(210639)) { 

     $args = array('post_type' => 'storylist', 'posts_per_page' => 30); 

     } 
     endif(); 
    ?> 

    <?php get_footer(); ?> 

我在想什麼?

回答

0

我通過添加代碼解決了這個問題:$list_args2 = array("post_type" => "any", "post__in" => get_post_meta(210639, 'storylist', true), "orderby" => "post__in", 'post_status' => 'publish');

相關問題