0

I've written my first plugin. The intent is to show the image, title and excerpt of the first 6 posts. (It uses a function from Divi, here and there).

function jm_blog_layout($atts) {

    ob_start();

    echo '<div class="featured-posts">';

    query_posts(array('category_name' => "", 'posts_per_page' => "6"));

    $post_index = 0;

    while (have_posts()) : the_post();

    $thumbnail_image = get_thumbnail($width = 333)["thumb"];
    $post_id = get_the_id();
    $post_class = 'featured-post';
    if ($post_index == 3) {
        $post_class = $post_class.' focus-post';
    }

    echo '<a href="'.get_permalink().'">';
    echo '<div class="'.$post_class.'" id="post-'.$post_id.'"style="background-image: url('.$thumbnail_image.');">';
    echo '<div class="overlay">';
    echo '<div class="transformable">';
    echo '<h2 class="vertical-align">'.get_the_title().'</h2>';
    echo '<h3 class="thumb-excerpt">'.get_the_excerpt().'</h3>';
    echo '</div>';
    echo '</div>';
    echo '</div>';
    echo '</a>';

    $post_index += 1;
    endwhile;

    echo '</div>';

    $output = ob_get_contents();
    ob_end_clean();
    return $output;

}

It outputs what I want, followed, strangely, by all of the content (i.e the body of text) of the 6 posts I queried - structured like so:

<article id="post-84" class="post-84 post type-post status-publish format-standard has-post-thumbnail hentry category-tips">
    <div class="entry-content">
        <p>...</p>
    </div> <!-- .entry-content -->
</article>

It is not part of my page design, nor of my code. Where could it be coming from?

  • 3
    Never use query_posts(), use WP_Query() instead. Here is why. – Mayeenul Islam Dec 27 '15 at 7:51
  • Oh. It just wraps a big fat global instance? Ewww. Somebody, sometime thought this was a good idea... – Synesso Dec 27 '15 at 8:00
0

I found it is coming from wp-content/themes/Divi/page-template-blank.php, which is my parent template. I concluded the mutable handwaving of wp_query was confusing the template and, after a search, found this neat flowchart letting me know I should call wp_reset_query. That fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.