2011-12-02 67 views
1

我正在寫一個自定義的簡短代碼,用於放入我的子主題functions.php中的childtheme。這裏的代碼:WordPress:WP_Query在functions.php中返回空查詢(用於自定義簡碼)

function get_featured_video(){ 
$video_query = new WP_Query('category_name=videos&order=ASC'); 
$videoPath = "/"; 
$videoText = ""; 
while ($video_query->have_posts()){ 
    $video_query->the_post(); 
    $featured = get_post_meta($post->ID, "vid_feature", $single = true); 
    if(strtolower($featured)=='yes'){ 
     $videoPath = get_permalink($post->ID); 
     $content = strip_tags($post->post_content); 
     $contentArr = str_word_count($content,1); 
     if(count($contentArr>50)){ 
      $videoText = join(" ",array_slice($contentArr,0,50)); 
      $videoText .= " <a href='$link'>&lt;read more&gt;</a>"; 
     } else { 
      $videoText = $content; 
     } 
     break; 
    } 
} 
$returnStr = "<h1><a href='$videoPath'>You've Got to See This!</a></h1>\n"; 
$returnStr .= $videoText; 
return $returnStr; 
} 

add_shortcode('getfeaturedvideo','get_featured_video'); 

我遇到的問題是,它返回一個空白查詢。我知道視頻類別中有一個帖子。我從來沒有在functions.php中使用WP_Query。我需要使用不同的方法嗎?

回答

5

在函數的頂部嘗試聲明:

global $post; 
+1

沒錯。一點前意識到這一點,我忘了發佈。感謝您的回答。 – LoneWolfPR

+0

沒問題:)總是覺得自己也要好好解決它。 – CookiesForDevo

+0

感謝您的回答,也被困在這一個。 – deflime