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:
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:
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:
- $args = array();:
$args
variable holds the parameters for the next line. - $recentcust = wp_get_recent_posts($args):
$recentcust
variable holds the results of*wp_get_recent_posts*
query with my set of parameters. - foreach( $recentcust as $post ){} loops through the results and separates them into
$post.
- Inside the
foreach(){}
loop: $linkid = get_permalink($post["ID"]):$linkid
is the link with the $post's ID. - 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? - Inside the
foreach(){}
loop: $thumb_url = $thumb_url[0]: gets the 1st picture (with the array position at 0)? - 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: