2014-02-17 31 views
0

因此,即時建立一個網站至少是基於templatic events v2主題。我已經完成了一些可以正常工作的定製,但是這是一種我無法實現的功能。WordPress的 - 只顯示被標記爲特色的(自定義)帖子

問題如下。

我需要構建一個小部件,只輸出標記爲精選的事件(自定義帖子)。現在我已經有了一些顯示事件的不同方式,但它們幾乎都是按類別或按日期排序的。

示例站點:http://www.blaest.no/jonnysnorkel

這裏是我的插件的當前代碼的插件IM工作:

class blaestfeatured extends WP_Widget { 
    function blaestfeatured() { 
    //Constructor 
     $widget_ops = array('classname' => 'widget category List View', 'description' => 'Custom Featured event widget by Christoffer Sandstrøm'); 
     $this->WP_Widget('blaestfeatured', 'blaestfeatured', $widget_ops); 
    } 

    function widget($args, $instance) { 
    // prints the widget 

     extract($args, EXTR_SKIP); 
     echo $before_widget; 
     $widget_id= $args['widget_id']; 
     $title = empty($instance['title']) ? '' : apply_filters('widget_title', $instance['title']); 
     $category = empty($instance['category']) ? '' : apply_filters('widget_category', $instance['category']); 
     $post_number = empty($instance['post_number']) ? '5' : apply_filters('widget_post_number', $instance['post_number']); 
     $post_link = empty($instance['post_link']) ? '' : apply_filters('widget_post_link', $instance['post_link']); 
     $more_link = empty($instance['more_link']) ? '' : apply_filters('widget_more_link', $instance['more_link']); 
     $character_cout = empty($instance['character_cout']) ? '15' : apply_filters('widget_character_cout', $instance['character_cout']); 
     $sorting = empty($instance['event_sorting']) ? 'Latest Published' : apply_filters('widget_event_sorting', $instance['event_sorting']); 
     ?> 
      <h3 class=""><?php echo $title; ?> </h3> 
      <ul> 
      <?php //$type = get_option('ptthemes_event_sorting'); 
      if ($sorting != '') 
      { 
       global $wpdb; 
       if ($sorting == 'Random') 
       { 
        $orderby = "(select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id = p.ID and $wpdb->postmeta.meta_key like \"st_date\") ASC, rand()"; 
       } 
       elseif ($sorting == 'Alphabetical') 
       { 
        $orderby = "p.post_title ASC"; 
       }elseif($sorting =='s_date'){ 
        $today = date('Y-m-d'); 
        $orderby = "(select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id = p.ID and $wpdb->postmeta.meta_key like \"st_date\") ASC"; 
       } 
       else 
       { 
        $orderby = "(select $wpdb->postmeta.meta_value from $wpdb->postmeta where $wpdb->postmeta.post_id=p.ID and $wpdb->postmeta.meta_key = 'featured_h') ASC, p.post_date DESC"; 
       } 
      } 
      ?> 
      <?php 
      global $post,$wpdb; 
      if($category) 
      { 
       $category = "'".str_replace(",","','",$category)."'"; 
       $sqlsql = " and p.ID in (select tr.object_id from $wpdb->term_relationships tr join $wpdb->term_taxonomy t on t.term_taxonomy_id=tr.term_taxonomy_id where t.term_id in ($category) )"; 
      } 

      $today = date('Y-m-d'); 
      if (false === ($latest_menus = get_transient('onecolumnslist'.$widget_id))) { 
       // It wasn't there, so regenerate the data and save the transient  
       $where = "AND p.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='end_date' and date_format($wpdb->postmeta.meta_value,'%Y-%m-%d')>='".$today."') "; 
       @$sql = "select p.* from $wpdb->posts p where p.post_type='".CUSTOM_POST_TYPE1."' and (p.post_status='publish' or p.post_status='recurring') AND (p.ID in (select $wpdb->postmeta.post_id from $wpdb->postmeta where $wpdb->postmeta.meta_key='event_type' and $wpdb->postmeta.meta_value ='Regular event')) $sqlsql $where order by $orderby limit $post_number"; 
       $latest_menus = $wpdb->get_results($sql); 
       set_transient('onecolumnslist'.$widget_id, $latest_menus, 60*60*12); 
      } 

      $pcount=0; 
      if($latest_menus) 
      { 
       foreach($latest_menus as $post) : 
       setup_postdata($post); 
       $pcount++; ?> 
        <?php 
        $is_parent = $post->post_parent; 
        if($is_parent) 
         $post_id = $post->post_parent; 
        else 
         $post_id = $post->ID; 
        $post_images = bdw_get_images_with_info($post_id,'thumb'); 
        $image = wp_get_attachment_image_src(get_post_thumbnail_id($post_id) , 'single-post-thumbnail' ); 
        if($image[0] != '') 
         $thumb = $image[0]; 
        elseif($post_images[0]['file'] != '') 
         $thumb = $post_images[0]['file']; ?> 

         <?php 
$terms = get_the_terms($post->ID, 'eventcategory'); // Definerer måten å hente cssclassen for terms. 

if ($terms && ! is_wp_error($terms)) : 

    $classcategory = array(); 

    foreach ($terms as $term) { 
     $classcategory[] = $term->name; 
    } 

    $postclasscategory = join(", ", $classcategory); 
?> 



<?php 
$args = array(
    'post_type' => 'event', 
    'meta_query' => array(
     array(
      'key' => 'featured_h', 
      'value' => 'h', 
     ) 
    ) 
); 
$query = new WP_Query($args); 
while($query->have_posts()) : $query->the_post(); ?> 
    <li id="post-<?php the_ID(); ?>" <?php get_post_class(); ?> ><?php } ?> 
      <h3 class="<?php echo $postclasscategory; ?>" > <?php echo get_formated_date(get_post_meta($post->ID,'st_date',true));?> </h3> 
      <div class="eventcontainer"> 
      <?php if(get_post_meta($post->ID,'featured_h',true) == 'h') { ?><div class="featured_img_s"></div><?php }?> 
      <?php if ($thumb != '') { ?> 
      <a class="headliner-img" href="<?php the_permalink(); ?>"> 
      <img src="<?php echo $thumb; ?>" width="290" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a> 
      <?php 
      } else { ?> 
      <a href="<?php echo get_permalink($post->ID) ; ?>" class="headliner-img"><img src="<?php echo get_template_directory_uri()."/images/no-image.png"; ?>" height="120" alt="<?php echo @$post_img[0]['alt']; ?>" /></a> 
      </div> 
<?php endwhile; ?> 



        <h3 id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> 

       </li> <!-- --> 
       <?php endforeach; ?> 
       <?php }else{ ?> 
        <p><?php _e('Not a single Event is there','templatic');?></p> 
       <?php } ?> 
<?php 

     echo '</ul>'; 

     echo $after_widget; 
    } 

    function update($new_instance, $old_instance) { 
    //save the widget 
     $instance = $old_instance; 
     $instance['title'] = strip_tags($new_instance['title']); 
     $instance['category'] = strip_tags($new_instance['category']); 
     $instance['post_number'] = strip_tags($new_instance['post_number']); 
     $instance['post_link'] = strip_tags($new_instance['post_link']); 
     $instance['more_link'] = strip_tags($new_instance['more_link']); 
     $instance['character_cout'] = strip_tags($new_instance['character_cout']); 
     $instance['event_sorting'] = strip_tags($new_instance['event_sorting']); 
     return $instance; 

    } 

    function form($instance) { 
    //widgetform in backend 
     $instance = wp_parse_args((array) $instance, array('title' => '', 'category' => '', 'post_number' => '','character_cout' => '','more_link' => '', 'event_sorting' => 'Latest Published')); 
     $title = strip_tags($instance['title']); 
     $category = strip_tags($instance['category']); 
     $post_number = strip_tags($instance['post_number']); 
     $post_link = strip_tags($instance['post_link']); 
     $more_link = strip_tags($instance['more_link']); 
     $character_cout = strip_tags($instance['character_cout']); 
     $sorting = strip_tags($instance['event_sorting']); 

?> 
<p> 
    <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo TITLE_TEXT; ?>: 
    <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /> 
    </label> 
</p> 
<p> 
    <label for="<?php echo $this->get_field_id('category'); ?>"><?php echo CATEGORY_IDS_TEXT; ?>: 
    <input class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" type="text" value="<?php echo esc_attr($category); ?>" /> 
    </label> <p><?php echo fetch_categories_ids('eventcategory'); ?></p> 
</p> 
<p> 
    <label for="<?php echo $this->get_field_id('post_number'); ?>"><?php echo NUMBER_POSTS_TEXT; ?>: 
    <input class="widefat" id="<?php echo $this->get_field_id('post_number'); ?>" name="<?php echo $this->get_field_name('post_number'); ?>" type="text" value="<?php echo esc_attr($post_number); ?>" /> 
    </label> 
</p> 
<p> 
    <label for="<?php echo $this->get_field_id('event_sorting'); ?>"><?php echo SORT_EVENT; ?>: 
    <select name="<?php echo $this->get_field_name('event_sorting'); ?>" id="<?php echo $this->get_field_id('event_sorting'); ?>"> 
    <option selected="selected" value="Latest Published"><?php _e('Latest Published','templatic'); ?></option> 
    <option <?php if ($sorting == 'Random') { echo 'selected=selected'; } ?> value="Random"><?php _e('Random','templatic'); ?></option> 
    <option <?php if ($sorting == 'Alphabetical') { echo 'selected=selected'; } ?> value="Alphabetical"><?php _e('Alphabetical','templatic'); ?></option> 
    <option <?php if ($sorting == 's_date') { echo 'selected=selected'; } ?> value="s_date"><?php _e('As Per Start Date','templatic'); ?></option> 
    </select> 
    </label> 
</p> 
<?php 
    } 
} 
register_widget('blaestfeatured'); 
// BLAEST FEATURED ENDS 
?> 

I`ve嘗試了幾個晚上我自己,但不幸的是我的PHP技能最好是暗淡..

任何幫助將深表感謝! - Chris

編輯:用整個小部件代碼更新。

回答

1

這裏是你的查詢:

$args = array(
    'post_type' => 'my_custom_post_type', 
    'posts_per_page' => 10, 
    'meta_query' => array(
     array(
      'key' => 'featured_h', 
      'value' => 'h', 
     ) 
    ) 
); 
$query = new WP_Query($args); 
while($query->have_posts()) : $query->the_post(); ?> 
    <li id="post-<?php the_ID(); ?>" <?php get_post_class(); ?> ><?php } ?> 
      <h3 class="<?php echo $postclasscategory; ?>" > <?php echo get_formated_date(get_post_meta($post->ID,'st_date',true));?> </h3> 
      <div class="eventcontainer"> 
      <?php if(get_post_meta($post->ID,'featured_h',true) == 'h') { ?><div class="featured_img_s"></div><?php }?> 
      <?php if ($thumb != '') { ?> 
      <a class="headliner-img" href="<?php the_permalink(); ?>"> 
      <img src="<?php echo $thumb; ?>" width="290" alt="<?php the_title(); ?>" title="<?php the_title(); ?>" /></a> 
      <?php 
      } else { ?> 
      <a href="<?php echo get_permalink($post->ID) ; ?>" class="headliner-img"><img src="<?php echo get_template_directory_uri()."/images/no-image.png"; ?>" height="120" alt="<?php echo @$post_img[0]['alt']; ?>" /></a> 
      </div> 
<?php endwhile; ?> 
+0

很好的回答雖然'「當你使用一元查詢meta_key''不是必需的。 –

+0

這是正確的 - 已更新。 – Banago

+0

非常感謝!我知道這是一個相當遲鈍的問題,但我如何使用它來輸出它?我試圖在這裏搜索一些,但它沒有多大意義:/ –

相關問題