2016-01-21 227 views
0

我正在製作我的第一個插件,並且在顯示短代碼時遇到了問題。它始終顯示在頂部,但我已閱讀了一些關於ob_start()的內容。並試圖使用它,但簡碼只是沒有返回。ob_start()與wp_query結合使用

使用下面的代碼 - 它似乎與我的帖子有關。任何人都知道爲什麼如何解決這個問題?

我WP_Query:

$query = new WP_Query(array(
'category__in' => $categories, 
'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0] 
)); 

我用它來顯示它的代碼:

ob_start(); 

echo '<div class="whpost_content">'; 
    while($query->have_posts()) : $query->the_post(); 
     // Get the URL to the attached image. 
     $attached_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'large'); 
     echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">'; 
     the_title(); 
     echo '</div>'; 
    endwhile; 
    echo '</div>'; 

    return ob_get_clean(); 

爲簡碼功能的完整代碼:

<?php 
function cpt_content_func($atts) { 
    // Get the ID we putted in into [wh-postgrid id="THIS_ID"] 
    extract(shortcode_atts(array(
    'id' => null 
), $atts)); 

    // Get stored meta data (For the categories - needed to be formatted in 
    // a certain way) 
    $categories = get_post_meta($id, 'whpost_cats', true); 

    // Get meta data for settings and such. 
    $whpost_stored_meta_shortcode = get_post_meta($id); 

    // Get the correct categories and use the settings we got. 
    $query = new WP_Query(array(
    'category__in' => $categories, 
    'posts_per_page' => $whpost_stored_meta_shortcode['whpost_number_of_posts'][0] 
)); 

    // Set the styles 
    switch ($whpost_stored_meta_shortcode['whpost_posts_per_line'][0]) { 
    case 1: 
     $post_width = '100%'; 
     $post_max_height = ''; 
     break; 

    case 2: 
     $post_width = '50%'; 
     $post_max_height = ''; 
     break; 

    case 3: 
     $post_width = '33.333333%'; 
     $post_max_height = ''; 
     break; 

    case 4: 
     $post_width = '25%'; 
     $post_max_height = ''; 
     break; 

    default: 
     $post_width = '50%'; 
    } 



    // Display the front-end 
    ob_start(); 

    echo '<div class="whpost_content">'; 
    while($query->have_posts()) : $query->the_post(); 
     // Get the URL to the attached image. 
     $attached_image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()), 'large'); 
     echo '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">'; 
     the_title(); 
     echo '</div>'; 
    endwhile; 
    echo '</div>'; 

    return ob_get_clean(); 
} 
add_shortcode('wh-postgrid','cpt_content_func'); 
+0

請出示你的整個功能,因爲它代表這應該工作完全正常。 – George

+0

@George Hello - 我已經添加了我的簡碼完整代碼。它可能是我試圖插入的主題的東西? – xatroN

+0

如果啓用調試模式,(在wp-config.php中將'define('WP_DEBUG',false)'更改爲'define('WP_DEBUG',true)')是否會出現錯誤? – George

回答

0

我認爲問題與您查詢,您可以添加此行來檢查

if (!$query->have_posts()) { 

     return 'Empty result'; 

} else { 

     return ob_get_clean(); 
} 
+0

這不會返回任何內容,將其添加到我的文檔末尾。 @nefro – xatroN

0

函數必須返回輸出。

更改您這樣的代碼,它應該工作:

$output = '<div class="whpost_content">'; 
while($query->have_posts()) : $query->the_post(); 
    // Get the URL to the attached image. 
    $postId = get_the_ID(); 
    $attached_image = wp_get_attachment_image_src(get_post_thumbnail_id($postId), 'large'); 
    $output .= '<div class="whpost_post" style="width: ' . $post_width .'; background-image: url(' . $attached_image[0] . ');">'; 
    $output .= get_the_title($postId); 
    $output .= '</div>'; 
endwhile; 
$output .= '</div>'; 

return $output; 
+0

感謝您的回答 - 它仍然只是顯示爲空白。但是,如果我註釋掉整個循環,只需添加$ output。='test';然後它返回測試。我會試試看另一個主題,看看它是否有效。似乎是一個主題問題@Mario Werner – xatroN

+0

@xatroN不應該是一個主題問題。如果你說它在移除循環後返回輸出,那麼問題可能在那裏? –