Hi Kyle,
Glad that helped! Makes sense with having “Load value based on the post’s terms and update the post’s terms on save” selected…
Have a good one.
Jeff
Hi Kyle, I recreated the situation over here and have some verified code. Here’s a complete, but simplified page template (‘single-project.php’):
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php
// Grab all of the 'category' terms that have been applied to this 'project' and
// go through and output the category title and any posts that fall under the category.
$categories = get_the_terms( $post->ID, 'category' );
if ( $categories && ! is_wp_error( $categories ) ) : ?>
<?php foreach ( $categories as $category ) : ?>
<h3><?php echo $category->name; ?>:</h3>
<?php // Generate a custom query using the category's slug
$relatedPosts = new WP_Query( array('category_name' => $category->slug, 'posts_per_page' => -1) );
if ( $relatedPosts->have_posts() ) : ?>
<ul>
<?php while ( $relatedPosts->have_posts() ) : $relatedPosts->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif;
// Restore the global $post variable to the current post in the main query
wp_reset_postdata(); ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
As I was setting this up, I realized that it’s possible that the earlier snippet may not have worked for you because of the way the options may be set in the custom field definition. When adding the ‘taxonomy’ custom field, make sure that the field is set to return the ‘Term Object’ instead of the ‘Term ID’ so that it will work with the above code block.
OK, let me know how this works.
Thanks,
Jeff
Hi Kyle,
Sounds good. Let me know if you need me to take another shot at it.
Thanks,
Jeff
Hi Kyle,
Thanks for the additional explanation. Drop this into the code (within the loop) and give this a shot real quick to see if this outputs the category name and slug correctly. If so, you can then take the ‘category slug’ to generate a custom WP Query to grab the 5 most recent posts by category. If this doesn’t work it’s because I grabbed it from my code and attempted to modify it for your purposes without actually testing… : )
Just let me know if it doesn’t work and I’ll see what I can do. Here’s the code snippet:
<?php
$categories = get_the_terms( $post->ID, 'category' );
if ( $categories && ! is_wp_error( $categories ) ) : ?>
<ul>
<?php foreach ( $categories as $category ) : ?>
<li>Category Name: <?php echo $category->name; ?> / Category Slug: <?php echo $category->slug; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I realized that the forum has changed. Here’s a link to the original thread on the old forum:
Let me know if that helps!
Thanks,
Jeff
Hi Joe,
You could try this to strip out all HTML tags. Might be good to do this for RSS:
<?php $steps = get_field(‘step’); ?>
<?php if($steps) { ?>
<p>Directions: </p>
<div>
<?php foreach($steps as $step) { ?>
<p><?php echo strip_tags($step[‘add_step’]); ?></p>
<?php } ?>
</div>
<?php } ?>
Here’s the documentation: http://php.net/manual/en/function.strip-tags.php
Hi Kyle,
I’ve been doing some work with this field type lately and have gotten to know how to work with it a little more. Can you give me a little more information on what you need to do? Fill out the scope a little bit?
Also, as a sidenote, you may already be on top of this, but you can format your var dumps so they’re easier to view:
$values = get_field('projects_category');
print '<pre>';
print_r( $values );
print '</pre>';
Thanks,
Jeff
I had a similar need and ended up using jQuery to basically grab the sub-options page element, remove it from its default location below the “Options” top level nav item, and then insert it back into the DOM in another location where I wanted it to live. Maybe not the ideal solution, but it worked fine.
I had opened up a forum topic for this and remember documenting this solution, but I can’t seem to find it here in the forum any more. Let me know if you still need assistance and I can go back in and try to document what I did.
Thanks,
Jeff
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.