Support

Account

Home Forums Front-end Issues Loop a Group

Solved

Loop a Group

  • Hello community,

    I am currently in the process of creating and improving a template for a project that allows users to enter a place name, a picture of the place, facts, content and a comment. Except for the comment, everything is mandatory. If I leave the field with the comment blank, it will still be displayed in the html source code. I think I’m putting the loop wrong. Can someone help me? Many thanks.

    <?php
    /**
     * The template for displaying all pages.
     *
     * This is the template that displays all pages by default.
     * Please note that this is the WordPress construct of pages
     * and that other 'pages' on your WordPress site will use a
     * different template.
     *
     * @package understrap
     */
    
    // Exit if accessed directly.
    defined( 'ABSPATH' ) || exit;
    
    get_header();
    
    $container = get_theme_mod( 'understrap_container_type' );
    
    ?>
    
    <div class="wrapper" id="page-wrapper">
    	<div class="<?php echo esc_attr( $container ); ?>" id="content" tabindex="-1">
    		<div class="row">
    			<div class="col-md-12 content-area" id="primary">
    				<main class="site-main" id="main" role="main">
    					<?php while ( have_posts() ) : the_post(); ?>
    					<?php get_template_part( 'loop-templates/content', 'page' ); ?>
    						<?php
    						// If comments are open or we have at least one comment, load up the comment template.
    						if ( comments_open() || get_comments_number() ) :
    							comments_template();
    						endif;
    						?>
    					<?php endwhile; // end of the loop. ?>
    				</main>
    			</div>
    		</div>
    		<div class="row">
    			<?php if( have_rows('ortsteil') ): ?>
    				<?php while( have_rows('ortsteil') ): the_row();
    					// vars
    					$ortsname = get_sub_field('ortsname');
    					$bild = get_sub_field('bild');
    					$fakten = get_sub_field('fakten');
    					$inhalt = get_sub_field('inhalt');
    					$kommentar = get_sub_field('kommentar');
    				?>
    				<div class="col-md-12 ortsteil">
    					<div class="row">
    						<div class="col-md-12">
    							<h2><?php echo $ortsname; ?></h2>
    						</div>
    						<div class="col-lg-5 col-md-12 mb-3 ortsmotiv">
    							<img class="img-fluid mx-auto d-block" src="<?php echo $bild['url']; ?>" alt="<?php echo $bild['alt'] ?>" />
    						</div>
    						<div class="col-lg-7 col-md-12 mb-3 fakten">
    							<strong><?php echo $fakten; ?></strong>
    						</div>
    						<div class="col-md-12">
    							<div class="inhalt">
    								<?php echo $inhalt; ?>
    							</div>
    						</div>
    						<?php if( have_rows('kommentar') ):?>
    							<?php while( have_rows('kommentar') ) : the_row();
    
    								$profilbild = get_sub_field('profilbild');
    								$kommentar = get_sub_field('kommentar');
    							?>
    							<div class="col-md-12">
    								<div class="kommentar">
    									<div class="row">										
    										<div class="col-lg-2 col-md-4 col-sm-12">
    											<img class="img-fluid mx-auto d-block" src="<?php echo $profilbild['url']; ?>" alt="<?php echo $profilbild['alt'] ?>" />
    										</div>
    										<div class="col-lg-9 col-md-8 col-sm-12 kommentar_text">
    											<?php echo $kommentar; ?>
    										</div>
    									</div>
    								</div>
    							</div>
    							<?php endwhile; ?>
    						<?php endif; ?>
    					</div>
    				</div>
    				<?php endwhile; ?>
    			<?php endif; ?>
    		</div>
    	</div>
    </div>
    
    <?php get_footer();
    
  • assuming you mean the feidl ‘komentar’, $kommentar = get_sub_field('kommentar'); is this field a sub field or a repeater <?php if( have_rows('kommentar') ):?>, or both?

  • Sorry for my late answer! It is both. It is the name of the repeater and a sub field in the repeater.

  • Looking back over your code I can’t tell what part you are referring to that is not working as you expect. I’m going to need you to narrow it down and explain where it’s going wrong, what you expect and what is happening instead.

  • I would like to have checked via php whether the fields in the repeater field are empty. If so, the empty source code for the field should not appear on the website. For individual fields it is possible for me.

    Example:

    <?php if( get_field('beschreibung') ): ?>
        <?php the_field('beschreibung'); ?>
    <?php endif; ?>

    How do I do this on repetitions?

  • It is that same for sub field as other fields.

    
    if (get_sub_field('beschreibung')) {
      the_sub_field('beschreibung');
    }
    
  • Thank you very much, that solved my problem.

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Loop a Group’ is closed to new replies.