2014-08-28 94 views
0
function Service_shortcode($atts , $content = null){ 
     extract(shortcode_atts(array(
     'title'=>'', 
     ),$atts 
     )); 
     $q = new WP_Query(
      array('posts_per_page'=>3, 'post_type'=>'services') 
      ); 

      $list = ' 
       <h1>'.$title.'</h1> 
       <ul> 
      '; 
      while ($q->have_posts()) : $q->the_post();   
       $list.=' 
        <li> 
         <h3>'.get_the_title().'</h3> 
         <h6>'.the_category(',').'</h6> 
         <p>'.get_the_content().'</p> 
        </li> 
       '; 
      endwhile; 
      $list.='</ul>'; 
      wp_reset_query(); 

     return $list; 
      } 
     add_shortcode('service','Service_shortcode'); 

當使用它主題時,它顯示類別名稱,但在短代碼中使用它時不起作用。哪些代碼用於通過簡碼顯示類別名稱。如何通過簡碼顯示wordpress類別名稱?

回答

1

試試這個代碼:

function categories_list_func($atts){ 
    $categories = get_the_category(); 

     if($categories) { 
      foreach($categories as $category) { 
       $output .= '<li class="cat-' . $category->cat_ID . '"><a href="'.get_category_link($category->term_id).'" title="' . esc_attr(sprintf(__("Read more posts from : %s"), $category->name)) . '">'.$category->cat_name.'</a></li>'; 
      } 
      $second_output = trim($output); 
      } 
      $return_string = '<h4>'.__("Categories :", "my_site").'</h4><div class="overflow"><ul class="post-categories">' . $second_output . '</ul></div>'; 

    return $return_string; 

    } // END Categories 
    add_shortcode('categories-list', 'categories_list_func'); 
相關問題