Having difficulty utilizing WP_Query effectively for accomplishing certain tasks

Currently, I am diving into the world of WordPress theme development by adapting my existing Bootstrap and JavaScript website.

My requirements are as follows:

1) Implement a section on the homepage of my WordPress site to display the latest three blog articles with just their thumbnails and titles.

2) Both the thumbnail images and post titles should be clickable, linking to the respective blog posts.

I have already crafted everything, and at the moment, my section looks like this:

<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>

By following this structure, I have successfully created a loop showcasing the last three articles for a specific category. To ensure that each thumbnail links to its corresponding post page, I integrated the following code in my functions.php file:

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 );

This solution works perfectly for the thumbnails. However, I am facing a challenge regarding creating clickable links for the post titles as well. I am seeking guidance on how to achieve the same result efficiently, possibly utilizing the WP_Query function.

Any assistance or suggestions would be greatly appreciated. Thank you!

Answer №1

If you're open to including a title with an anchor within the title, then,

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

This method should be effective for your needs.

Answer №2

Instead of simply using the echo the_post_thumbnail(); function, it might be worth considering writing a custom code snippet that specifically suits your needs. You could try something like the code below:

<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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to utilizing an Ajax response object

Here is the code I am using to display data based on values selected from dropdowns: $("#botao-filtrar").click(function(){ $(".mask-loading").fadeToggle(1000); $.ajax({ url: 'datacenter/functions/filtraDashboardGeral.php', as ...

Tips for removing special characters from HTML?

I'm currently grappling with the concept of the filter My situation is as follows: $htmlData includes a user-entered html string formatted like this $htmlData = array('<em>test</em>', 'text here','\n') ...

Exploring the world of Ajax queries in Silex

When I decided to build my new website, I opted to experiment with the Silex framework. Familiarizing myself with its documentation helped me progress smoothly thus far. Currently, I am developing a voting system and intend to incorporate dynamic function ...

The Speed of Apache Requests

After setting up my server with centos6, I decided to use a combination of nginx as the frontend and apache as the backend, along with APC for optimization purposes. To benchmark the performance, I used the following command: ab -n 1000 -c 100 http://doma ...

Discover the seamless method of transferring data from an HTML5 form to a PHP page for processing without the need to refresh the page or manually navigate to the PHP file

When creating a website, I encountered an issue with a form that requires the user to select three options from select elements and then click a search button to search the database. The problem is that PHP does not retrieve the form data unless the button ...

Looking up data in PHP from a set number of rows

I manage a patient table containing over 1000 patient records, each created by one of the 5 admins. Patient records are sorted using the admin_id column, so that when admin "A" logs in, they can only see patients they have created. Now, I want to search fo ...

Refresh PHP variable without having to refresh the entire website

Is there a way to update the variable $online without having to refresh the website? I have attempted the following: data.php <?php include("steam.php"); $data = $online; // get current value of data from somewhere echo $data; // should ...

Enhance the appearance of specific wordpress usernames by adding a touch of "flair" next to them

I'm looking to add a special "flair" next to specific users on my WordPress website, similar to how YouTube distinguishes verified users. I have the CSS for changing the color on hover, but I need help keeping it positioned correctly. Examples from Y ...

The theme font fails to take effect on the paragraph

As a novice in HTML and CSS, I attempted to use the "Roboto Condensed" font from a WordPress theme for my entire site. Although I successfully implemented this custom font for a menu, I encountered difficulties when trying to apply it to paragraph elements ...

Storing user input values in a database using PHP

How can I efficiently calculate the sum of values from multiple input files and then store them in a database for later use? Here are examples of my input fields: <input type="checkbox" name="test[]" value="1"> <input type="checkbox" name="test[ ...

Is cakephp generating tabs as output?

I created a web application using cakephp on my Windows XP system with xampp, and everything worked perfectly. However, when I deployed it to a CentOS and an Ubuntu Server, I encountered a strange issue. The problem is that there seems to be a tab space ad ...

"Unable to retrieve data using simpleXML_load_file. No luck finding answers

Attempting to establish a connection with a server that is currently shut down using the simplexml_load_file() command in a PHP script. When trying from my personal computer utilizing WAMP SERVER, an error message is received: Warning: simplexml_load_ ...

Is it possible to use combox to deactivate the buttons?

My goal is to deactivate the button as soon as I select a value from the drop-down list. For example, here's what I have: <select name =""> <option> disable </option> <option> enable </option> </select> <input ...

PdoServiceProvider not found in Dokku-deployed Silex deployment

After encountering random crashes with socket errors while using the abandoned herrera-io/silex-pdo PDO provider in my Silex project, I switched to csanquer/pdo-service-provider. Everything worked perfectly on my localhost server, but upon deployment to a ...

The increasing number of ajax requests is causing the page to become sluggish and unresponsive

Here is the ajax script that I am currently using: $.ajaxSetup ({ // Disable caching of AJAX responses cache: false }); function getRandomInt() { return Math.floor(Math.random() * Math.pow(10,6)); } $(document).ready(fu ...

Tips on performing a null verification within an end-of-day function in PHP

Not too familiar with PHP, but I'm interested in accomplishing something like this $html = $html . <<<EOD <h10>Quick ID:</h10> if (!empty($row->quickid)) I only want to add that row if it has a value and not null. ...

When the page is refreshed, the POST array does not become empty

I recently developed a PHP form with the action set to the same page URL and method as POST. <?php if(isset($_POST['submitted']) && $_POST['submitted'] != ''){ echo $_POST[&apo ...

Unable to establish a connection with standalone mongodb: No suitable servers were located

My debian development server is equipped with the standard MongoDB as well as PHP 5.4 and PECL MongoDB drivers v1.4.3. Attempting to establish a simple connection using PHP CLI resulted in an exception: php -r "MongoLog::setLevel(MongoLog::ALL); MongoLog: ...

Changing an image into a background image

I currently have functional code that retrieves an image inside a div. However, upon checking it on mobile devices, the image appears too small. When I attempt to increase its height, it becomes disproportionate. As a solution, I am attempting to set the i ...

Creating a table to showcase the outcomes of two different conditions

I am struggling to include two conditional result statements in a table, but I can't seem to make it work. If the form input is empty, the result should not be shown. In order to save space, I would like to organize the results in a table with eithe ...