How to use team, player news sections

One of the best parts about using the Sports Bench theme with the plugin is the built-in support, including team and player pages with no code from you. And one of the best parts of those pages is a section for news items for the team or player.

But how do you get items to show up there? Well it involves adding tags in just the right way. So let’s learn, shall we?

How to get news items to appear

In order to get posts for a team to show in the news section, you need to add the team name as a tag in the post. This tag must have be the same as the text entered for the team name, which is the first and largest field on the add/edit team page.

For posts for a player to show up in the news section, you need to add the player’s name as a tag for the post. This tag must be the same as the player’s first and last name as it’s entered on his/her edit player page.

The reason these tags have to be the same is that the code for those sections are using the team or player slugs to look for posts that have tags with the same slug. And then those posts are added to the section.

This might be changed in coming versions of Sports Bench by adding custom taxonomies for teams and players.

How to add news section to team page

But what if you’re not using the Sports Bench theme and want to have a news section for your team page template? Well, fortunately that’s pretty easy to use.

Before I start, I’m going to assume you have a team page template already to go. If not, read up on that before we start. I’m also just going to show you how to query posts for a team. What you do with the posts is up to you.

So this functionality relies heavily on the WP_Query class and setting different arguments to get specific posts. It’s always a good idea to read through the WP_Query page on the WordPress codex, especially if you’ve never developed with it before.

In order to get our team posts, we’re going to use the tag argument and then make sure they’re ordered from newest first.

$args = array(
'tag' => $team->team_slug,
'orderby' => 'date',
'order' => 'DESC'
);
$team_query = new WP_Query( $args );

And now you can do whatever you want with these posts.

How to add news section to the player page

Likewise for the player page, I’m going to assume that you already have a player page template. If not, you should read up on that before starting. And like the team page, we’re going to be using the WP_Query class.

So, our query code for getting posts for a player will look like this.

$args = array(
'tag' => $player->player_slug,
'orderby' => 'date',
'order' => 'DESC'
);
$player_query = new WP_Query( $args );

And that’s how you can add a news section for your team and player page templates to make your site that much better.

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept the Privacy Policy

This site uses Akismet to reduce spam. Learn how your comment data is processed.