Thanks for this code Elliot, it works just as I wanted. Not sure if you can help with this but I’ve got a related problem.
I’m creating a list of tabs with the repeater field and using the psuedo-class :checked to give the selected tab a different background colour.
The problem is that when using the repeater field the last tab rendered is the one selected when the user arrives on the page. What I’d like to happen is the first tab be selected instead.
My code is below and you can see it in operation here: http://londonsleepcentre.ae/sleep-apnea-osa-treatment-services/mandibular-devices/
<div class="tabs">
<?php if(get_field('additional_content')): $i = 0; ?>
<?php while(has_sub_field('additional_content')): $i++; ?>
<input type="radio" name="tabs" id="tab<?php echo $i; ?>" checked />
<label for="tab<?php echo $i; ?>" class="<?php the_sub_field('field_label'); ?>"><?php the_sub_field('extra_title'); ?></label>
<?php endwhile; ?>
<?php endif; ?>
<?php if(get_field('additional_content')): $i = 0; ?>
<?php while(has_sub_field('additional_content')): $i++; ?>
<div id="tab-content<?php echo $i; ?>" class="tab-content">
<?php the_sub_field('extra_content'); ?>
</div> <!-- #tab-content1 -->
<?php endwhile; ?>
<?php endif; ?>
</div>
Thanks Elliot, I thought it might have been beyond the range of what you could offer, thanks for your time trying to help.
The Burma Campaign wanted to construct their own pages so that a page could have two or three promo boxes on the left, some centre column content, and two or three promo boxes on the right.
I’ve attempted to solve this by creating a Promo Box CPT and then linking this to a page template with the ACF Relationship Field. In most places this works fine. Different promo boxes can be added to the same page. And the same promo box (Donate for example) can be added to different pages.
The sticking point is category specific news. News is stored as posts and each has a category (or categories) assigned to it. The challenge is to find a way to allow the top five posts of a certain category (say those categorised: “Crisis in Arkan State”), to be displayed on a specific page (in this case, the Crisis in Arkan Stage page).
I’ll happily go into more detail about how I’ve attempted to solve this problem but thought I’d stop here for now in case there’s a better way of achieving the result I’m looking for.
Perhaps screenshots will help.
Page 1 is a screenshot of the Promo Box CPT with all fields shown and the categories area highlighted.
Page 2 shows how the Promo Box is assigned to a page using a ACF Relationship (a probably important element I forgot to mention before now!). Towards the bottom of this page is the field Right 1. “Crisis in Arkan State – Latest News” is the relevant Promo Box I’m bringing in to the top right position on http://burma.23.co.uk/campaigns/crisis-in-arkan-state/.
Thanks E, I really appreciate your help with this.
Does that give you what you need?
Hi Elliot,
1. You are setting the variable $post_objects twice. Why?
Most probably because I don’t know what I’m doing :). I’ve removed the extraneous one on my code now.
2. I can’t see in your code where you are loading the category for.
I think this is my problem, I’m not sure how to load the category. What I’ve done is:
Does that make sense?
There is no custom field containing the category name, should there be?
I think the problem might be I’m not sure where your code should be added in order for me to test the variables. I’ve probably complicated things by (ironically) trying to display a simplified code example. The full and complete code is below.
<?php $post_objects = get_field('right_1'); if( $post_objects ): ?>
<div class="promoWrapper">
<div class="promoBox clearfix">
<?php $post_objects = get_field('right_1'); if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<h2>
<?php if(get_field('pb-link', $post_object->ID) != ""): ?> <!-- Gets the link for the promo box -->
<a href="<?php the_field('pb-link', $post_object->ID); ?>">
<?php endif; ?><!-- pb-link ENDS -->
<?php if(get_field('alternative_title', $post_object->ID) != ""): ?><!-- Gets the alternative title if there is one -->
<?php the_field('alternative_title', $post_object->ID); ?>
<?php else : ?>
<?php echo get_the_title($post_object->ID); ?>
<?php endif; ?><!-- alternative_title ENDS -->
<?php if(get_field('pb-link', $post_object->ID) != ""): ?></a><?php endif; ?>
</h2>
<?php if(get_field('pb-image', $post_object->ID) != ""): ?>
<a href="<?php the_field('pb-link', $post_object->ID); ?>">
<img src="<?php the_field('pb-image', $post_object->ID); $size = "promo-box" ?>" class="promoImage" alt="<?php echo get_the_title($post_object->ID); ?>" title="<?php echo get_the_title($post_object->ID); ?>" />
</a>
<?php endif; ?><!-- pb-image ENDS -->
<div><?php the_field('pb-content', $post_object->ID); ?></div>
<?php if(get_field('pb-link', $post_object->ID) != ""): ?><span><a class="moreLink" href="<?php the_field('pb-link', $post_object->ID); ?>">More...</a></span><?php endif; ?>
<!-- If the promo box is showing latest news or reports -->
<?php if(get_field('latest_news_or_reports', $post_object->ID) == "news"): ?>
<?php query_posts( array ('posts_per_page' => 5, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => '' ) ); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<li class="title"><a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php endif; wp_reset_query(); ?>
<?php endif; ?><!-- News/Reports promoBox ENDS -->
<?php endforeach; ?>
<?php endif; ?>
</div><!-- .promoBox ENDS -->
</div><!-- .promoWrapper ENDS -->
<?php endif; ?><!-- right_1 ENDS -->
Apologies Eliot, I’m still a real newbie with PHP, and the WordPress loop for that matter. I’m away right now though, so will get back to you with answers next week.
Of course I can. Doh! Thanks 🙂
I’m not exactly sure where this is supposed to go, so forgive the ignorance but if placed above or between any of the following nothing is returned:
<?php if(get_field('latest_news_or_reports', $post_object->ID) == "news"): ?>
<?php query_posts( array ('posts_per_page' => 5, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => '' ) ); ?>
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
If placed below I get:
Array
(
[36] => stdClass Object
(
[term_id] => 36
[name] => Events
[slug] => events
[term_group] => 0
[term_taxonomy_id] => 36
[taxonomy] => category
[description] =>
[parent] => 0
[count] => 3
[object_id] => 2470
)
)
Which seems to indicate its grabbing the details from the most recent post (which is something in the Events category with an ID of 2470).
I don’t see a Kitchen Sink button?
That threw up an error:
Warning: array_pop() expects parameter 1 to be array, boolean given in D:\Dropbox\NooMedia Upload\Burma Campaign\burmacampaign.dev\wp-content\themes\roots\templates\sidebar-right-section.php on line 33
On line 33 is: $term = array_pop( $terms );
The full code I’m using is
<?php
$terms = get_the_terms( $post->ID, 'category' );
$term = array_pop( $terms );
query_posts( array
(
'posts_per_page' => 5,
'order'=> 'DESC',
'orderby' => 'date',
'category__in' => $term->term_id
)
);
?>
Below the error was a list of the latest five news stories from all categories.
Hi Elliot,
the post type Promo Boxes (promo-boxes) has:
The promo box CPT uses the standard WP categories
The code above is what I’m running to return the latest five news stories – though that particular segment just the latest five from all categories, not a specific one.
Thanks for your help with this.
BTW, I realise this may well be a question for the creator of the custom post type plugin, feel free to send me that way if you think this is not in your remit.
I’m asking it here because I wonder if there’s something I can do with the custom field to get the category ID out.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.