Я делаю свой первый плагин, и у меня проблема с отображением моего короткого кода. Он отображается в верхней части все время, но я прочитал некоторые о 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();
Полный код для функции SHORTCODE:
<?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 Привет. Я добавил полный код для короткого кода. Может ли быть что-то с темой, которую я пытаюсь подключить к плагину? – xatroN
Если вы включите режим отладки, (Change 'define ('WP_DEBUG', false)' to 'define ('WP_DEBUG', true)' в wp-config.php) вы получаете какие-либо ошибки? – George