Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Garconis/d4eb99bda35b00fde31c0eb130a45591 to your computer and use it in GitHub Desktop.
Save Garconis/d4eb99bda35b00fde31c0eb130a45591 to your computer and use it in GitHub Desktop.
WordPress | Shortcode Loop of CPT with ACF Fields (Bing Devils score cards) [work-in-progress]
<?php
// create shortcode to list all Games in Page
add_shortcode( 'fs-game-schedule-page-loop', 'fs_sc_game_schedule_page_loop' );
function fs_sc_game_schedule_page_loop( $atts ) {
ob_start();
// define attributes and their defaults
extract( shortcode_atts( array (
'post_type' => 'fs_schedule',
'posts_per_page' => -1,
'header_only' => 0,
'game_status_check' => 'Upcoming',
'order_by' => 'ASC',
), $atts ) );
// define query parameters based on attributes above
$options = array(
'post_type' => $post_type,
'post_status' => 'publish', // only get posts with this status
'posts_per_page' => $posts_per_page,
// get items with the following meta (has Game Date AND set to be active in Header)
'meta_query' => array(
'relation' => 'AND',
'game_date_clause' => array(
'key' => 'game_date',
'compare' => 'EXISTS',
),
// only get items that are set to be shown in header (true)
'display_in_header_clause' => array(
'key' => 'display_in_header',
'value' => $header_only,
'compare' => '>='
),
// only get items that are marked as Completed or Upcoming
'game_status_check_clause' => array(
'key' => 'game_status_check',
'value' => $game_status_check,
'compare' => '='
),
),
// orderby the date value
'orderby' => array(
'game_date_clause' => $order_by,
),
);
$query = new WP_Query( $options );
// run the loop based on the query
if ( $query->have_posts() ) {
echo'<div class="game-schedule-page"><ul class="game-schedule-listing game-schedule-page-loop">';
while ( $query->have_posts() ) : $query->the_post();
$game_date = get_field('game_date');
$versus_team = get_field('versus_team');
if ( $versus_team == "Laval Rocket" ) {
$versus_team_url = 'https://www.rocketlaval.com/';
$versus_team_image_filename = 'laval-rocket.png';
}
if ( $versus_team == "Binghamton Devils" ) {
$versus_team_url = 'http://www.binghamtondevils.com/';
$versus_team_image_filename = 'binghamton-devils.png';
}
if ( $versus_team == "Belleville Senators" ) {
$versus_team_url = 'https://bellevillesens.com/';
$versus_team_image_filename = 'belleville-senators.png';
}
if ( $versus_team == "Utica Comets" ) {
$versus_team_url = 'http://www.uticacomets.com/';
$versus_team_image_filename = 'utica-comets.png';
}
if ( $versus_team == "Cleveland Monsters" ) {
$versus_team_url = 'https://www.clevelandmonsters.com/';
$versus_team_image_filename = 'cleveland-monsters.png';
}
if ( $versus_team == "Toronto Marlies" ) {
$versus_team_url = 'http://marlies.ca/';
$versus_team_image_filename = 'toronto-marlies.png';
}
if ( $versus_team == "Syracuse Crunch" ) {
$versus_team_url = 'https://syracusecrunch.com/';
$versus_team_image_filename = 'syracuse-crunch.png';
}
if ( $versus_team == "Rochester Americans" ) {
$versus_team_url = 'https://www.amerks.com/home';
$versus_team_image_filename = 'rochester-americans.png';
}
if ( $versus_team == "Colorado Eagles" ) {
$versus_team_url = 'https://www.coloradoeagles.com/';
$versus_team_image_filename = 'colorado-eagles.png';
}
if ( $versus_team == "Tucson Roadrunners" ) {
$versus_team_url = 'http://www.tucsonroadrunners.com/home/';
$versus_team_image_filename = 'tucson-roadrunners.png';
}
if ( $versus_team == "Stockton Heat" ) {
$versus_team_url = 'https://stocktonheat.com/';
$versus_team_image_filename = 'stockton-heat.png';
}
if ( $versus_team == "San Jose Barracuda" ) {
$versus_team_url = 'http://www.sjbarracuda.com/';
$versus_team_image_filename = 'san-jose-barracuda.png';
}
if ( $versus_team == "San Diego Gulls" ) {
$versus_team_url = 'https://www.sandiegogulls.com/';
$versus_team_image_filename = 'san-diego-gulls.png';
}
if ( $versus_team == "Ontario Reign" ) {
$versus_team_url = 'http://www.ontarioreign.com/';
$versus_team_image_filename = 'ontario-reign.png';
}
if ( $versus_team == "Bakersfield Condors" ) {
$versus_team_url = 'https://www.bakersfieldcondors.com/';
$versus_team_image_filename = 'bakersfield-condors.png';
}
if ( $versus_team == "Grand Rapids Griffins" ) {
$versus_team_url = 'http://www.griffinshockey.com/';
$versus_team_image_filename = 'grand-rapids-griffins.png';
}
if ( $versus_team == "Milwaukee Admirals" ) {
$versus_team_url = 'http://www.milwaukeeadmirals.com/';
$versus_team_image_filename = 'milwaukee-admirals.png';
}
if ( $versus_team == "Manitoba Moose" ) {
$versus_team_url = 'https://moosehockey.com/';
$versus_team_image_filename = 'manitoba-moose.png';
}
if ( $versus_team == "Iowa Wild" ) {
$versus_team_url = 'https://www.iowawild.com/';
$versus_team_image_filename = 'iowa-wild.png';
}
if ( $versus_team == "Texas Stars" ) {
$versus_team_url = 'http://www.texasstars.com/';
$versus_team_image_filename = 'texas-stars.png';
}
if ( $versus_team == "Rockford IceHogs" ) {
$versus_team_url = 'http://www.icehogs.com/home/';
$versus_team_image_filename = 'rockford-icehogs.png';
}
if ( $versus_team == "San Antonio Rampage" ) {
$versus_team_url = 'http://www.sarampage.com/';
$versus_team_image_filename = 'san-antonio-rampage.png';
}
if ( $versus_team == "Chicago Wolves" ) {
$versus_team_url = 'https://www.chicagowolves.com/';
$versus_team_image_filename = 'chicago-wolves.png';
}
if ( $versus_team == "Lehigh Valley Phantoms" ) {
$versus_team_url = 'http://www.phantomshockey.com/';
$versus_team_image_filename = 'lehigh-valley-phantoms.png';
}
if ( $versus_team == "Providence Bruins" ) {
$versus_team_url = 'http://www.providencebruins.com/';
$versus_team_image_filename = 'providence-bruins.png';
}
if ( $versus_team == "Hartford Wolf Pack" ) {
$versus_team_url = 'http://www.hartfordwolfpack.com/';
$versus_team_image_filename = 'hartford-wolf-pack.png';
}
if ( $versus_team == "Springfield Thunderbirds" ) {
$versus_team_url = 'http://www.springfieldthunderbirds.com/';
$versus_team_image_filename = 'springfield-thunderbirds.png';
}
if ( $versus_team == "Charlotte Checkers" ) {
$versus_team_url = 'http://gocheckers.com/';
$versus_team_image_filename = 'charlotte-checkers.png';
}
if ( $versus_team == "Hershey Bears" ) {
$versus_team_url = 'https://www.hersheybears.com/';
$versus_team_image_filename = 'hershey-bears.png';
}
if ( $versus_team == "Bridgeport Sound Tigers" ) {
$versus_team_url = 'http://www.soundtigers.com/';
$versus_team_image_filename = 'bridgeport-sound-tigers.png';
}
if ( $versus_team == "Wilkes-Barre/Scranton Penguins" ) {
$versus_team_url = 'http://www.wbspenguins.com/';
$versus_team_image_filename = 'wilkes-barre-scranton-penguins.png';
}
// get uploads directory
$upload_dir = wp_upload_dir();
// get full path of the uploads directory and append the filename
$versus_team_image_filepath = $upload_dir['baseurl'] .'/'. $versus_team_image_filename;
$home_or_away = get_field('home_or_away');
if ( $home_or_away == "Home" ) {
$list_item_home_or_away_class = 'game-home';
}
elseif ( $home_or_away == "Away" ) {
$list_item_home_or_away_class = 'game-away';
}
$watch_live = get_field('watch_live');
$listen_live = get_field('listen_live');
$ticket_link = get_field('ticket_link');
// this only gets the field if it is true (1)
$display_in_header = get_field('display_in_header');
if ( $display_in_header) {
$list_item_header_class = 'display-in-header-true';
}
else {
$list_item_header_class = 'display-in-header-false';
}
$promotions = get_field('promotions');
$score_devils = get_field('score_devils');
$score_opponent = get_field('score_opponent');
$score_outcome = get_field('score_outcome');
if ( $score_outcome == "In Game" ) {
$list_item_game_status_class = 'game-in-play';
$game_status = '-';
}
elseif ( $score_outcome == "Won" ) {
$list_item_game_status_class = 'game-won';
$game_status = 'W';
}
elseif ( $score_outcome == "Lost" ) {
$list_item_game_status_class = 'game-lost';
$game_status = 'L';
}
elseif ( $score_outcome == "Tie" ) {
$list_item_game_status_class = 'game-tie';
$game_status = 'T';
}
$post_game_link = get_field('post_game_link');
$post_game_link_text = get_field('post_game_link_text');
$game_sheet_link = get_field('game_sheet_link');
$game_status_check = get_field('game_status_check');
echo '<li class="'. $list_item_header_class .' '. $list_item_home_or_away_class .' '. $list_item_game_status_class .'">';
echo $game_status_check;
echo '<div class="game-card-top">';
// TOP LEFT
echo '<div class="game-card-top-left">';
if( $game_date ) {
echo '<div class="game-date-wrapper">';
$date = new DateTime($game_date);
echo '<div class="game-date-day">'. $date->format('D') .'</div>';
echo '<div class="game-date-month-day"><span class="game-date-month">'. $date->format('M') .'</span> <span class="game-date-day">'. $date->format('d') .'</span></div>';
echo '<div class="game-date-time">'. $date->format('h:i A') .'</div>';
echo '</div>';
}
if( $versus_team ) {
echo '<div class="versus-team">';
echo '<span class="versus-team-divider">vs</span>';
echo '<span class="versus-team-image-link"><a href="'. $versus_team_url .'" target="_blank" title="'. $versus_team .'">';
echo '<img src="'. $versus_team_image_filepath .'" alt="'. $versus_team .'">';
echo '</a></span>';
echo '</div>';
}
echo '</div><!-- / .game-card-left -->';
/*
* if the score status is still "In Game",
* then the final score has not been set yet,
* so show the links for the Feeds and Tickets
*/
if ( $score_outcome == "In Game" ) {
// TOP RIGHT (PRE GAME)
echo '<div class="game-card-top-right game-card-top-right-pre-game">';
if ( $watch_live || $ticket_link ) {
echo '<div class="watch-or-listen-live">';
if( $watch_live ) {
echo '<div class="watch-live"><a href="'. $watch_live .'" target="_blank" title="Watch Live">Watch Live</a></div>';
}
if( $listen_live ) {
echo '<div class="listen-live"><a href="'. $listen_live .'" target="_blank" title="Listen Live">Listen Live</a></div>';
}
echo '</div>';
}
if( $ticket_link ) {
echo '<div class="ticket-link"><a href="'. $ticket_link .'" target="_blank" title="Tickets">Tickets</a></div>';
}
echo '</div><!-- / .game-card-right -->';
}
/*
* if a score has been entered for both teams,
* and the score status is no longer the default "In Game",
* then the score is likely a Final, so show the Score info
* and also the Game Sheet button (if it exists)
*/
if( $score_devils && $score_opponent && ( $score_outcome !== "In Game" ) ) {
// TOP RIGHT (POST GAME)
echo '<div class="game-card-top-right game-card-top-right-post-game">';
echo '<div class="final-score"><span class="final-score-numbers">'. $score_devils .' - '. $score_opponent .'</span> <span class="game-status">'. $game_status .'</span></div>';
// if the Game Sheet Link has been filled out, show the button
if( $game_sheet_link ) {
echo '<div class="game-sheet"><a href="'. $game_sheet_link .'" target="_blank" title="Official Game Report">Game Sheet</a></div>';
}
echo '</div><!-- / .game-card-right -->';
}
echo '</div><!-- / .game-card-top -->';
/*
* if the score status is still "In Game",
* then the final score has not been set yet,
* so show the game's main Promotion
*/
if ($score_outcome == "In Game") {
// BOTTOM (PRE GAME)
echo '<div class="game-card-bottom game-card-bottom-pre-game">';
if( $home_or_away ) {
echo '<div class="home-or-away">'. $home_or_away .'</div>';
}
// check if the Promotions have any filled out
if( have_rows('promotions') ) {
echo '<div class="promotions"><ul class="promotions-wrapper">';
// loop through each promotion within the repeater field
while ( have_rows('promotions') ) : the_row();
// grab the sub fields within the actual promotion row
$promotion_name = get_sub_field('promotion_name');
echo '<li class="promotion-name-list-item">';
if( $promotion_name ) {
echo '<div class="promotion-name">'. $promotion_name .'</div>';
}
echo '</li>';
// this BREAK forces the loop to ONLY grab the FIRST row. If you want ALL of them, then remove the BREAK line.
// break;
endwhile;
echo '</ul></div><!-- / .promotions -->';
}
echo '</div><!-- / .game-card-bottom -->';
}
/*
* if a Post Game Link exists,
* and if a score has been entered for both teams,
* and the score status is no longer the default "In Game",
* then display the Post Game link and its text (if it has any)
*/
if( $post_game_link && ( $score_devils && $score_opponent ) && ( $score_outcome !== "In Game" ) ) {
// BOTTOM (POST GAME)
echo '<div class="game-card-bottom game-card-bottom-pre-game">';
echo '<div class="post-game-link"><a href="'. $game_sheet_link .'" target="_blank">';
// if there is data in the Post Game Link Text field, then show that text
if( $post_game_link_text ) {
echo $post_game_link_text;
}
// otherwise we use this as a fallback
else {
echo 'Post-Game Article';
}
echo '</a></div>';
echo '</div><!-- / .game-card-bottom -->';
}
echo '</li>';
endwhile;
wp_reset_postdata();
echo '</ul></div><!-- / .game-schedule-page -->';
$myvariable = ob_get_clean();
return $myvariable;
}
else {
echo'<div class="game-schedule-page" class="game-schedule-page-loop-empty"><ul class="game-schedule-listing game-schedule-page-loop">';
echo '<li>';
echo 'Sorry, no games to show.';
echo '</li>';
echo '</ul></div>';
$myvariable = ob_get_clean();
return $myvariable;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment