0

I'm kinda stuck. I want to show 3 sections on an author page. Section one shows all uploaded images by author based on tag Tattoo. Section two shows all uploaded images by author based on tag Piercing. Section three shows all uploaded images by author based on tag Modification. So far so good. This is the code I'm using to do this for one section:

<!--- start test --->

                <div class="uk-grid" data-uk-grid-margin>
                    <div class="uk-width-1-1">
                        <h2>Tattoo Work by <span style="text-transform: capitalize;"><?php the_author_meta('user_nicename'); ?></span>:</h2>
                        <ul id="switcher-content" class="uk-switcher">
                            <li class="uk-active">
                                <div class="uk-grid" data-uk-grid-margin>

                                    <?php // Loop Tattoo
                                    $first_query = new WP_Query('cat=1&author=' . $post->post_author . '&order=DESC&tag=Tattoo&posts_per_page=1000');
                                    while($first_query->have_posts()) : $first_query->the_post(); ?>

                                    <div class="uk-width-medium-1-4">
                                        <a href="<?php the_permalink() ?>?MediaTag=Tattoo">
                                        <?php 
                                            if ( has_post_thumbnail() ) { 
                                            the_post_thumbnail('artiststhumbnailsize');
                                            } 
                                        ?>
                                       </a>
                                    </div>

                                    <?php endwhile;
                                    wp_reset_postdata(); ?>

                                </div>  
                            </li>
                        </ul>
                    </div>
                </div>

                <!--- end test --->`

BUT, I want to display the h2 title per section only if the section has images. To prevent the h2 title is displayed without ever having images below. So I kinda need to pre check the query and I can't find any thing to make this happen. The other two queries are $second_query and $third_query with each their own tag definition. Thanks for the help in advance!

2

You can use the function wp_list_pluck to get the ID of the posts retrieved from the query.

Just move the query before the h2 element and get the posts ID's:

// Set a bool to know if at least one post has post thumbnail.
$has_thumb   = false;
$first_query = new WP_Query('cat=1&author=' . $post->post_author . '&order=DESC&tag=Tattoo&posts_per_page=1000');

// Check if the query have posts.
if ( $first_query->have_posts() ) {
    // Get the posts id's.
    $ids = wp_list_pluck($first_query->posts, 'ID');

    foreach( $ids as $id ) {
        if ( has_post_thumbnail($id) ) {
            $has_thumb = true;
            break; // If at least we have a post thubmnail we don't need to continue.
        }
    }
}

Then you can check if there are thumbnails and show the h2 if so.

<?php if ( $has_thumb ) : ?>
    <h2>Tattoo Work by <span style="text-transform: capitalize;"><?php the_author_meta('user_nicename'); ?></span>:</h2>
<?php endif; ?>
1

Set a variable like $has_images = false; then loop through your posts to only check $has_images = $has_images || has_post_thumbnail(). Now only generate the section if ( $has_images ) { }. Just be sure to rewind the posts before you loop through the query again - https://codex.wordpress.org/Function_Reference/rewind_posts

  • thank you @jgraup never heard of the rewind_post function. Will get into that also! – AKNL Aug 7 '16 at 19:45

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.