Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:30:22+00:00 2026-06-12T23:30:22+00:00

I have created a shortcode in my functions.php which is designed to return the

  • 0

I have created a shortcode in my functions.php which is designed to return the 3 latest blog posts (titles, thumbnail, read more link) with a tag of “highlight”. The PHP in my shortcode successfully recognises the tag parameter but it only returns one post.

As you can see, I am using the “posts_per_page” parameter to define the number of posts I want to return. At the moment, when I enter 1, 2 or 3 it simple returns the 1st, 2nd or 3rd post in date order – rather than the 3 latest posts. I have unsuccessfully tried “numberposts” and “showposts” (trial and error!).

Can anyone please tell me what I’m missing here? This is in my functions.php file:

function highlight_posts() {
    $args = array('tag' => 'highlight' , 'posts_per_page' => 4);
    $the_query = new WP_Query( $args );
        while ( $the_query->have_posts() ) : $the_query->the_post();
            $return_string = '<div class="blog-post-highlight"><div class="blog-post-img"><a href="'.get_permalink().'">'.get_the_post_thumbnail($page->ID, array( 300, 168 )).'</a></div><div class="blog-post-content"><h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3><p class="read-more"><a href="'.get_permalink().'">Read More...</a></p></div><div class="clear"></div></div>';
        endwhile;
    wp_reset_postdata();
    return $return_string;
}

add_shortcode( 'highlightposts', 'highlight_posts' );

And this is deployed within the WYSIWYG editor by inserting [highlightposts]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:30:23+00:00Added an answer on June 12, 2026 at 11:30 pm

    try this:

    function highlight_posts() {
        $args = array('tag' => 'highlight' , 'posts_per_page' => 4);
        $return_string ="";
        $the_query = new WP_Query( $args );
            while ( $the_query->have_posts() ) : $the_query->the_post();
                $return_string .= '<div class="blog-post-highlight"><div class="blog-post-img"><a href="'.get_permalink().'">'.get_the_post_thumbnail($page->ID, array( 300, 168 )).'</a></div><div class="blog-post-content"><h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3><p class="read-more"><a href="'.get_permalink().'">Read More...</a></p></div><div class="clear"></div></div>';
            endwhile;
        wp_reset_postdata();
        return $return_string;
    }
    
    add_shortcode( 'highlightposts', 'highlight_posts' );
    

    You were giving new values to the $return_string, instead of adding the new ones.
    Check if it was that

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created several helper functions which I use when creating templates for Wordpress. An
I have created an xPages application which uses a lot of server side javascript
I have a SQL Server table-valued function which is created by me I have
Have created a stored procedure which is utilised for monitoring purposes of a website.
I have created a windows service which is set to start automatically. This service
i have created a running process which listens for input: listen = Popen([home/user/listen], stdout=PIPE,
I have created a custom shortcode and can get the information to output, however
I have created a QWidget with a bunch of QToolButtons in it and I
I have created 3 classes as following Ext.mine.TextParent - Inherting from Textfield Ext.mine.child.TextChildA -
I have created some JQuery that will expand a div 'popup' on hover and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.