2016-07-24 133 views
0

我試圖動態地爲自定義分類術語創建簡碼。但是沒有這樣做。 假設,如果有一個名爲「wordpress」的術語,那麼我應該能夠通過簡碼查詢與該術語相關的所有帖子。 更準確地說,假設有一個稱爲「事件」的分類法,並且在該分類法下有多個術語。所以,我試圖通過每個術語的簡碼來查詢每個術語下的帖子。WordPress短代碼術語

這裏是我的嘗試:

function wordpress_recent_post($atts, $content) { 
    $a = shortcode_atts(array(
    'cat' => '', 
), $atts); 
    $args = array(
    'posts_per_page' => 1, 
    'offset' => 0, 
    'category_name' => $a['cat'], 
    'orderby' => 'post_date', 
    'order' => 'DESC', 
    'post_type' => 'post', 
    'post_status' => 'publish', 
    'ignore_sticky_posts' => true, 
); 
    $recent_posts = new WP_Query($args); 
    ob_start(); 
    if (! $recent_posts-> have_posts()) { 
    return 'No Posts Found for ' . $a['cat']; 
    } 
    while ($recent_posts->have_posts()) { 
    $recent_posts->the_post(); 
    the_title('<h2>', '</h2>'); 
    if ('' != $a['cat']) { 
     $href = '/category/' . $a['cat']; 
    } else { 
     $href = '/blog'; 
    } 
    echo "<p><a href='$href'>Read More" . ucwords($a['cat']) . '</a></p>'; 
    } 
    wp_reset_query(); 
    return ob_get_clean(); 
} 
add_shortcode('wordpress_recent_post', array($this, 'wordpress_recent_post')); 

然後我用這個來調用一個叫「特色」,其ID是「183」(想) [wordpress_recent_post貓長期職位=「183」 ]

任何幫助將非常可觀。

謝謝!

回答

1

添加term slug做到了。應該有slug不是id,像這樣: [wordpress_recent_post cat =「features」]