Adding Game Recaps and Previews

Beginner
Intermediate
Game
Game Recaps
Plugin

We are so close to the finish line. You’re doing great, but now we’ve reached our toughest challenge yet. However, with this as your guide, you’ll be done with it in no time.

This section deals with post templates. Introduced in WordPress 4.7, post templates work the same as page template, but obviously with post types and an extra line of code. This is also why WordPress 4.7 is required for this plugin.

This section will cover creating game preview and game recap post templates. We’ll start easy first with game previews.

Creating the Game Preview Template

There are two ways you can create game previews. The easiest is to use the “Game” shortcode. To do this, add a new post and then hit the “SB” at the top of the editor. Then select game and then the game you want to show. And that’s all you need. If you want to create a custom post template for previews, use the directions below.

  1. Like the player and team templates, create a new php file and name it page-game-preview.php. But now, copy the contents of your theme’s single.php file. And we’re also going to leave the loop untouched. Our focus is on the sidebar.
  2. Again, like the player team templates, add the following lines of code to right after the opening php tag. Note the extra line which helps WordPress realize that this is intended for posts and not pages.
    				Template Name: Game Preview
    				Template Post Type: post
    
  3. Now that that’s taken care of, let’s turn our attention to the sidebar. Again, locate where the get_sidebar function. If it’s wrapped in a div with a class of “sidebar” or “widgets” or something of the like, you have nothing to worry about. The lines of code we’ll write later on will go right before this function. Otherwise, locate your theme’s sidebar.php file. Copy the lines of code that control the primary sidebar and replace the get_sidebar function in the template with the copied code. It might look something like this:
    				<div id="sidebar1" class="sidebar>
    					<?php if ( is_active_sidebar( 'sidebar1' ) ) : ?>
    						<?php dynamic_sidebar( 'sidebar1' ); ?>
    					<?php endif; ?>
    				</div>
    

    Note: Your sidebar.php file probably won’t look exactly like this. This is more a guide. It should, however, at least look something like it if it’s up to WordPress standards.

  4. Now, right before either the code you just pasted or the get_sidebar functions, add in the following lines of code to add a game preview “widget”. This will show the teams’ records, names and logos as well as the date, time and location of the game. If the game is final, the score will show. And if the game has a recap, a link to the recap will show as well.
    				<?php echo sports_bench_show_game_preview_info(); ?>
    
  5. So that’s all of the coding we have to do for the preview template. Now let’s add a preview. To start, add a new page. Then type a preview into the main post editor.
  6. Once you are done, go the “Post Attributes” section on the right-hand side. Select “Game Preview” from the post template dropdown. next, find the custom fields for Sports Bench. It’ll be below the main editor somewhere, depending on what plugins you have installed. There, select “Preview” and then select which game this is a preview for.
  7. Now hit “Publish” and then hit “View Post”. And that’s it. You’ve created a really cool game preview post template that adds necessary info with minimal effort from you in the future. Congrats! Now let’s move on to game recaps.

Creating the Game Recap Template

There are two ways you can create game recap areas. The easiest is to use the game recap shortcode. To do this, add a new post and then hit the “SB” at the top of the editor. Then select “Game Recap” and then the game you want to show. And that’s all you need. If you want to create a custom post template for recaps, use the directions below.

  1. Like the game preview template, create a new php file and name it page-game-recap.php and copy the contents of your theme’s single.php file into the new template.
  2. Like the game preview template, add the following lines of code right after the opening php tag:
    				Template Name: Game Recap
    				Template Post Type: post
    
  3. Again, let’s turn our attention to where the get_sidebar function is located. If it’s inside a div with the class “sidebar” or “widgets” or the like, simply delete the function since we’re overriding the default sidebar here. Otherwise, go to the sidebar.php file and copy the div that contains the actual sidebar. You don’t need any inside that div, just the outer shell. Then paste that over the get_sidebar function in the recap template.
  4. Now, inside the sidebar container that was either already there or just pasted, place the following line of code:
    				<?php echo sports_bench_show_game_stats_info(); ?>
    
  5. So that’s all the coding you have to do. But let’s add a recap just to test it out. To start go add add a new post and put some text into the main text editor.
  6. Next, scroll down on the right-hand side and find the “Post Attributes” section. Select “Game Recap” as the post template.
  7. Then find the Sports Bench custom fields. This will be somewhere below the main post editor depending on what plugins you have on your site. There select “Recap” and then select the game the recap is for.
  8. Finally, hit publish and then view the post. Congrats! You’ve now created a recap template that will show off all sorts of information from the scoreline to the team stats to the individual stats. It looks awesome.

And that’s all the code you need to get started. The best part is now, when you create a preview or a recap for a game, the link to that post will automatically show up on the team page and where the game is located on the scoreboard. It adds that extra functionality that takes your site to the next level.

Leave a Reply

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

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