Zarbat
Zarbat

Reputation: 465

Problem to use correctly WP_Query to make some stuffs

i'm studying and developing my first wordpress theme starting from my bootstrap and js web site that i created.

My need are:

1) include in homepage wordpress site a section to insert the last three blog articles with only thumbnail and title foreach one.

2) Both of thumbnail and post title need to have a link "a href" to the post blog.

I have already created all and actually my section is

<section id="counter" class="parallax-section" style="background-position: 50% 0px;">
        <div class="container">
            <div class="row">
                <div class="col-md-12 title text-center my-5">
                    <h2 class="bord">The History Of <span class="color"><?php bloginfo('name'); ?></span></h2>

                </div>
                <?php
                $categories = get_the_category();
                $category_id = $categories[0]->cat_ID;
                $esi_query = new WP_Query( array( 'cat' => $category_id,'posts_per_page' => '3' ) ); while($esi_query->have_posts()) : $esi_query->the_post(); ?>

                    <div class="col-md-4 col-sm-6">
                        <div class="member-photo">
                            <?php echo the_post_thumbnail();?>
                            <div class="member-title">
                                <h5><?php the_title()?></h5>
                            </div>
                        </div>
                    </div>

                <?php endwhile; ?>
                <?php wp_reset_postdata(); // reset the query ?>


            </div>
        </div>
    </section>

in this way i can have a loop with 3 last articles for a cert category. To do that thumbnail have link to post page i added in functions.php file this

function esi_post_thumbnail( $html, $post_id) {

    $html = '<a href="' . get_permalink( $post_id ) . '" title="' . esc_attr( get_post_field( 'post_title', $post_id ) ) . '">' . $html . '</a>';
    return $html;
}
add_filter( 'post_thumbnail_html', 'esi_post_thumbnail', 10, 3 );

that's working correctly, so i have a link for every thumbnail...

how to have the same result even for the title post ? i'm going out of mind to solve it and i don't know if i need to solve in a better way only with WP_Query function.

Thank you for all suggestions and help. Regards

Upvotes: 0

Views: 39

Answers (2)

Peter Breen
Peter Breen

Reputation: 447

Instead of using echo the_post_thumbnail(); I'd try writing out something manually that does exactly what you want it to do. I'd try something like the following.

<a href="<?php the_permalink() ?>" title="<?php esc_attr( get_post_field('post_title', $post_id ) )?>" >
    <img src="<?php the_post_thumbnail_url() ?>"/>
    <h5><?php the_title()?></h5>
</a>

Upvotes: 1

Mohammad Ashique Ali
Mohammad Ashique Ali

Reputation: 588

If you are willing to add title with anchor on title then,

<h5><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h5>

This should work for you.

Upvotes: 1

Related Questions