I’m trying to create an advanced WordPress page that shows the regular content, plus a second section on the same page that shows the latest posts from X tag. For example, a page is created about “cats”, has pictures, content, etc. Afterwords, I’d like to display the last X posts tagged with “cats”. Is that possible without using plugins, and without creating a custom page template for every page?
Perhaps it’s doable with the custom fields feature and multiple loops. E.g. get X amount of posts from a custom field with the value “cats”. I’ve been struggling with it for a while now, can can’t seem to find a solution that doesn’t involve create a template for every single page.
Thanks!
This should do the job. It’ll check for posts that have been tagged with the title of the current page. There’s no need to use
if (have_posts()):
on a page template: if the template is called it will be because there is a post 🙂If you’d rather use custom meta to add the ‘tag’ to the page (instead of just using the title) you could add a custom meta box to the page like so. Add the following code to your functions.php (nb: this will add the meta box to all pages)
Then your page.php code could look like this:
Do a WP_Query in the second part of your page:
I’m currently working with a theme on WordPress, and i’m actually doing this stuff, so it work !
There are already answers above on how to use loops so I won’t add more about wp get recent posts or Taxonomy Parameters.
But you might want to consider wrapping your code in a Widget which would make adding to your existing templates a little easier.