Get all taxonomy term and their acf image field - wordpress

I am very new to wordpress theme development and I have created a custom post and custom taxonomy . I have created a image acf on taxonomy now I want to show all the taxonomy term and their acf fields on front-page . I can get all term by using get_terms() function but I don't know how to get acf field of that taxonomy.
$terms = get_terms(array(
"taxonomy" => "categories",
"hide_empty" => false ));
foreach($terms as $term):
echo $term->name;
endforeach;
I want term name and image acf field of that term on front-page.php . Any advice will be helpful and thanks in advance.

Please try this code
<?php
$terms = get_the_terms(get_the_ID(), "categories");
if (!empty($terms)): ?>
<ul>
<?php foreach ($terms as $term): ?>
<li class="<?php echo $term->slug; ?>">
<img src="<?php the_field("image_field_name", $term); ?>" />
</li>
<?php endforeach; ?>
</ul>
<?php endif;
?>
For more details you can check this documentation.

Related

Display ACF image field inside foreach loop

I have created a custom taxonomy for my custom post type.
Each term only needs to display its name (which works with $term->name;) and an image. For the image I am using ACF (as I usually do for custom fields on a page or post). It is set up and I can select an image when I add a new term in the dashboard, but how do I display it in a foreach loop? The usual get_field() isn't showing anything.
<ul class="taxonomy-terms">
<?php
$terms = get_terms( array(
'taxonomy' => 'my_taxonomy',
'hide_empty' => false,
) );
?>
<?php foreach ( $terms as $term) { ?>
<li>
<img src="<?php the_field('image'); ?>" />
<?php echo $term->name; ?>
</li>
<?php } ?>
</ul>
The issue being with the_field('image');
The image field is set to return the image url.
Maybe you should add your taxonomy as a parameter when calling the image, try :
<?php if( get_field('image') ): ?>
<img src="<?php the_field('image', $term); ?>" />
<?php endif; ?>
It's better to test the Image Field as you don't wan't to show an empty <img> tag.
Also please check that you didn't change the field name in the Image Field settings.
You can read about that here : https://www.advancedcustomfields.com/resources/image/
And here :
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
Let's hope this works,
Have a good day.
You should pass the current term of the loop as the second parameter of the ACF function:
<?php the_field('image', $term); ?>
See here:
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

How to show wordpress parent catgorie page show subcategories list

It's possible parent categories show subcategories list (name,image,description)
Example
Parent Category:
Subcategory name 1.1(Title)
Subcategory image
Subcategory description
Subcategory name 1.2(Title)
Subcategory image
Subcategory description
Thanks
I have solved this. Please find the code given below,
<?php $this_category = get_category($cat); ?>
<ul class="product_list">
<?php
$id = get_query_var('cat');
$args = array( 'parent' => $id );
foreach (get_categories($args) as $cat) : ?>
<li>
<img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" />
<?php echo $cat->cat_name; ?>
<p><?php echo $cat->description; ?></p>
</li>
<?php endforeach; ?>
</ul>
Thanks,
Vino
Yes
<?php wp_list_categories('child_of=parent_cat_id'); ?>
Referance This

Showing Categories in WordPress loop

HI I have added a loop code to a WP page to display a list of thumb of posts.
It works fine, but it shows each post I have published, even if isn't in the categories choosen. It's a problem because some posts shouldn't be showed!
Could you help me please?
<?php $posts = get_posts('category=Products&numberposts=-1');
foreach($posts as $post) : setup_postdata($post);
?><li><div class="fotoBoxContent"><a class="fotoBox" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); the_title(); ?></a></div></li>
<?php endforeach; ?>
Posts of category "Products" are showed, posts of categories child of "Products" are showed too, but are showed posts of other categories including "uncategorized".
PS: is printed, below the title of the page, "cat : 11,10,13,14,16,9,12, | " there is method to avoid this printing? thanks to everyone
Is "Products" the actual slug of the category you're trying to grab? I would double-check that. You want the category's slug, not its display name.
The category has to be the ID not the name. Also, try setting up the get_posts() using an arguments array instead of doing it inline:
<?php
$args = array(
'category' => '*ID*',
'numberposts' => -1
);
$posts = get_posts($args);
foreach($posts as $post) : setup_postdata($post); ?>
<li>
<div class="fotoBoxContent">
<a class="fotoBox" href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); the_title(); ?>
</a>
</div>
</li>
<?php endforeach; ?>
From WordPress Codex - Note: The category parameter needs to be the ID of the category, and not the category name.
http://codex.wordpress.org/Template_Tags/get_posts

Iterate through custom post type by custom taxonomy type? (Ordering wordpress posts by category, or displaying custom post type by taxonomy term)

I want to have a page that shows all posts, separated by category. The idea is to get the categories, and then iterate through all posts for each category. The problem is complicated by the fact that I want to iterate through all posts of a given custom type, using a custom taxonomy as the categories. (Running Wordpress 3)
In my functions.php, my custom post type is registered as "video" and the custom taxonomy as "video_types".
In my custom page template that is supposed to show all videos arranged by category, this is the code that isn't returning any posts (and they're there, I checked):
<?php
$categories = get_categories(array(
'taxonomy' => 'video_types'
));
foreach ($categories as $cat):
?>
<section id="<?php $cat->slug ?>" class="video-category">
<?php
query_posts(array(
'cat' => $cat->cat_ID,
'posts_per_page' => -1
));
?>
<h2><?php single_cat_title(); ?></h2>
<p class="description"><?php echo category_description($cat->cat_ID); ?></p>
<?php while (have_posts()) : the_post(); ?>
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<article class="video">
<h3><?php the_title(); ?></h3>
<p>
<?php the_content() ?>
</p>
</article>
<?php endwhile; ?>
</section>
<?php endforeach; ?>
Jeez, once you figure out that each item of a custom taxonomy is called a term (not immediately obvious in the wordpress docs for the noob), its all much simpler to search for. This solution is easier to understand without all the custom query stuff.
<?php
// A term is an item of a taxonomy (e.g. "Promotional" could be a term for the taxonomy "video_type")
// ...so $categories could be $terms and it would still make sense
$categories = get_terms('taxonomy_name');
foreach( $categories as $category ):
?>
<section class="category-<?php echo $category ?>">
<h2><?php echo $category->name; // Print the cat title ?></h2>
<p class="description"><?php echo $category->description ?></p>
<div class="<?php echo $category->post_type ?>-list">
<?php
//select posts in this category (term), and of a specified content type (post type)
$posts = get_posts(array(
'post_type' => 'custom_post_type_name',
'taxonomy' => $category->taxonomy,
'term' => $category->slug,
'nopaging' => true, // to show all posts in this category, could also use 'numberposts' => -1 instead
));
foreach($posts as $post): // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
?>
// Now you can do things with the post and display it, like so
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<?php
// Getting custom field data example
echo get_post_meta($post->ID, 'field_key', true);
?>
<?php the_content() ?>
</article>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
And then any gaps in understanding can be filled by searching the functions above in the wordpress codex. In the above code, for my specific application, custom_post_type_name would be video, and taxonomy_name would be video_type (or video_types, I forget).
You might try another approach. Try using get_posts to get your posts sorted by your custom taxonomy, set up a variable that is initially an empty string (called $current_cat or something), and with each loop of the results, check the taxonomy and compare it with $current_cat - if different, print out a header for the new category and then the entry, if the same, print out an entry as usual.
A clear issue with your code (I believe) is that you're not properly querying your custom taxonomy. You should be using simply taxonomy_name => 'value' within your query, a custom taxonomy won't be touched by cat in a query.
Let me know if you need more detail.
edit: More detail!
// get a list of categories, in this case your custom taxonomy (your_taxonomy_name)
$querystr = "SELECT terms.* FROM $wpdb->term_taxonomy tax LEFT JOIN $wpdb->terms terms ON tax.term_id = terms.term_id WHERE tax.taxonomy = 'your_taxonomy_name'";
$categories = $wpdb->get_results($querystr, OBJECT);
foreach( $categories as $category ): // begin a loop through those terms (categories in your custom taxonomy)
echo '<div class="category-header"><h3>'.$category->name.'</h3>'; // print the cat title
echo '<p class="category-description">'.strip_tags(term_description($category->term_id,'your_taxonomy_name')).'</p></div>'; // cat description
$posts = get_posts( array( 'your_taxonomy_name' => $category->name, 'post_type' => 'your_post_type' ) ) //select posts in this category, and of a specified content type
foreach($posts as $post) : // begin cycle through posts of this category
setup_postdata($post); //set up post data for use in the loop (enables the_title(), etc without specifying a post ID)
[ ... ] // do things with your post (display it)
endforeach;
endforeach;
That should do it - and this may be helpful for using get_posts.

WordPress 3.0.1 Query Custom Post Type with Custom Taxonomy

I have a custom post type with multiple taxonomy types. The issue is focused around just one of them.
I need to display all custom posts that have a taxonomy from featured-vendors checked. Right now, there is just one "featured" , but there may be more in the future, such as "highlight" or "sponsor" or something alone those lines.
But, for now, I just need to go through all the "vendors" custom post type, find ones with "featured" checked inside of the "featured-vendors" taxonomy.
I have some some posts out there that state it's not possible, but they were either from 2.8 or from the very first of this year and I know WordPress has released at least one update since then.
Thanks in advance!!
Query Custom Post Types by Taxonomy Term
This sample will assume:
the custom post type was registered with the taxonomy and the taxonomy was registerd with 'query_var' =>true and 'hierarchial' => true
The "checked" term will be the parent and any new ones can be added later on as children.
The Code:
<?php query_posts( array( 'featured-vendors' => 'checked' ) ); ?>
<?php if( is_tax() ) {
global $wp_query;
$term = $wp_query->get_queried_object();
$title = $term->name;
} ?>
<div class="my-custom-post">
<h3><?php echo($title); ?></h3> //this will show the name of the post type
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry"> <h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?> //shows the excerpt
</div><!--/end entry-->
</div><!--/end post-->
<?php endwhile; else: ?>
<p><?php _e('No Post Found','your_theme_text_domain'); ?></p>
<?php endif; ?>

Resources