Home › Forums › General Issues › 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
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
The topic ‘Display a custom field through category’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.