I am a big fan of Advanced Custom Fields plugin
http://www.advancedcustomfields.com/
I developed a site where there are 2 types of categories:
normal and “custom”
most of the categories display normally like any other site showing
the posts in the archive.php page
and some posts archive are displaying in a special “category” template which I created which is styled differently called: “category-projects.php”
this is a special “archive” or “category” page which display different archive template for special posts.
this template works fine and using a function I created and everything looks good.
Now, I am trying to create ACF slider using the great repeater that I have in this special category template. so I created this new group called “post_slider_projects” and a simple repeater field for images called: “post_slide_projects”
Now, I assigned the rules to appear in category and I see it when I am browsing my admin categories section.
until this point everything is working. the only problem is that it is not displaying when I look at the site in this special category page..
here is the code I was using to display it:
<?php if(get_field('post_slider_projects')){ ?>
<div class="post-section">
<section class="flexslider">
<ul class="slides">
<?php while( has_sub_field('post_slider_projects') ){
$post_slide_projects = get_sub_field('post_slide_projects');
?>
<li class="latest-post"> <img alt="" src="https://wordpress.stackexchange.com/questions/143088/<?php echo $post_slide_projects;?>"/> </li>
<!--latest-post-->
<?php } ?>
<?php } else{ ?>
<img alt="" src="<?php bloginfo('template_directory'); ?>/assets/images/slider-img1.jpg"/>
<?php } ?>
</ul>
</section>
</div>
<?php } ?>
Any help will be appreciated
Thanks a lot
2 Answers
The ACF API functions normally work with the current post’s ID. Taxonomies and Users work a bit differently, you have to supply the ID of the object you want to fetch fields from.
$category_id = 'category_' . get_queried_object_id();
$field = get_field( 'post_slider_projects', $category_id );
See get_field
in the documentation for more info.