我正在製作我的第一個插件,並且在顯示短代碼時遇到了問題。它始終顯示在頂部,但我已閱讀了一些關於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');
請出示你的整個功能,因爲它代表這應該工作完全正常。 – George
@George Hello - 我已經添加了我的簡碼完整代碼。它可能是我試圖插入的主題的東西? – xatroN
如果啓用調試模式,(在wp-config.php中將'define('WP_DEBUG',false)'更改爲'define('WP_DEBUG',true)')是否會出現錯誤? – George