Forums

[resolved] How to show image of first post in monthly-Archive page (14 posts)

  1. syed544
    Member
    Posted 1 year ago #

    In my monthly Archive.php page, I am showing links of all posts of the month.
    In the very first post of each month, there shall be only one image.
    I want to pull this image and need to show it as a thumbnail on the specific-month archive page, before all the posts-links of the said month.

    How can it be possible?
    Many thanks for kind help.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    How are you creating the monthly archive page? Does this page use a custom template or query of any kind?

  3. syed544
    Member
    Posted 1 year ago #

    @esmi:
    yes, this page (Archive.php) uses a custom template.
    It will show all the posts-links for the particular month category-wise.

  4. MAS
    Member
    Posted 1 year ago #

    use counter variable.

  5. syed544
    Member
    Posted 1 year ago #

    @chinmoy29 :
    How and where?

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    We'd really need to see your code. Drop a copy of the template file into the WordPress pastebin and post the pastebin url here.

  7. syed544
    Member
    Posted 1 year ago #

    @esmi :
    Here is the code
    code - Archive.php

  8. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Looking at the code, there's a slight problem - the sql query that grabs the relevant posts is being set up and used after the page title has been displayed and within the table that is being used to create the link list. So, at the point when you need the image (near the top of the page before the table), the query hasn't even been called. The whole script would have to be re-written and the query moved much higher up to achieve what you want.

    Not a simple fix, I'm afraid

  9. syed544
    Member
    Posted 1 year ago #

    @esmi,
    OK. I got your point.
    Forget about the previous location.
    Can you guide me, as how can I display the image just after the "title" of the post?
    Since it is in a loop, so I can place the code in between IF condition that the image will show only if it is the first post of the month.

  10. syed544
    Member
    Posted 1 year ago #

    just after the "title" of the post, I am using the code as like this:

    <br />
    <?php if (++$post_count == 1) {
    
    $args = array(
    	'post_type' => 'attachment',
    	'posts_per_page' => -1,
    	'post_status' => null,
    	'post_parent' => $post->ID
    	);
    $attachments = get_posts($args);
    if ($attachments) {
    	foreach ($attachments as $attachment) {
    echo wp_get_attachment_image($attachment->ID, $size='medium', $icon = false);
    	}
    }
    }
    ?>

    Its OK. but for some months, the thumbnail is not showing. And instead of this, two thumbnails of two month's first post are displaying in a single month.

    Can anybody check to find the error? Please.

  11. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Assuming that you only want an image from the most recent post, try using:

    $args = array(
    	'post_type' => 'attachment',
    	'posts_per_page' => 1,
    	'post_status' => null,
    	'post_parent' => $post->ID
    	);
  12. syed544
    Member
    Posted 1 year ago #

    Its working fine now. The problem of 2-images was due to two attachments linked to same post.

    now I need to link the image to its post.
    i.e: How can I give the permalink of the post to the image?

  13. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    Try:

    ?>
    <a href="<?php the_permalink(); ?>"><?php echo wp_get_attachment_image($attachment->ID, $size='medium', $icon = false);?></a><?php
  14. syed544
    Member
    Posted 1 year ago #

    Thanks.
    Topic resolved!

Topic Closed

This topic has been closed to new replies.

About this Topic