0
votes

The code that I'm working on resides in a Twenty Seventeen's child theme, on content-front-page.php. I'm trying to get the most recent posts's featured image in each category (I have three categories) to display in a certain way. Shown below:

enter image description here

Originally, in each colored block. I had this in a php block: <?php

        $recentport = new WP_Query(array('category_name' => 'ports-and-terminals', 'numberposts'=> 1, 'posts_per_page'=> 1));

        while($recentport->have_posts()): $recentport->the_post(); 
        $urlp = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
     ?>  
     <div style="height: 250px; position: relative">
         <div style="height:inherit; background: url('<?php echo $urlp;?>'); background-size:cover; "> <!--i-->
         <div id="img-overlay" style="height: inherit; background: linear-gradient(#0000ff, #000066); opacity: .7;">
         </div>

             <span class="feat-title-link">
                <a href="<?php the_permalink(); ?>">
                  <?php the_title_limit(75, '...'); ?>
                </a>
             </span>

         </div>
       </div>
     <?php
        endwhile;
        wp_reset_postdata();
     ?>

The featured images populated with the above code. If I add a new post in wordpress and set it to a category with the code above, the new post appears underneath or above the set category, like: enter image description here

That is not what I want. I want to keep the layout in the 1st picture. So...I changed the code in one color block to this to test: <?php

$args = array('category_name' => 'ebusiness', 'numberposts'=>'1');
     $recentcust = wp_get_recent_posts($args);
     foreach( $recentcust as $post ){
      $linkid = get_permalink($post["ID"]); 
      $thumb_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' );
      $thumb_url = $thumb_url[0];
    echo '<a href="' . $linkid . '">' .$post["post_title"].'</a>';
    echo (!empty($thumb_url)) ? $thumb_url : 'No thumb!';
    }
    wp_reset_query();       
    //$recentebiz = new WP_Query(array('category_name' => 'ebusiness', 'post_per_page'=>1));

    //while($recentebiz->have_posts()): $recentebiz->the_post(); 
    //$url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
  ?>  
   <div id="recentebiz-tile" style="background: url('<?php echo $thumb_url;?>');">
   </div>

The above code does not populate the featured image of the recent post in each category. Hence, my problem. Here's my logic:

  1. $args = array();: $args variable holds the parameters for the next line.
  2. $recentcust = wp_get_recent_posts($args): $recentcust variable holds the results of *wp_get_recent_posts* query with my set of parameters.
  3. foreach( $recentcust as $post ){} loops through the results and separates them into $post.
  4. Inside the foreach(){} loop: $linkid = get_permalink($post["ID"]): $linkid is the link with the $post's ID.
  5. Inside the foreach(){} loop: $thumb_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'thumbnail' ): gets the featured image that is associated with the $post's ID?
  6. Inside the foreach(){} loop: $thumb_url = $thumb_url[0]: gets the 1st picture (with the array position at 0)?
  7. Outside of the foreach(){} loop: echo $thumb_url[0] to display featured image.

But it's not displaying it. I want the featured image to display in each code block. I know I'm going about this the hard way, but I want to know how the code and Wordpress works. Am I missing something? Thanks in advance.

Resources I've used are to come up with this code:

3

3 Answers

0
votes

I've figured it out...somehow. For future reference, I got the code snippet from here. Then changed it to this:

<?php 
$args = array('category_name' => 'ebusiness', 'numberposts'=>'1');

$posts = get_posts( $args );

foreach($posts as $post) {
$thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array('220','220'), true );
$thumbnail_url = $thumbnail_url[0];
echo ( !empty($thumbnail_url) ) ? $thumbnail_url : 'No thumb!';
}
wp_reset_query();
?>

Then I used another foreach loop to bring back the permalink and the post title with the same args parameters in a different query.

I just wanted get help in understanding the functions involved bringing back pictures. I know there's a better way of doing this. Just need help figuring that out now.

0
votes

In this code of yours

   $recentport = new WP_Query(array('category_name' => 'ports-and-terminals', 'numberposts'=> 1, 'posts_per_page'=> 1));

    while($recentport->have_posts()): $recentport->the_post(); 
    $urlp = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' );
 ?>  
 <div style="height: 250px; position: relative">
     <div style="height:inherit; background: url('<?php echo $urlp;?>'); background-size:cover; "> <!--i-->
     <div id="img-overlay" style="height: inherit; background: linear-gradient(#0000ff, #000066); opacity: .7;">
     </div>

         <span class="feat-title-link">
            <a href="<?php the_permalink(); ?>">
              <?php the_title_limit(75, '...'); ?>
            </a>
         </span>

     </div>
   </div>
 <?php
    endwhile;
    wp_reset_postdata();
 ?>

As what can I see, you are getting ports-and-terminals latest post then display it within HTML, and the while loop continues doing so.

What I think you should do is closing While loop after getting the recent post immediately.

0
votes

You can do it all at once like so...

<?php 
$args = array('category_name' => 'ebusiness', 'numberposts'=>'1');
$posts = get_posts( $args );
foreach($posts as $post) {
$thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array('220','220'), true );
?>
<div style="height: 250px; position: relative">
 <div style="height:inherit; background: url('<?php echo $thumbnail_url[0];?>'); background-size:cover; "> <!--i-->
 <div id="img-overlay" style="height: inherit; background: linear-gradient(#0000ff, #000066); opacity: .7;">
 </div>

     <span class="feat-title-link">
        <a href="<?php get_permalink($post->ID); ?>">
          <?php echo wp_trim_words( $post->post_title, 5, '...' );?>
        </a>
     </span>

 </div>
</div>
<?php
}
?>