2017-03-22 36 views
0

我想創建一個顯示我的職位類型簡碼:advanced_topic但要添加的類別參數如如何將類別參數添加到短代碼?

[高級課題類別=「假釋」]

顯示下唯一主題該類別。

這是我到目前爲止的代碼,但我覺得我失去了一些東西,因爲它不工作......任何幫助表示讚賞。謝謝。

function display_advanced_topics(){ 

    extract(shortcode_atts(array('category_name' => 'criminal-records'), $args)); // $categoryName variable will be initialized to 'criminal-records' if the shortcode is missing the 'category' parameter 

    $string = ''; 
    $args = array(
    'post_type' => 'advanced_topic', 
    'posts_per_page' => -1, 
    'post_status' => 'publish', 
    'category_name' => $categoryName 
    ); 

$query = new WP_Query($args); 
if($query->have_posts()){ 
    $string .= '<div class="advanced-topics">'; 
    while($query->have_posts()){ 
     $query->the_post(); 

     $string .= 
     '<div class="row advanced-topic">' 
        .'<div class="content"> 
          <div class="title"><?php get_the_title(); ?></div> 
         </div>' 
       .'</div>'; 
    } 
      $string .= '</div>'; 

} 
wp_reset_postdata(); 
return $string; 
} 
add_shortcode('advanced-topics', 'display_advanced_topics'); 
+0

您應該閱讀Codex關於如何使用屬性製作簡碼的方法。你的失蹤。 https://codex.wordpress.org/Function_Reference/shortcode_atts – Christina

+0

我做到了,那是我如何開始這個簡碼,但我似乎無法找到我失蹤的東西? –

+0

我想通了!謝謝。 –

回答

0
  1. 始終是很好用

    error_reporting(E_ALL); 
        ini_set('display_error', 1); 
    
  2. 錯誤報價

    ob_start(); 
        get_the_title() 
        $the_title = ob_get_clean(); 
        $string .= 
        '<div class="row advanced-topic">' 
           .'<div class="content"> 
             <div class="title">'. $the_title .'</div> 
            </div>' 
          .'</div>'; 
    

如果get_the_title()打印結果,你必須使用ob_get_clean

+0

嗨,所以它是打印結果,但它只打印所有與初始默認類別「犯罪記錄」的結果。我嘗試了[高級主題類別=「假釋」]來顯示「假釋」類別,但它似乎沒有采取參數。 –

+0

我改進了代碼。您無法將打印結果分配給變量。 (簡化)通過'ob_start()' - 'ob_get_clean()'可以將變量分配給每個打印。 – bato3