Support

Account

Home Forums Add-ons Repeater Field Issue with repeater rows and conditional statement

Solved

Issue with repeater rows and conditional statement

  • I have logic in my site that says, if you are on a Category page and that page has repeater rows entered, then show those repeater rows and if not then show the repeater rows from the homepage. The problem is if I add Articles (my repeater rows allow for entering multiple Articles info) to the Category page, and then later delete them, I want the homepage repeater rows to display, but what happens is it just shows a blank space on my Category page, seemingly still seeing that there is data there but not showing anything. Trying to figure out how I can do the conditional logic on this. This is what I currently have — first if statement is for when there are Article repeaters on the Category page and the elseif statement is for when there are no ARticle repeaters on that page, so it should use the repeaters from the homepage instead. I tried adding a 3rd condition in that first IF statement but that didnt work ( if ( isset($page_category_id) && have_rows(‘articles’,’product-categories_’ . $page_category_id) && have_rows(‘article’,’product-categories_’ . $page_category_id))

    Any ideas?

    <?php 
    if ( isset($page_category_id) && have_rows('articles','product-categories_' . $page_category_id)) :
        while( have_rows('articles','product-categories_' . $page_category_id)): 
           the_row(); 
            if ( have_rows('article')) : ?>
            <div class="gallery gallery--news featured-articles taxonomy">
            <?php while( have_rows('article')): the_row(); 
                include( 'content/content-articles-grid.php' );
             endwhile; ?>
            </div>
           <?php endif;
       endwhile;
    elseif ( have_rows('articles',$front_page_id)) :
        while( have_rows('articles',$front_page_id)): the_row(); 
           if ( have_rows('article')) : ?>
             <div class="gallery gallery--news featured-articles homepage">
             <?php while( have_rows('article')): the_row(); 
                 include( 'content/content-articles-grid.php' );
             endwhile; ?>
             </div>
           <?php endif;
       endwhile;
    endif; ?>
  • The problem is that the repeater on the category still has rows. Deleting a post, category, or whatever might be in a repeater row will not remove that row from the repeater.

    Sorry, because of the way you are doing this I cannot give you code. I can only give you an idea.

    You need to loop over the first repeater and detect if any output is created, if not then do the second repeater.

    Basic example

    
    $output_created = false;
    if (have_rows('repeater')) {
      while (have_rows('repeater')) {
        the_row();
        // if you generate any content here
        $output_created = true;
        
      }
    }
    if (!$output_created) {
       // noting created in the first loop
       // do the other repeater
    }
    
  • Perfect! Great solution John, thanks for the help.

  • So that you know, I generally use output buffers for things like this.

    
    ob_start();
    
    // do something
    
    $content = ob_get_clean();
    
    if (!empty($content)) {
      echo $content;
    } else {
      
      // do something else
    
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.