Support

Account

Home Forums General Issues Display a custom field through category

Solved

Display a custom field through category

  • I have been using ACF for some months now and creating basic page templates to display information. I have found this plugin excellent.

    But now I am stuck. All I am attempting to do is output two separate lists in two side by side columns, with the first column listing schools in Queensland and the other listing schools in NSW. ‘Queensland’ and ‘NSW’ are categories. The custom post type is called ‘school_locations’. So my idea was to call up the relevant category for the relevant column and print the text custom field ‘school_name’. But I have had no success and I am sure that this must be relatively straightforward, but not for me it seems.

    Here is my now messy code as I have tried all manner of means to get this to work. And in my two columns you will see I currently have two different approaches.

    <?php
    /*
     * Template Name: School List
     */
    
    // Do the Custom Loop
    add_action( 'genesis_loop', 'sf_custom_loop' );
     
    function sf_custom_loop() {
    // Outro Text (hard coded)
    	$args = array(
    		'post_type' => 'school_locations',
    		'orderby' => 'title',
    		'order' => 'ASC',
    		'posts_per_page'=> '100',  // overrides posts per page in theme settings
    	);
    
    // Remove post info and meta info
    remove_action('genesis_entry_footer', 'genesis_post_meta');
    remove_action( 'genesis_entry_header', 'genesis_post_info', 12 );
    
    // Remove default content for this Page Template
    remove_action('genesis_entry_header', 'genesis_do_post_image');
    
    remove_action('genesis_entry_content', 'the_excerpt');
    
    $loop = new WP_Query( $args );
    	if( $loop->have_posts() ):
    				
    		while( $loop->have_posts() ): $loop->the_post(); global $post;
    			
    			echo '<div class="col50-1">';
    			$posts = get_posts(array(
    			'post_type'		=> 'school_locations',
    			'posts_per_page'	=> -1,
    			'meta_key'		=> 'state',
    			'meta_value'		=> 'queensland'
    ));
     
    if($posts)
    {
    	foreach($posts as $post)
    	{
    echo '<p class="tx-c">' . get_field( 'school_name' ). '
    ';
    	}
    }
               		 echo '</div>';
    
    			echo '<div class="col50-2">';
    			if(is_category( 'queensland' )) :
    			echo '<p class="tx-c">' . genesis_get_custom_field( 'school_name' ). '
    ';
    			endif;		
    			echo '</div>';
    
    		endwhile;
    
    	endif;
    	
    	echo '</div><!-- end .entry-content -->';
    	echo '</div><!-- end .page .hentry .entry -->';	
    } 
    
    /** Move Post Info */
    remove_action('genesis_entry_header','genesis_post_info');
    remove_action('genesis_entry_footer','genesis_post_meta');
     
    genesis();

    Can anyone point me in the right direction? Thanks

  • Hi @johnhillcoat

    If a ‘category’ is a standard WP taxonomy, then I don’t think this is an ACF question.

    You should ask general WP questions on a WP forum such as stack overflow.

    Thanks
    E

  • Thanks Elliot. I have it sorted.

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

The topic ‘Display a custom field through category’ is closed to new replies.