If Statement Not Displaying All Set Elements

With the below code, I’m trying to have the IF statement do the following:

If there is a Catalog to display, post it.
If there are Accessories to display, post it.

My issue:
The loop only works if there are Accessories to display, even if there is a Catalog to display. If there are no Accessories to display, the Catalog won’t display either.

How can this be modified to where this can be an either/or statement?

<?php
/**
  if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

?>
<h1 itemprop="name" class="product_title entry-title"><?php the_title(); ?></h1>

<?php
global $product, $woocommerce_loop;

$upsells = $product->get_upsells();

if ( sizeof( $upsells ) === 0 ) {
    return;
}

$meta_query = WC()->query->get_meta_query();

$args = array(
    'post_type'           => 'product',
    'ignore_sticky_posts' => 1,
    'no_found_rows'       => 1,
    'posts_per_page'      => $posts_per_page,
    'orderby'             => $orderby,
    'post__in'            => $upsells,
    'post__not_in'        => array( $product->id ),
    'meta_query'          => $meta_query
);

$products = new WP_Query( $args );

$woocommerce_loop['columns'] = $columns;

if ( $products->have_posts() ) : ?>

     <div class="upsells products">

        <a id="trigger" class="product btn"><?php _e( 'Accessories', 'woocommerce' ) ?> <span class="glyphicon glyphicon-chevron-down">&nbsp;</span></a>
      
        <?php 
        
                $file = get_field('catalog');
                
                if( $file ) {
                
                    $url = wp_get_attachment_url( $file );
                    
                    ?><a id="catalog" target="_blank" class="product btn" href="<?php echo $url; ?>" >DOWNLOAD CATALOG</a><?php
                
                }
                
                ?>  

        <?php woocommerce_product_loop_start(); ?>

            <?php while ( $products->have_posts() ) : $products->the_post(); ?>

                <?php wc_get_template_part( 'content', 'accessories' ); ?>

            <?php endwhile; // end of the loop. ?>

        <?php woocommerce_product_loop_end(); ?>
        
    </div>
    
   
    

<?php endif;

wp_reset_postdata();

?>

Can you remove the “/**” at the top?

As for the logic, the display of everything depends on $products->have_posts(). If you want to output, when there is a catalog, then you need to make that the conditional. Something like

$products = new WP_Query( $args );

$woocommerce_loop['columns'] = $columns;

$file = get_field('catalog');
                
if( $file ) {?>

     <div class="upsells products">

        <a id="trigger" class="product btn"><?php _e( 'Accessories', 'woocommerce' ) ?> <span class="glyphicon glyphicon-chevron-down">&nbsp;</span></a>
      
        <?php 
        
        $url = wp_get_attachment_url( $file );
                    
        ?><a id="catalog" target="_blank" class="product btn" href="<?php echo $url; ?>" >DOWNLOAD CATALOG</a><?php

Scott

Thanks, s_molinari.

But I’m still getting the same result. Items that have ‘Accessories’ will post both the accessories and the Catalog. But those that don’t won’t post just the Catalog.

I can’t help you any further, without more information.

Scott

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.