1

I'm facing a very strange issue when displaying custom loop items (from a new WP_Query) on a static homepage template.

Each item has a thumbnail, title, and some meta info. Every time I use the_post_thumbnail() to show the featured image the current post ID from the loop is set to the page ID that is used for the static page template.

Everything I output after the featured image shows the current page properties. If I move the featured thumbnail code at the end of each loop everything I output before shows up correctly.

If I use wp_get_attachment_image_src(get_post_thumbnail_id()) to construct the img tag the issue doesn't appear anymore, but I need the_post_thumbnail() so I can hook later to change the sizes attribute.

Any ideas why this happens?

UPDATE (code added):

This is the code for the static template page:

<?php

/**
 * Template Name: Magazine homepage
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 */

get_header();

do_action('ultraquick_after_header');

if (is_paged()) {
    if (function_exists('ultraquick_breadcrumbs')) {
        ultraquick_breadcrumbs();
    }
}

if (is_front_page() && !is_paged() && in_array(get_theme_mod('ultraquick_featured'), array('featured-5', 'featured-6'))) : ?>

    <div class="featured">
        <div class="featured-wrapper">
            <?php get_template_part('template-parts/featured'); ?>
        </div>
    </div>
    <!--/.featured-->

<?php endif; ?>

<div id="content" class="site-content">

    <div id="primary" class="content-area">

        <main id="main" class="site-main">

            <?php if (!is_paged()) { // If on the first page show template contents 
            ?>
                <div class="magazine-homepage-widgets">

                    <?php if (is_active_sidebar('homepage-1')) { ?>
                        <div class="magazine-homepage-posts homepage-1">
                            <?php dynamic_sidebar('homepage-1'); ?>
                        </div>
                    <?php } ?>

                </div>

            <?php }

            if (is_front_page()) { // Only if page is set as static homepage

                if (get_query_var('paged')) {
                    $paged = get_query_var('paged');
                } elseif (get_query_var('page')) {
                    $paged = get_query_var('page');
                } else {
                    $paged = 1;
                }

                $posts = new WP_Query(array('post_type' => 'post', 'paged' => $paged));
                $wp_query = $posts; // Replace main query

                if (is_paged() && have_posts()) { // If on the second page of the static homepage
                    /* Start the Loop */
                    while (have_posts()) :
                        the_post();

                        /*
                        * Include the Post-Type-specific template for the content.
                        * If you want to override this in a child theme, then include a file
                        * called content-___.php (where ___ is the Post Type name) and that will be used instead.
                        */
                        get_template_part('template-parts/content', get_post_type());

                    endwhile;
                }
                wp_reset_postdata();

                // show pagination.
                get_template_part('include/pagination');
            }
            ?>

        </main><!-- #main -->
    </div><!-- #primary -->

    <?php get_sidebar(); ?>

</div><!-- #content -->
<?php get_footer();

And this is the code for the widgets I'm using:

$custom_query_args = array(
            'post_type' => 'post',
            'posts_per_page' => $number_posts,
            'post_status' => 'publish',
            'ignore_sticky_posts' => $ignore_sticky,
            'category__in' => $block_category,
            'tag__in' => $block_tag,
            'order' => $order,
            'orderby' => $orderby,
        );

        $custom_query = new WP_Query(apply_filters('widget_block_posts_args', $custom_query_args));
        if ($custom_query->have_posts()) {
            $count = 0;

            echo str_replace('class="', 'class="' . $layout_block . ' ', $args['before_widget']);

            $widget_title = ultraquick_widget_magazine_title($block_category, $block_tag, $title);
            if ($widget_title) { ?>
                <div class="posts-block-title-wrapper">
                    <?php
                    echo $args['before_title'];
                    echo '<div class="posts-block-title">';
                    if (!$widget_title['link']) {
                        echo  $widget_title['title'];
                    } else {
                    ?>
                        <a rel="term" href="<?php echo esc_url($widget_title['link']); ?>"><?php echo esc_html($widget_title['title']); ?></a>
                    <?php
                    }
                    echo '</div>';
                    echo $args['after_title'];
                    ?>
                </div>
            <?php } ?>

            <div class="posts-block <?php echo esc_attr($instance['layout']); ?> col-<?php echo esc_attr($instance['column']); ?>">

                <?php
                while ($custom_query->have_posts()) : $custom_query->the_post();
                    $count++;
                    if ((strpos($layout, 'first-row-large') !== false) && $count == 1) : $large = "article-large";
                    else : $large = "article-small";
                    endif; ?>
                    <article <?php post_class($large); ?>>

                        <div class="archive-thumbnail">
                            <?php the_post_thumbnail(); ?>
                        </div>

                        <div class="archive-meta">
                            <div class="entry-header">
                                <?php if ($meta) { ?>
                                    <div class="entry-meta">
                                        <?php
                                        ultraquick_posted_on();
                                        ultraquick_posted_by();
                                        echo '<br/>';
                                        ultraquick_posted_in();
                                        ultraquick_post_comments();
                                        ?>
                                    </div>
                                <?php } ?>

                                <?php the_title('<h2 class="entry-title"><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h2>'); ?>

                                <?php if ($excerpt) { ?>
                                    <div class="entry-summary">
                                        <?php
                                        the_excerpt(sprintf(
                                            wp_kses(
                                                /* translators: %s: Name of current post. Only visible to screen readers */
                                                __('Continue reading<span class="screen-reader-text"> "%s"</span>', 'ultraquick'),
                                                array(
                                                    'span' => array(
                                                        'class' => array(),
                                                    ),
                                                )
                                            ),
                                            get_the_title()
                                        ));
                                        ?>
                                    </div><!-- .entry-summary -->
                                <?php } ?>
                            </div>
                        </div>

                    </article>
                <?php
                endwhile; ?>

            </div> <!-- .posts_block -->

        <?php echo $args['after_widget'];

            // Reset the global $the_post as this query will have stomped on it
            wp_reset_postdata();
        } // end if posts
    }
4
  • Please add your code.
    – Elex
    Commented Dec 24, 2020 at 14:35
  • I've added most of the code in question. It's a static homepage template that displays widgets with a collection of posts.
    – figarro
    Commented Dec 24, 2020 at 15:00
  • Being honest, can't find way it doesn't work. Quick test for you echo get_the_post_thumbnail(get_the_ID()) might replace your `the_post_thumbnail()'
    – Elex
    Commented Dec 24, 2020 at 16:40
  • It doesn't seem to work. No matter what I do if I use the native function to output the thumbnail the current loop post ID gets reset to the page template ID. If I construct the img tag myself and grab the image src with a native function it works, the current post ID stays the same, but I need the other functions so I can use the hooks for sizes. It's pretty weird this behavior and don't know what to debug next.
    – figarro
    Commented Dec 24, 2020 at 16:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.