• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Shortcode to display latest post from one category

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Shortcode to display latest post from one category

This topic is: resolved

Tagged: shortcode

  • This topic has 4 replies, 3 voices, and was last updated 11 years, 6 months ago by wyattwerner.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • April 24, 2014 at 11:39 pm #102269
    jodzeee
    Member

    I’m trying to display the most recent post from one category on a page. I thought I’d try to do it without a plugin and I’m close … but where do I define which category?

    function recent_posts_function() {
       query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => 1));
       if (have_posts()) :
          while (have_posts()) : the_post();
             $return_string = '<div class="coupon">'.get_the_content().'</div>';
          endwhile;
       endif;
       wp_reset_query();
       return $return_string;
    }
    
    function register_shortcodes(){
       add_shortcode('recent-posts', 'recent_posts_function');
    }
    
    add_action( 'init', 'register_shortcodes');
    
    April 25, 2014 at 5:14 am #102284
    Sridhar Katakam
    Participant

    query_posts() is not recommended.

    You might want to try http://wordpress.org/plugins/display-posts-shortcode/.


    Genesis Tutorials | Follow me on Twitter

    April 25, 2014 at 9:30 am #102319
    jodzeee
    Member

    I have a lot of plugins already installed and I'm trying to avoid using another one. Is there another way to create a shortcode in functions.php without using query_posts()?

    April 25, 2014 at 3:30 pm #102378
    jodzeee
    Member

    Okay, I think I've got it!

    /** Creates shortcode for recent posts */
    
    function recent_posts_shortcode() {
    
    $args = array(
       'category_name' => 'coupons',
       'posts_per_page' => '1',
     );
    $query = new WP_Query($args);
    
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    		return '<div class="coupon">' . get_the_content() . '</div>';
    	}
    } else {
    }
    
    wp_reset_postdata();
    }
    
    function register_shortcodes(){
       add_shortcode('recent-posts', 'recent_posts_shortcode');
    }
    
    add_action( 'init', 'register_shortcodes');

    One issue with it is that if I change the number of posts per page, it doesn't show more than one. In this case, it doesn't matter because I only want one, but I'm curious why it doesn't work. If I use "echo" instead of "return" it does work. However, it also puts the post above my content.

    July 29, 2014 at 12:45 pm #116243
    wyattwerner
    Member

    I'm liking this. How do I get the post title to show up?

  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble