Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Different loop in wordpress page

I run a soccer organisation website that has a lot of youth teams. I created a template named 'teams' and created a page for every single team (let's say: U10, U11, U12,...). In the template I've made loop so I can easily add news items concering team news. Sometimes, a team can have specific new items, that only need to be visible for them.

It is not possible for me to make a single page template for every team, because there are over 20 teams. Is there a solid solution to add a different loop to a particular page on one page template?

2 Answers

Hi Bjorn,

as always with WordPress there are multiple solutions to your "problem".

One very simple solution would be the usage of a shortcode within the already set up pages for each of those 20 teams. You could use this plugin to achieve your goal: https://wordpress.org/plugins/display-posts-shortcode/

It allows you to add a shortcode with certain arguments into your pages and then display certain categories only in that page.

Another solution would be to alter your template file with conditional statements querying the page you are on and according to that displaying certain post categories. You'd have to write conditionals for each of the 20 teams. There is a lot of info on conditional functions in the WordPress Codex here: https://codex.wordpress.org/Conditional_Tags Each page has it's own unique ID within your WP system. Let's''' say U11 page had the ID of 233 and U12 had the ID of 244 then altering the loop to display only certain post categories would need a line like this:

if ( is_page( 233 ) ) { 
    echo "my U11 loop";
} elseif ( is_page( 244 ) ) {
    echo "my U12 loop";
} else {
    echo "my standard loop as long as it is not U11 or U12";
}

Hope this will guide you to solve your issue

Hugs Sas

Thanks for this useful information. I was also thinking about the the conditional statements, but I thought it was not possible with loops. I will try the plugin first, looks dead simple, but I don't like to use too many plugins.