Support

Account

Home Forums Front-end Issues Displaying Sibling ACF Fields

Solving

Displaying Sibling ACF Fields

  • Pretty stuck on this so any help would be greatly appreciated as I haven’t been able to solve my issue.

    I working on a gallery that displays projects. The hierarchy of the pages is
    – gallery parent page
    — child individual project
    — child individual project

    The UI I’m aiming for is to have a slider that a user can click through to see a picture, headline, and a link to the next individual project page. The picture and headline I plan to pull from an ACF group field that each page is already currently displaying.

    I’ve tried following along at this thread but couldn’t quite get it to work.

    I understand that I’m going to need to use some method like query_posts to look for the sibling pages, but I’m unsure of which method to use and then how to even begin querying for specific ACF fields.

    So, if someone would kindly explain I’d be grateful. Here is what I have so far (not much as I’ve been stuck for a bit)

    
    <?php
        $args = array( 'numberposts' => 5, 'offset' => 1, 'category' => 1 ); 
        $posts = get_posts( $args )
    ?>
    <?php echo $this_page_id ?>
    <?php query_posts(array('showposts' => 20, 'post__not_in' => $this_page_id, 'post_type' => 'page')); while (have_posts()) { the_post(); ?>
    <div class="gallery">
        <!-- Inside of here is where I'd like to loop over all the 
             sibiling pages to display an image
             paragraph, and link -->
    </div>
    <?php } ?>
    
  • After doing some more reading I found that I can pass a pageID to have_rows and retrieve fields that way, so I thought I could maybe retrieve an array of all the siblings of a page and just loop through them. However, I can’t find the appropriate way to do this. Am I going about this right way? Is there a better way to implement to this?

    
    <?php
    $pageId = array-of-sibling-ids.
    ?>
    <?php 
     foreach ($pageId as $sibling) {
        if( have_rows('single_project_hero', $sibling) ): 
    
        while( have_rows('single_project_hero', $sibling) ): the_row();  
        $image   = get_sub_field('single_project_hero_image');
        $heading = get_sub_field('single_project_hero_headline');
        $teaser = get_sub_field('single_project_hero_teaser');
      ?>
      <?php if ($image): ?>
        <div class="singleProject__hero">
          <img src="<?php echo $image ?>"/> 
          <div class="singleProject__heroCopy centered-heading">
            <h2 class="title"><?php echo $heading ?></h2> 
            <p class="copy"><?php echo $teaser ?></p>
          </div>
        </div>
      <?php endif; ?>
    
      <?php endwhile; ?>
      <?php endif; ?>
    
  • I can’t give you exact code because I don’t have any handy.

    How you would get a list of sibling pages would depend. I’m not sure by reading your question if you are currently displaying the parent page or one of the other siblings.

    If you are displaying the parent page then what you need to do is do a WP_Query to get all of the child pages of the current page using the post_parent parameter https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters.

    If you are showing one of the sibling pages already then the first thing you need to do is to get the parent id of the current post https://codex.wordpress.org/Function_Reference/wp_get_post_parent_id and then use this value when doing the query I mentioned above.

  • Hey John,

    Thanks for replying. To clarify my question, I’m currently displaying one of the child pages, and on that page I’m trying to query for the ids of all the sibling pages. I tried the following:

    
    <?php
      $pageId = $wp_query->post->ID;
      $parentId = wp_get_post_parent_id( $pageId );
      $query = new WP_Query( array( 'post_parent' => $parentId ) );
    ?>
    

    However, in the $query object I don’t see any of the siblings nor their ids. Am I missing something?

  • i was able to figure it out by using get_posts.

    
      $siblingProjects = get_posts(array('post__not_in' => array($pageId), 'showposts' => 20, 'post_parent' => $parentId, 'post_type' => 'page'));
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Displaying Sibling ACF Fields’ is closed to new replies.