1

So, I'm taking my first shot at creating my own custom Wordpress theme with Underscores. I have everything straightened out, with the exception of one last thing I want to add.

I would like to add a latest/recent posts section to my front page. I know there's a lot of answers out there on how to do this, but I need to do it differently.

I noticed that the most common way is to create a static page template for the front page and and insert your own php code. This is the way everyone seems to suggest when someone asks. However, this won't work for me because of the way that I designed the site for the editors. The front page needs to be editable, so telling anyone to open up a php file and shift stuff around won't cut it. Instead, I've set it up so all front page can be changed through "edit page" in the admin panel. I have also created shortcodes for specific modules that allows the user to reflow the page without breaking the site. I want to do this same thing for recent posts, with a twist.

I came across a plugin called "display-posts" which seems to do what I describe, but not perfectly. This plugin creates a shortcode that shows any posts you want where you insert [display-posts]. What I don't like about this plugin is how it inserts the post using its own HTML fomatting, and not the same code that is in the template's content.php.

I styled the post layout in content.php to be exactly as I want it to display on the blog page. I would like this same layout to be called in a shortcode on a static page where I want to display the recent posts. Essentially, I think want a shortcode that returns the main loop in index.php (which in turn does get_template_part), but limits to 3 posts.

I looking for others who wanted to do what I described, but I only came across about two people asking for something similar, and no solution. I made a brief attempt at creating my own shortcode but it only had the effect of looping the content of the static page itself, not the latest posts.

If anyone could help, it would be greatly appreciated. Since this is my first WordPress theme, I'm still a novice, and I don't have a ton of php experience as well. Enough to get by with basic things, but not enough to figure this out on my own.

Many thanks

  • 1
    The WP_Query Codex page has examples of creating additional queries, and the Shortcode page will get you started on that end. You'll be more likely to get help if you edit your question and show an attempt at solving your problem. – Milo Jan 31 '16 at 21:11
  • Alright well, I took a look at the WP_Query Codex page and I realized that it's exactly how the display-posts shortcode plugin works. So I took another quick look, but rather than starting from scratch I made a small modification to the display-posts template. After the query I found a loop where it builds the output and added get_template_part( 'template-parts/content', get_post_format() ); from index.php into it. This now outputs the posts as I want, BUT for some reason it's not inserting it at the position of the shortcode. The element is being inserted at the very top of the page. – mr_bones Jan 31 '16 at 23:07
  • Due to the character limit I can't post the entire thing here, but this is the part I'm referring to: while ( $listing->have_posts() ): $listing->the_post(); global $post; get_template_part( 'template-parts/content', get_post_format() ); So now I just wonder if anyone knows why the <article> element is being inserted at the wrong location, yet still inside of the <main> element. – mr_bones Jan 31 '16 at 23:15
  • Shortcodes have to return their content, not echo or print directly to the page. You can edit your question to add code there. If you want to use the same template, look into PHP output buffering to capture template output for a Shortcode. – Milo Feb 1 '16 at 0:36
  • Thank you! That solved it. When I first read your comment, I looked at it as if you had two heads. I noticed in the "related" sidebar that there was another thread about the same issue with shortcodes outputting to the top of the page. After a little more searching I found this themeavenue.net/using-html-shortcode-function which I followed and now it works perfectly. Now I want to see about stripping down this display-posts plugin to its essential parts so it's not so bloated, but other than that it's working as intended. – mr_bones Feb 1 '16 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.