Support

Account

Forum Replies Created

  • Hey John, I ended up finding a solution. Thank you again for all your help. I really took a lot away from your suggestions and in the future will be developing and coding in a different way. THANK YOU.

    Here is what worked, I know it may not be the cleanest.

    <?php get_header(); ?>
    <div class="row" data-equalizer="bridge">
      <?php
        // moved while outside of two main divs
      while (have_posts()) {
        the_post();
        ?>
        <div class="large-4 medium-4 small-12 columns tout-single-doctor" data-equalizer-watch="bridge">
          <div class="single-doctor-container">
            <h2 class="doctor-title"><?php echo get_the_title(); ?></h2>
            <?php
    
            $posts = get_field('doctor_specialties');
            if ($posts) {
              foreach ($posts as $post) {
                setup_postdata($post);
                ?>
                <a class="doctor-specialty-link" href="<?php
                the_permalink(); ?>"><?php the_title(); ?></a>
                <?php
                    } // end foreach $posts
                    wp_reset_postdata();
                  } // end if $posts
    
                  the_post_thumbnail('full');
    
                  $posts = get_field('doctor_services');
                  if ($posts) {
                    foreach ($posts as $post) {
                      setup_postdata($post);
                      ?>
                      <p>
                        <span class="doctor-background-title">Provider Services:</span>
                        <a class="doctor-specialty-link" href="<?php
                        the_permalink(); ?>"><?php the_title(); ?></a>
                      </p>
                      <?php
                    } // end foreach $posts
                    wp_reset_postdata();
                  } // end if $posts
    
                  // cleaned this up a bit to reduce code
                  // all of the get_field() calls
                  // need to be in The Loop
                  $fields = array(
                    'doctor_biography' => 'Provider Biography',
                    'doctor_education' => 'Provider Education',
                    'doctor_residency' => 'Provider Residency',
                    'doctor_internship' => 'Provider Internship',
                    'doctor_fellowship' => 'Provider Fellowship',
                    'doctor_board_status' => 'Board Status'
                    );
                  foreach ($fields as $field => $label) {
                    $value = get_field($field);
                    if ($value) {
                      ?>
                      <p>
                        <span class="doctor-background-title"><?php
                          echo $label; ?>:</span>
                          <?php echo $value; ?>
                        </p>
                        <?php
                    } // end if $value
                  } // end foreach $fields
                  ?>
    
                </div><!-- end single-doctor-container -->
              </div> <!-- end tout-single-doctor -->
    
              <?php
              $hasRelatedContent = false;
              $this_post = $post->ID;
              $posts = get_field('doctor_specialties');
              if ($posts) {
                foreach ($posts as $post) {
                  setup_postdata($post);
                    } // end foreach $posts
                    // wp_reset_postdata();
                  } // end if $posts
    
                  $args = array(
                    'post_type' => 'our-providers',
                    'orderby' => 'meta_value',
                    'post__not_in' => array($this_post),
                    'meta_query' => array(
                      array(
                        'key' => 'doctor_specialties', // name of custom field
                        'value' => '"' . get_the_ID() . '"',
                        'compare' => 'LIKE'
                        )
                      )
                    );
                  $doctors = get_posts($args);
                  if ($doctors) {
                    $hasRelatedContent = true;
                  }
    
                  if($hasRelatedContent == true) {
                    wp_reset_postdata();
                    ?>
    
                    <div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
                      <div class="single-specialty-container">
                        <?php
    
                        $this_post = $post->ID;
                        $posts = get_field('doctor_specialties');
                        if ($posts) {
                          foreach ($posts as $post) {
                            setup_postdata($post);
                            ?>
                            <a class="doctor-specialty-link" href="<?php the_permalink(); ?>"><h2 class="related-specialties-title">More <?php the_title(); ?> Specialists</h2></a>
                            <?php
                    } // end foreach $posts
                    // wp_reset_postdata();
                  } // end if $posts
    
                  $args = array(
                    'post_type' => 'our-providers',
                    'orderby' => 'meta_value',
                    'post__not_in' => array($this_post),
                    'meta_query' => array(
                      array(
                        'key' => 'doctor_specialties', // name of custom field
                        'value' => '"' . get_the_ID() . '"',
                        'compare' => 'LIKE'
                        )
                      )
                    );
                  $doctors = get_posts($args);
                  if ($doctors) {
                    ?>
                    <ul>
                      <?php
                      foreach ($doctors as $doctor) {
                        ?>
                        <li>
                          <a href="<?php
                          echo get_permalink($doctor->ID); ?>"><?php
                          echo get_the_title( $doctor->ID ); ?></a>
                        </li>
                        <?php
                          } // end foreach $doctors
                          ?>
                        </ul>
                        <?php
                  } // end if $doctors
    
                  wp_reset_postdata();
                  ?>
                </div><!-- end single-specialty-container -->
              </div><!-- end tout-more-specialists -->
              <?php
            } // end if has content check
            else {
              ?>
              <div class="large-4 medium-4 small-12 columns tout-bridge-patient-portal_home">
                <!-- Patient Portal Widget -->
                <?php get_template_part( 'partials/global-custom-fields/patient', 'portal' ); ?>
              </div>
              <?php
            }
            ?>
    
            <?php
        } // end while have_posts
        ?>
        <div class="large-4 medium-4 small-12 columns tout-bridge-make-reservations_home" data-equalizer-watch="bridge">
          <!-- Make Reservations Widget -->
          <?php get_template_part ('partials/global-custom-fields/make', 'reservations'); ?>
        </div>
    
      </div>
      <?php get_footer(); ?>
  • Thank you again for taking the time to not only write me back but also clean up my code. I am still learning as I go (trying my best to improve) and appreciate being pointed in the right direction. The code you provided gets everything to display only if I remove the wp_reset_postdata(); from

      $this_post = $post->ID;
        $posts = get_field('doctor_specialties');
        if ($posts) {
          foreach ($posts as $post) {
            setup_postdata($post);
            ?>
              <h2 class="related-specialties-title">
                <a class="doctor-specialty-link" href="<?php 
                    the_permalink(); ?>">More <?php 
                    the_title(); ?> Specialists</a></h2>
            <?php 
          } // end foreach $posts
          //wp_reset_postdata();
        } // end if $posts

    If I keep the wp_reset_postdata then doctors with the same specialty do not display. I am still trying to figure out the best solution for doing the if else statement. The If else has to be outside of the div.tout-more-specialists, so whatever I am checking with the if statement must exist outside of the div itself. When I var_dump ($doctors) outside of the div.tout-more-specialists NULL is returned. This makes sense because I am not even calling the doctors array until later in the code. But if I var_dump ($doctors) inside the code after the doctors array I see the list of doctors that have the same specialties (example pediatrics has many doctors), and on the doctors single page that have no like specialties like (neurology), this is returned array(0) { } . So I know that I need to check to see if ($doctors) returns a full array, else display the other div. I am just trying to figure out how to write the if else statement outside of the div.tout-more-specialists. So far everything I have tried ends up breaking the code. Thank you again for your time John, you are extremely generous for all your help.

  • My last code post did not work, I see what you are saying that wrapping my page in the while loop is the first step, I wonder what I need to change to display the results from this point, because now no specialties are displaying even for the doctors that have multiple relationships.

  • Thanks for your advice. I guess I was mistaken. I thought that get_the_title could be called outside the loop? Also, So does the loop need to be around EVERY custom field used? For example:

    <h2><?php the_field('text'); ?></h2>

    Does this need to be in a while loop?

    I placed the entire page in a while loop besides the wrapper elements and changed get_the_title to the_title. I also understand you recommend “removing all the ACF and extra coding for displaying custom field and data and to first get the template code correct to get the post that’s supposed to be displayed in order.”

    I guess I am confused because I thought I was accurately getting the correct post? My issue is checking to see if there is a relationship with like specialties. Again, thank you for your time, I am truly trying to code the correct and semantic way, I am just a little confused.

    	<?php 
    			$this_post = $post->ID;
    			$posts = get_field('doctor_specialties'); 
    		?>
    	
    
    		<?php 
    			$doctors = get_posts(array(
    				'post_type' => 'our-providers',
    				'orderby' => 'meta_value',
    				'post__not_in' => array($this_post),
    				'meta_query' => array(
    					array(
    						'key' => 'doctor_specialties', // name of custom field
    						'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    						'compare' => 'LIKE'
    					)
    				)
    			));
    		?>
    
    		<?php if( $doctors ): ?>
    		
    		<div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
    			
    			<div class="single-specialty-container">
    				
    				<?php if( $posts ): ?>
    				    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    				        <?php setup_postdata($post); ?>
    			            <a class="doctor-specialty-link" href="<?php the_permalink(); ?>"><h2 class="related-specialties-title">More <?php the_title(); ?> Specialists</h2></a>    
    				    <?php endforeach; ?>
    				<?php endif; ?>
    					
    			
    				<ul>
    				<?php foreach( $doctors as $doctor ): ?>
    					<li>
    						<a href="<?php echo get_permalink( $doctor->ID ); ?>">
    							<?php echo get_the_title( $doctor->ID ); ?>
    						</a>
    					</li>
    				<?php endforeach; ?>
    				</ul>
    				
    			</div>
    		</div>
    
    		<?php else: ?>
    
    		<!-- If not providers with same specialty then display this div -->
    		<div class="large-4 medium-4 small-12 columns if-not-specialists" data-equalizer-watch="bridge" style="background:#ffffff;">
    			
    			<h1> THIS DIV DISPLAYS IF THERE ARE NO OTHER DOCTORS WITH LIKE SPECIALTIES</h1>
    			
    		</div>
    
    		<?php endif; ?>
    
    		<?php wp_reset_postdata(); ?>

    Thanks

  • ANOTHER UPDATE

    I was able to get the div to display alternate content with the if else however now the doctors that do have relationships don’t display. Instead the alternate div is always displayed.

    <?php while ( have_posts() ) : the_post(); ?>
    			<?php $doctors = get_posts(array(
    					'post_type' => 'our-providers',
    					'orderby' => 'meta_value',
    					'post__not_in' => array($this_post),
    					'meta_query' => array(
    						array(
    							'key' => 'doctor_specialties', // name of custom field
    							'value' => '"' . get_the_ID() . '"',
    							'compare' => 'LIKE'
    						)
    					)
    				));	
    			?>
    		<?php if( $doctors ): ?>
    		<div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
    			<div class="single-specialty-container">
    				<?php 
    				$this_post = $post->ID;
    				$posts = get_field('doctor_specialties');
    				if( $posts ): ?>
    				    <?php foreach( $posts as $post): ?>
    				        <?php setup_postdata($post); ?>
    			            <a class="doctor-specialty-link" href="<?php the_permalink(); ?>"><h2 class="related-specialties-title">More <?php the_title(); ?> Specialists</h2></a>    
    				    <?php endforeach; ?>
    				    <?php wp_reset_postdata(); ?>
    				<?php endif; ?>
    				<ul>
    				<?php foreach( $doctors as $doctor ): ?>
    					<li>
    						<a href="<?php echo get_permalink( $doctor->ID ); ?>">
    							<?php echo get_the_title( $doctor->ID ); ?>
    						</a>
    					</li>
    				<?php endforeach; ?>
    				</ul>
    			</div>
    		</div>
    		
    		<?php else: ?>
    		<!-- If not providers with same specialty then display this div -->
    		<div class="large-4 medium-4 small-12 columns if-not-specialists" data-equalizer-watch="bridge">
    			
    			<!-- No Related Providers Content Here -->
    			
    		</div>
    		<?php endif; ?>
    		<?php wp_reset_postdata(); ?>
    		<?php endwhile; // end of the loop. ?>
  • Hey John, Thank you for the quick response. I tried implementing the solution you suggested but have some sort of PHP error because I am getting a white screen on page load. Yes, I am trying to display the div tout-more-specialists and all of its content if the div has a relationship with more doctors.

    For Example:

    The Doctor has a specialty of Urology. I only need to show the div tout-more-specialists if there are other specialists that also specialize in Urology. If there are no other doctors specializing in Urology then display an alternate div with alternate content. I successfully was able to use the relationship fields to get all doctors that have the same specialty, but my issue is setting it up where if no one else has that specialty then display other content. Again any help is appreciated and thank you for getting back to me.

    Here is my updated code based on your suggestion. (The reason I am posting all of the code on the page is because I am not sure if it is causing other conflicts.)

    <?php get_header(); ?>
    			
    	<div class="row" data-equalizer="bridge">
    
    		<div class="large-4 medium-4 small-12 columns tout-single-doctor" data-equalizer-watch="bridge">
    			<div class="single-doctor-container">
    				<h2 class="doctor-title"><?php echo get_the_title(); ?></h2>
    					<?php 
    					$posts = get_field('doctor_specialties');
    					if( $posts ): ?>
    					    <?php foreach( $posts as $post): ?>
    					        <?php setup_postdata($post); ?>
    				            <a class="doctor-specialty-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    					    <?php endforeach; ?>
    					    <?php wp_reset_postdata(); ?>
    					<?php endif; ?>
    					
    					<?php the_post_thumbnail('full'); ?> 
    					
    					<?php 
    					$posts = get_field('doctor_services');
    					if( $posts ): ?>
    					    <?php foreach( $posts as $post): ?>
    					        <?php setup_postdata($post); ?>
    				            <p><span class="doctor-background-title">Provider Services:</span> <a class="doctor-specialty-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    					    <?php endforeach; ?>
    					    <?php wp_reset_postdata(); ?>
    					<?php endif; ?>
    
    					<?php if( get_field( "doctor_biography" ) ): ?>
    						<p><span class="doctor-background-title">Provider Biography</span> <?php the_field( "doctor_biography" ); ?></p>
    					<?php endif ?>
    
    				    <?php if( get_field( "doctor_education" ) ): ?>
    						<p><span class="doctor-background-title">Provider Education:</span> <?php the_field( "doctor_education" ); ?></p>
    					<?php endif ?>
    
    					<?php if( get_field( "doctor_residency" ) ): ?>
    						<p><span class="doctor-background-title">Provider Residency:</span> <?php the_field( "doctor_residency" ); ?></p>
    					<?php endif ?>
    
    					<?php if( get_field( "doctor_internship" ) ): ?>
    						<p><span class="doctor-background-title">Provider Internship:</span> <?php the_field( "doctor_internship" ); ?></p>
    					<?php endif ?>
    
    					<?php if( get_field( "doctor_fellowship" ) ): ?>
    						<p><span class="doctor-background-title">Provider Fellowship:</span> <?php the_field( "doctor_fellowship" ); ?></p>
    					<?php endif ?>
    
    					<?php if( get_field( "doctor_board_status" ) ): ?>
    						<p><span class="doctor-background-title">Board Status:</span> <?php the_field( "doctor_board_status" ); ?></p>
    					<?php endif ?>
    			</div><!-- end single-doctor-container -->
    		</div> <!-- end tout-single-doctor -->
    
    		<?php $this_post = $post->ID; ?>
    		<?php $posts = get_field('doctor_specialties');?>
    		<?php if( $posts ): ?>
    
    		  <div class="large-4 medium-4 small-12 columns tout-more-specialists" data-equalizer-watch="bridge">
    		    <div class="single-specialty-container">
    		      
    		       <?php foreach( $posts as $post): ?>
    				        <?php setup_postdata($post); ?>
    			            <a class="doctor-specialty-link" href="<?php the_permalink(); ?>"><h2 class="related-specialties-title">More <?php the_title(); ?> Specialists</h2></a>    
    				    <?php endforeach; ?>
    				    <?php wp_reset_postdata(); ?>
    				<?php endif; ?>
    					
    
    				<?php while ( have_posts() ) : the_post(); ?>
    					<?php $doctors = get_posts(array(
    							'post_type' => 'our-providers',
    							'orderby' => 'meta_value',
    							'post__not_in' => array($this_post),
    							'meta_query' => array(
    								array(
    									'key' => 'doctor_specialties', // name of custom field
    									'value' => '"' . get_the_ID() . '"',
    									'compare' => 'LIKE'
    								)
    							)
    						));
    
    						?>
    						<?php if( $doctors ): ?>
    							<ul>
    							<?php foreach( $doctors as $doctor ): ?>
    								<li>
    									<a href="<?php echo get_permalink( $doctor->ID ); ?>">
    										<?php echo get_the_title( $doctor->ID ); ?>
    									</a>
    								</li>
    							<?php endforeach; ?>
    							</ul>
    						<?php endif; ?>
    
    					<?php endwhile; // end of the loop. ?>
    		    </div>
    		</div>
    		
    		<?php else: ?>
    		
    		<?php wp_reset_postdata(); ?>
    		  
    		<!-- If not providers with same specialty then display this div -->
    		<div class="large-4 medium-4 small-12 columns if-not-specialists" data-equalizer-watch="bridge">
    			
    			<!-- No Related Providers Content Here -->
    			
    		</div>
    
    		<?php endif; ?>
    
    		<div class="large-4 medium-4 small-12 columns tout-bridge-make-reservations_home" data-equalizer-watch="bridge">
    			<!-- Make Reservations Widget -->
    			<?php get_template_part( 'partials/global-custom-fields/make', 'reservations' ); ?>
    		</div>
    	
    	</div>
    <?php get_footer(); ?>
Viewing 6 posts - 1 through 6 (of 6 total)