Advanced Custom Fields category link - php

I'm struggling with this even after looking at the documentation but all I'm wanting to do is link to a category. I have created a taxonomy field to select categories. Everything is working apart from when I add
I just get the ID of the category but I want the name. This is all within a repeater as I'm creating a menu but I need help getting the name and not the ID.
any help please?

If there is an option of selecting only one taxonomy.
Then write the query:
<?php $term_id = get_sub_field('category_link');
if( $term_id):
$term_name = get_cat_name( $term_id ) ;
$term_url = get_category_link( $term_id ); ?>
<?php echo $term_name; ?>
<?php endif; ?>
OR
If there are multiple category terms, then place this query:
<?php
$terms = get_sub_field('category_link');
if( $terms ):
foreach( $terms as $term_id ):
$term_name = get_cat_name( $term_id ) ;
$term_url = get_category_link( $term_id ); ?>
<?php echo $term_name; ?>
<?php endforeach; endif; ?>
Now place your category term url & name in the anchor tag.

Try this. It can give all your terms under the preferred taxonomy. Just give your taxonomy name in the below code
<?php $terms= get_terms( array(
'taxonomy' => 'taxonomy name',
) );
foreach ( $terms as $term ) {
echo $term->name;
}
?>

First of all, let me know have you selected Term_id in the custom field which you have created & also let me know you can select one or more taxonomy through that custom field.

Related

woocommerce get_terms filter based on product category

I am just hoping that someone might help me.
My problem is in woocommerce, I would like to list terms for brands depending on the current product category.
But all I know for now is to list all terms and this is fine when in shop page but I also like to have a filter on product category and also to count available items for that product category.
Thanks in advance
<?php
$args = array(
'parent' => 0
);
$terms = get_terms( 'yith_product_brand', $args );
foreach( $terms as $term ):
$term_link = get_term_link( $term ); ?>
<li><?php echo $term->name; ?> <span class="count">(<?php echo $term->count; ?>)</span></li>
<?php
endforeach; ?>

Get taxonomy term of a custom post type

Trying to get a custom "category" of a custom post type in wordpress. With no luck. Went through a whole bunch of answers, but none worked.
Here is the latest one i've tried
<?php // Get terms for post
$terms = get_the_terms( $post->ID , 'quizes' );
if ( $terms != null ){
foreach( $terms as $term ) {
print $term->slug ;
unset($term);
} } ?>

Display Custom Taxonomy Field in sidebar

Hi Guys, I'm trying to display a taxonomy field called clpr_store_video in my Wordpress sidebar for each store is it possible to call the field value outside the loop on the sidebar, but yet attached to the post id?
I don't really know if its possible or how to do that.
EDITED
What I've tried from another tutorial but no luck.
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$subheading = get_field('clpr_store_video', $taxonomy . '_' . $term_id);
echo $subheading;
See code block to display taxonomy associated with post and list of taxonomies and their links:
If you wish to get taxonomy associated with particular post
$terms = get_the_terms( get_the_ID(), 'clpr_store_video' );
foreach ( $terms as $term ) {
echo $subheading = $term->name;
}
If you wish to get list and link of all taxonomies
<ul>
<?php
$taxonomies = get_terms('clpr_store_video', array("fields" => "all"));
foreach ($taxonomies as $taxonomy) {
echo '<li>'.$taxonomy->name.'</li>';
}
?>
</ul>

Wordpress: Displaying only selected taxonomy tags

In Wordpress I created a taxonomy called "Cities" for a CPT called "People". I want to display all taxonomy tags selected for a person in the template of the single.php-file.
What is the code for displaying only those elements selected?
I use the following code in the single-people.php:
<?php
$taxonomy = 'cities';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term -> name; ?></li>
<?php
} ?>
</ul>
<?php endif; ?>
I want only the selected taxonomy tags without link.
I understand you want to display all the terms of a taxonomy selected for a single post.
You need to use the function wp_get_post_terms() like this:
$taxonomy = 'cities';
$post_terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "names" ) );
foreach ( $post_terms as $term ) {
echo $term;
}
This will print all the names of the cities the current post is related to. Now you have to adapt this code to your page and, if you don't want links, just do not use <a> tags...
$wrap = 'hello';
$terms = wp_get_post_terms(get_the_ID( ), $wrap, array("fields"=>"names"));
foreach ( $terms as $term ) {
echo $term;
}
in the place of hello you take the taxonomy name and it surely run

Wordpress Loop - Show posts from custom taxonomy terms in heirarchical order

ok, here's what I'm trying to do:
I've got a custom post type called drinks-menu, a taxonomy called drinks-menu-categories, and a page template called template-drinks-menu.php.
The taxonomy has a bunch of terms that are heirarchical - Wine, with children White and Red; Beer, with children Pilsener, Stout, etc...
I want to use one loop to display all the posts from these terms in the same order that they're ordered by in the admin. Here's the code I've got so far:
<?php
$post_type = 'drinks-menu';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array('post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );
foreach( $terms as $term ) :
echo '<h1>'.$term->name.'</h1>';
if ( $term->description !== '' ) {
echo '<div class="page_summary">'. $term->description .'</div>';
}
echo '<br>';
$posts = new WP_Query( "taxonomy=$taxonomy&term=$term->slug&posts_per_page=-1&orderby=id&order=DESC" );
if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post();
?>
<?php the_title(); ?>
<?php
if( get_field( "price" ) ): ?>
<?php echo '<span">'; the_field( "price" ); echo ' | '; the_field( "abv" ); echo '</span>';?>
<?php endif;
echo '<em>'; the_excerpt(); echo '</em><br><br>';
endwhile; endif;
endforeach;
endforeach;
?>
This is working well to bring all the posts from the terms onto the page, but not in order. You can see I tried taxonomy=$taxonomy&term=$term-slug&posts_per_page=-1&orderby=id&order=DESC but it's not working, everything shows in alphabetical order.
Any Wordpress gurus who can point me in the right direction? I hope I've been clear about the issue. Thanks :-)
Posts ordered in admin page by "menu_order";
If u want to order posts using query_posts (WP_Query constructor) u should use value of variable "orderby" in upper case -> "ID".

Categories