Support

Account

Home Forums Add-ons Repeater Field Repeater field for Sidebar Page Links Reply To: Repeater field for Sidebar Page Links

  • Hi James, I have resolved this…who says ACF can’t handle this sort of thing 😉

    I’ll post this on the forum as well just in case anyone wants to see it there. First things first, this is the code I ended up with and I will try to explain it:

    <?php $args = array (
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => true,
    );
    
    $terms = get_terms( 'category', $args );
    foreach ( $terms as $term ) {
    	echo '<br />';
        echo '' . '<img src="' . get_field('category_image', $term->taxonomy . '_' . $term->term_id) . '">';
        $post_args = array (
            'category_name' => $term->slug,
            'posts_per_page'   => '-1',
            'no_found_rows' => true
        );
    
        $query = new WP_Query( $post_args );
    
        while ( $query->have_posts() ) {
            $query->the_post(); ?>
    
    		<br /><div class="hidepost page-id-<?php echo $post->ID; ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
    
        <?php }
    } ?>

    In its simplest terms this code display a category image field created using ACF and all the pages assigned to the categories. Note these are pages, not posts!

    You will see in this code that I have applied <div class="hidepost page-id-<?php echo $post->ID; ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">

    Firstly the hidepost class can be used in CSS to hide all the pages that are now loaded into every page sidebar in the site.

    You would just use:

    .hidepost {
    display:none;
    }

    in your stylesheet.

    Secondly we have a class named: page-id-<?php echo $post->ID; ?>

    This pulls in the page id class so you end up with something like page-id-10 which can be used to show or hide the page link something like:

    .page-id-10 {
    display:block!important;
    }

    You can of course do this from the stylesheet but I needed to go a step further and have the display of these pages controlled through the admin, which is where ACF came in use again.

    I created a repeater field named Sidebar Links with a field in the repeater called Page Link which utilises the Page Link Field Type.

    This would normally output a url which was no use in my case as I needed it to output the page ID of the Page Link. So I delved into the ACF plugin itself and edited fields>page_link.php (I have kept a backup of the original).

    In page_link.php find around line 603:

    // convert $post to permalink
    			if( is_object($post) ) {
    				
    				$post = get_permalink( $post );
    			
    			}

    In mine I changed it to:

    // convert $post to permalink
    			if( is_object($post) ) {
    				echo '.page-id-';
    				echo $post->ID;
    				echo '{';
    				echo 'display:block!important;';
    				echo '}';
    				$post = ( '' );
    			
    			}

    In my template I can now use:

    <?php 
    
    // check if the repeater field has rows of data
    if( have_rows('sidebar_links') ):
    
     	// loop through the rows of data
        while ( have_rows('sidebar_links') ) : the_row();
    	    
    	the_sub_field('page_link');
    
        endwhile;
    
    else :
    
        // no rows found
    
    endif;
    
    		?>

    which outputs something like .page-id-10 {display:block!important;}

    So if I want to control the display of each field dynamically I can now apply this code into the header.php template in the <head></head> section like this:

    <style>
    <?php 
    
    // check if the repeater field has rows of data
    if( have_rows('sidebar_links') ):
    
     	// loop through the rows of data
        while ( have_rows('sidebar_links') ) : the_row();
    	    
    	the_sub_field('page_link');
    
        endwhile;
    
    else :
    
        // no rows found
    
    endif;
    
    		?></style>

    The above allows me to display the page link if it is selected using the ‘edited’ page link field.