Support

Account

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

  • Well got a couple of bits of code sort of working:

    Firstly this one which may be good for development further:

    <?php if ( is_active_sidebar( 'sidebar-1' )  ) : ?>
    	<aside id="secondary" class="sidebar widget-area" role="complementary">
    		<?php dynamic_sidebar( 'sidebar-1' ); ?>
    			
    			<?php $terms = get_terms( 'category' );
     if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
         echo '<ul>';
         foreach ( $terms as $term ) {
           echo '<li>' . $term->name . ' - ' . '<img src="' . get_field('category_image', $term->taxonomy . '_' . $term->term_id) . '"></li>';
            
         }
         echo '</ul>';
    	 
     } ?>

    This retrieves the category image for each category. I can build on that because I could apply the category image into a div background. This would hide the category image if there are no links to show beneath if I apply the image into the div as a background and the div doesn’t have a height.

    I just don’t know how to pull the posts in that would appear beneath each category heading.

    Then there is this:

    <?php 
    
        $args = array( 
            'post_type' => 'page','posts_per_page' => -1
        );
    
        $query = new WP_Query($args);   
        $q = array();
    
        while ( $query->have_posts() ) { 
    
            $query->the_post(); 
            
            $a = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>';
    
            $categories = get_the_category();
    
            foreach ( $categories as $key=>$category ) {
    			
                $b = '<a href="' . get_category_link( $category ) . '">' . $category->name . '</a>';    
    
            }
    
            $q[$b][] = $a; // Create an array with the category names and post titles
        }
    
        /* Restore original Post Data */
        wp_reset_postdata();
    
        foreach ($q as $key=>$values) {
            echo $key;
    
            echo '<ul>';
                foreach ($values as $value){
    				echo '<img src="';
    				the_field('category_image', 'category_4');
    				echo '">';
                    echo '<li>' . $value . '</li>';
                }
    		
            echo '</ul>';
        }
    
    ?>

    This lists a category image above each post. If you look at the code I am having to call a category id – the_field(‘category_image’, ‘category_4’);

    to get the image to display.

    This would mean the same category image would display for all categories which would be wrong. On the other hand this retrieves all posts and puts them under the correct category titles they should appear under.

    So if I could somehow combine the above two pieces of code I would be halfway there.

    Although I still have to find some way of choosing which pages display from each category under each heading in the sidebar. I was thinking perhaps set them all somehow as ‘false’ until chosen from each page in the admin from a custom field menu then set them as ‘true’ if chosen.

    Both examples of code can be viewed here:

    http://151.252.3.6/~lewishickeyplatf/wordpress/

    You will see Code 1 Example and Code 2 Example

    Code 1 being the first bit of code above and Code 2 being the second bit of code above.