Question

Hi I have a blog where the categories are news, events and inspiration. These categories are selected using radio buttons on acf. I want to load news on single-news.php while events on single-events.php and inspiration on single-inspiration.php

is it possible to use the acf fields to do this? I have came across this code with wordpress default categories to load custom single.php

$post = $wp_query->post;
if (in_category('1')) {
    include(TEMPLATEPATH.'/single1.php');
} elseif (in_category('2')) {
    include(TEMPLATEPATH.'/single2.php');
} else {
    include(TEMPLATEPATH.'/single_default.php');
}

what can be used instead of the ‘in_category’ while using acf?

thanks guys.

Was it helpful?

Solution

assuming your acf field is called acf_category you could do this..

$post = $wp_query->post;

$post_cat = get_field('acf_category', $post->ID);
switch($post_cat){
  case "news":
    include(TEMPLATEPATH.'/single-news.php');
    break;
  case "events":
    include(TEMPLATEPATH.'/single-events.php');
    break;
  case "inspiration":
    include(TEMPLATEPATH.'/single-inspriation.php');
    break;
  default:
    include(TEMPLATEPATH.'/single.php');

}

note that you will probably have to change the switch cases (this can also be done with if/else) to reflect the actual slugs of your acf, these are just guesses. The important part is the get_field() function call, which will pull the appropriate acf field for the post id given. Here is a list of all the acf functions, very well documented

OTHER TIPS

/*
@  in_category(int [wp_category_id], int [post_id])
*/

$post = $wp_query->post;

if (in_category(1, $post->ID)) {
    include(TEMPLATEPATH.'/single1.php');
} elseif (in_category(2, $post->ID)) {
    include(TEMPLATEPATH.'/single2.php');
} else {
    include(TEMPLATEPATH.'/single_default.php');
}

just change the category id or slug inside the function like

in_category('1')

to

in_category('news')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top