Display two items from a custom post type that have 2 different taxonomy terms but output different HTML in the loop for one of those terms

Question

sorry if my question title does not make much sense, I couldn’t think of a good way of wording it.

Essentially, I currently have some custom loop code on single.php that does the following:

If a post_tag from the main article (post_type: post) contains a matching post_tag of a single item (game) in the custom post type ‘games’ it displays a game info box with cover image, publisher, developer, etc, else if the post_tag matches 2 items then it displays different HTML output that lists both games. In this instance, it is the same game but under different ‘platform’ terms (ps4 and ps5)

What I am trying to achieve is this: if the article post_tag matches the games post_tag and finds 2 games with the same tag then outpit both games but, the game that has a ‘platform’ term of ps5 has the same HTML layout as if the tag only matched 1 game.

Here are some screenshots. https://imgur.com/a/lE2aBP2

The first image is how the game info box displays when the post_tag matches just one game.

The second image is how the HTML displays currently when the post_tag matches 2 games.

Third image is how I would like the HTML to output if the post_tag matches the same 2 games but the game with taxonomy ‘platform’ and the term ‘ps5’ is displayed as the full infobox.

Here is the code I currently have in single.php

    <div class="col-4 col-xs-12 col-sd-12 col-ld-4 post-sidebar">
        
        <?php

//get the taxonomy terms of custom post type
$getGameInfoByTag = wp_get_object_terms( $post->ID, 'post_tag', array('fields' => 'ids') );

//query arguments
$args = array(
    'post_type' => 'game',
    'post_status' => 'publish',
    'posts_per_page' => 1,
    'tax_query' => array(
        array(
            'taxonomy' => 'post_tag',
            'field' => 'id',
            'terms' => $getGameInfoByTag
        )
    ),
    'post__not_in' => array ($post->ID),
);

//the query
$relatedGame = new WP_Query( $args );
//loop through query
if($relatedGame->have_posts()){
    while($relatedGame->have_posts()){ 
        $relatedGame->the_post();

?>
<?php if ($relatedGame->found_posts == 1 ) : ?>
    <div class="game-info-box">
        <h3 class="widget-title">Game Info</h3>
        <div class="game-info-box-inner">
            <div class="game-ib-title">
            <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
            </div>
            <div class="game-ib-cover">
                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('game-cover-art'); ?></a>
            </div>      
            <div class="game-ib-info">
            <p><span class="taxo-bold">Publisher: </span> <?php the_terms( $post->ID, 'publisher', '', ', ', '' ); ?></p>
            <p><span class="taxo-bold">Developer: </span> <?php the_terms( $post->ID, 'developer', '', ', ', '' ); ?></p>
            <?php if ( get_field('release_date') ): ?>
            <p><span class="taxo-bold">Release Date: </span> <?php the_field('release_date'); ?></p>
            <?php else : ?>
            <p><span class="taxo-bold">Release Date: </span> <?php the_field('release_year'); ?></p>
            <?php endif; ?>
            <p><span class="taxo-bold">Genre: </span> <?php the_terms( $post->ID, 'genre', '', ', ', '' ); ?></p>
            <?php if (get_field('psvr_game')): ?>
            <p><span class="taxo-bold">PSVR: </span><?php the_field('psvr_game'); ?></p>
            <?php endif; ?>
            <p><span class="taxo-bold">Platform: </span> <?php the_terms( $post->ID, 'platform', '', ', ', '' ); ?></p>
            
            
            </div>   
            
            <?php 
$alink = get_field('amazon_link');
if( $alink ): ?>
<div class="CTAwrap">
    <a target="_blank" class="CTA-btn" href="<?php echo esc_url( $alink ); ?>">View on Amazon</a>
</div>
<?php endif; ?>
        </div>
    
    
    </div><!-- game info box -->
    <?php endif; ?>
<?php
    }
}
?>

<?php 
wp_reset_postdata();

?>
<?php if ($relatedGame->found_posts > 1 ) : ?>
<div class="post_related-games">
    <h3 class="widget-title">Related Games</h3>
    <div class="post_related-games-inner">
<ul>
<?php $related_games = psf_related_games_in_post(); ?>
<?php if ($related_games->have_posts()) : while ($related_games->have_posts()) : $related_games->the_post(); ?>

<li><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail('game-cover-small'); ?><?php the_title(); ?><span>(<?php the_field('release_year'); ?>)</span></a></li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
</div>
<?php endif; ?>

The code does it’s job but I can’t figure out how to output a game differently if it has the specific term of ‘ps5’.Can I modify the existing loop somehow, or do I require 2 loops?

Any assistance would be much appreciated.

Thanks.

0
Destiny2Gamer 2 years 2020-10-25T19:10:19-05:00 0 Answers 6 views 0

Leave an answer

Browse
Browse