Support

Account

Forum Replies Created

  • 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.

  • Ok I now have this which displays the category image and all the posts associated with the category beneath 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”><?php the_title(); ?></div>

    <?php }
    } ?>`

    What I now need is a way to show or hide the page links on a page by page basis.

    Anybody have any ideas how I might achieve this?

  • Another way would be to utilise the ‘Custom Sidebar Links’ plugin which allows you to choose what is displayed from tickboxes.

  • I’ve attached a screenshot. I am just wondering if I can somehow pull all the posts from all categories from each category under their correct category image, but make them all false, so they don’t display until they are chosen from a repeater field in the admin with a Page Link field using the Advanced Custom Fields: Link Picker add on for ACF.

    So false until picked and the page is updated then true so they display.

  • 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.

  • ok well looking through some of the google search it looks like I am not the only one that has had trouble with this. I’m aware its complex. It looks like there might be a few things to go at though which may get me halfway there at least. I suspect I will have to come back here for more help though. Thank you so far as this looks like it might be useful.

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