2014-03-04 65 views
0

我需要創建一個自定義的短代碼,以允許我爲給定類別中的頂級帖子動態填充3個值(帖子標題,發佈日期,鏈接到帖子)。Wordpress Shortcode在類別中的最新帖子

我想的是一樣的東西:[top_post貓= 「5」]

它將輸出是這樣的:

<div class="three_fourth "> 
<h2>[POST_TITLE] - [POST_DATE]</h2> 
</div> 
<div class="one_fourth last"> 
<a class="button" href="[LINK_TO_POST]" style="background-color: #c62b02"> 
</div> 

這是可行的?

感謝您的幫助。

回答

0

在functions.php中,這樣的事情...

function shortcodeFunction($atts) { 
    extract(shortcode_atts(array(
    'cat' => '' //the attr in the shortcode 
), $atts)); 
    if($cat){ //$cat is extracted from $atts 
     $args = array(
     'category' => $cat, 
     'orderby' => 'post_date', 
     'order' => 'DESC', // Show only latest, rejig as needed 
     'posts_per_page' => 1 // Only show 1 
    ) 
    $allPosts = get_posts($args); 
    $return = ''; 
    foreach($allPosts as $p){ // Bog standard get post foreach 
     $thePost = get_post($p); 
     $date = $thePost->post_date; 
     $date = strtotime($date); 
     $date = gmdate('jS F Y',$date); 
     $link = get_permalink($thePost->ID); 
     $return .= '<div class="three_fourth "><h2>'.$thePost->post_title.' - '.$date.'</h2></div><div class="one_fourth last"><a class="button" href="'.$link.'" style="background-color: #c62b02"></div>'; 
    } 
} return $return; 
} else { 
    return false; 
} 
add_shortcode('top_post', 'shortcodeFunction'); 
+1

我不得不做出一些調整,因爲它不會加載按原樣,但這工作。謝謝!! –

+0

太棒了!聽到那個消息很開心! – ggdx

相關問題