Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Featured Image from Blog Post as Background on Front Page Section

I want to create a section on a website with a featured image as the background. Over the image I will display the blog title, author, comments (if any) and a "Read More" link. I want to use an array that loops but I am getting the "white screen of death".

Any help or suggestions would be greatly appreciated :)

Rhoda

Here is the code:

<?php
        $featured_post = array();
        $args = array( 'post_type' => get_the_id());
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post();
            $featured_post[get_the_id()] = array(
                    'title' => wp_title(),
                    'excerpt' => get_the_excerpt(),
                    'date' => get_the_date(),
                    'author' => get_the_author(),
                    'comments' => get_comments(),
                    'image' => get_attachment_url( get_post_thumbnail_id($post->ID) ),
                    'link' => wp_get_field('learn_more_link', $post->ID),
                );
        endwhile;
        wp_reset_query();
        ?>

        <div class="featured-blog-wrapper">
            <ul>
            <?php foreach ($featured_posts as $featured_post_details): ?>
                <li class="<?php echo ($tab_count == 0) ? 'active' : '' ?>" data-page-id="<?php echo $page_id ?>"><?php echo $featured_post_details['title'] ?></li>
            <?php $featured_post_count++; endforeach ?>
            </ul>
        </div>

    </div>
    <div class="featured-blog">
        <?php $featured_post_count = 0; ?>
        <ul class="featured-blog-items">
        <?php foreach ($featured_post as $page_id => $featured_post_details): ?>
            <li class="<?php echo ($featured_post_count == 0) ? 'active ' : '' ?>$featured_post-panel" data-page-id="<?php echo $page_id; ?>">
                <?php if ($featured_post_details['comments'] != ""): ?>
                <div class="comment-wrapper">
                    <?php echo $featured_post_details['comments'] ?>
                </div>

                <?php if ($featured_post_details['image'] != ""): ?>
                <div class="image-wrapper" style="background-image: url(<?php echo $image[0] ?>);"></div>
                <?php endif ?>

                <div class="excerpt-wrapper"><?php echo $featured_post_details['excerpt'] ?></div>
                <?php if ( $tab_details['link'] != "" ): ?>
                <a href="<?php echo $featured_post_details['link']; ?>" class="button">Continue Reading</a>
                <?php endif ?>
            </li>
        <?php $featured_post_count; endforeach ?>
        </ul>
    </div>

    </section>

Dave McFarland, Zac Gordon Please and Thank you :)

5 Answers

Rhoda,

A lot of what you're doing can be done through the standard loop that already exists on the main blog home.php. For example:

<div class="row">
  <?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
    <?php
      $thumbnail_id = get_post_thumbnail_id( $recent[ID] );
      $thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'large' );
    ?>
  <a class="col-4 text-center" href="<?php the_permalink(); ?>">
    <div class="blog-link">
      <div class="blog-image" style="background: url('<?php echo $thumbnail_url[0]; ?>') no-repeat 50% 50%;"></div>
    </div>
    <article class="post text-center">
      <h2><?php the_title(); ?></h2>
      <h3><?php the_category(', '); ?></h3>
      <p><strong>
        Written by <?php the_author_posts_link(); ?><br>
        <?php echo the_time('l, F jS, Y'); ?><br>
      </strong></p>
    </article>
  </div>
  <?php endwhile; endif; ?>
</div>

This is what I use to grab the featured image thumbnail and set it as the background of an absolutely positioned div. In this case I have it as a separate div because I do specific hover effects on just that div that I don't want happening on the content itself. No I know this doesn't get you the comments, but notice that I'm not having to do anything with a custom query here just to get this information from the primary loop. If you look at the built in themes you can see how they use comments_popup_link to output the comments count which you could use in the loop.

<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>

Again, adding the comments will take a little more research, but maybe this will give you some guidance in another way you could be attempting your solution?

Rhoda,

It seems you're solution might be more complex than it needs to be. Without necessarily suggesting unnecessary refactors though, I did notice that the following statement doesn't close your if condition.

<?php if ($featured_post_details['comments'] != ""): ?>
  <div class="comment-wrapper">
    <?php echo $featured_post_details['comments'] ?>
  </div>

Give that a shot and see if that gets you out of your white screen of death.

Tim,

Thank you for that! The white screen of death vanished. I still have an error report for this section:

 <div class="featured-blog-wrapper">
            <ul>
            <?php foreach ($featured_posts as $featured_post_details): ?>
                <li class="<?php echo ($tab_count == 0) ? 'active' : '' ?>" data-page-id="<?php echo $page_id ?>"><?php echo $featured_post_details['title'] ?></li>
            <?php $featured_post_count++; endforeach ?>
            </ul>
        </div>

And unfortunately it's not pulling the blog information. I need the featured image of a blog post to be my background and then pull the title, date, and comments so I can float them over the image on the front page.

I'm not sure how else to pull the blog post and still make the image the background. Suggestions would be greatly appreciated :)

Rhoda, I have some similar code I've written that I'll post from my computer shortly (I'm on the road currently). I don't have all comments loops in my example just the comment count, but maybe it'll get you started.

Tim,

Thank you for the code. I will play with it and see what I can get it to do :) Comments seem to be a little more problematic! I might just save those until I have figured out the background and adding the other info.

I'll keep you posted :)

Rhoda

Tim,

Thank you so much for your help :) I got it working...and it was so much simpler than the mess I had going...I'm hoping it will make it past the final inspection, but so far so good!

For anyone wondering...or needing to see how it worked out, here is the code I finished with:

<section id="featured-post" class="blog-post">
            <div class="row">
              <?php query_posts('posts_per_page=1'); ?>
                  <?php while ( have_posts() ) : the_post(); ?>


                       <div class="blog-image" style="background: url('<?php 
                                if ( has_post_thumbnail() ) {$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
                                echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '" >';
                                echo get_the_post_thumbnail( $post->ID, 'full' ); 
                                echo '</a>'; } ?>') no-repeat;">
                        </div>

                            <article class="post text-center">
                              <h3><?php the_title(); ?></h3>        
                              <h3><?php the_author_posts_link(); ?></h3>
                              <h3><?php the_excerpt(', '); ?></h3>
                            </article>

                            <div class="more-button">
                                <a class="read-more-link" href="<?php the_permalink(); ?>">Continue Reading</a>
                            </div>

                      </div>
                  <?php endwhile; // end of the loop. ?>
                <?php wp_reset_query(); // resets the altered query back to the original ?>
            </div>
        </section>

:) Rhoda

Excellent Rhoda! I'm glad you were able to get something going. Good luck with it!