I have created a Custom Taxonomy called nationality, then I added it to let the user chose his/her nationality through a Custom Account Edit Form using ACF.
I am trying to get the Tax value into the Author.php page but it returns as Array ,
This is my code which I am using:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = wp_get_post_terms($post->ID, 'nationality' , $curauth);
if ($terms){
$out = array(
'name'=>'<span>Nationality: </span>'
);
foreach ($terms as $term)
{
$out[] = '<a " ' . ' href="' . esc_url(get_term_link( $term->slug, 'nationality')) .'">' .$term->name .'</a>' ;}
echo join( ' ', $out );
}
?>
I have also tried the following code:
<?php
$nationality = get_field('nationality', $curauth);
$nationality = get_field_object('nationality', $curauth);
echo $nationality['value'];
?>
still giving me an Array.
The Field type is select and Return Value is set to Term Object either Term ID
the error then is
Object of class WP_Term could not be converted to string
any Ideas how to make this Correct.
Thank you!
Ahmad
are you trying to get the custom taxonomy from the current user? try like this:
get_the_terms( get_the_ID(), 'nationality' )
and if you are sure it will always get only one (take into account some people have more than one nationality) just access the first element:
get_the_terms( get_the_ID(), 'nationality' )[0]
I have the right Code here :
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = get_field('nationality', $curauth);
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<?php echo $term->name ; ?>
<?php endforeach; ?>
<?php endif; ?>
Related
I have a custom field in an Apus theme of type multi checkbox and I need to display the selected values.
I am using this code:
<?php $keywords = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'keywords', true ); ?>
<?php if ( ! empty( $keywords ) ) : ?>
<li><span><?php echo esc_html__( 'Keywords:', 'apushome' ); ?></span> <?php echo esc_attr( $keywords ); ?></li>
<?php endif; ?>
What this code is showing is
Keywords: Array
How can I loop the field's values to show them on the page?
Best regards
Americo
you may also use
echo implode(",",$keywords);
maybe this would help if this is indexed array, otherwise use
foreach ($keywords as $val){
echo $val. "\n";
}
I'm using below variables to display category name.
<?php $postcat = get_the_category( $post->ID ); ?>
and
<?php echo esc_html($postcat[0]->name ); ?>
How i can get link to category?
Example: www.example.com/category/business
It's simple, You can used get_category_link() function.
// Get the URL of this category
$category_link = get_category_link( $category_id );
Please use the following code, that will do the work for you :
$categories = get_the_category();
if ( ! empty( $categories ) )
{
echo '' . esc_html( $categories[0]->name ) . '';
}
I'm trying to display the taxonomy term of a custom post type (like using <?php the_category( ' ' ); ?> in regular posts). The code below works but need to specify the taxonomy name, is there a way to use only the Post ID?
<?php
$terms = get_the_terms( $post->ID , 'taxonomy_name' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
Thanks in advance!
<?php print the_terms( $post->ID, 'taxonomy_name' , ' ' ); ?>
You cannot get the custom taxonomy terms without taxonomy name but the above code its shorter for you.
I found a way to do it. Using "get_post_taxonomies" and selecting the array witch cat[1]
<?php
$cat = get_post_taxonomies($post);
$terms = get_the_terms( $post->ID , $cat[1] );
// Loop
if ( $terms != null ){
foreach( $terms as $term ) {
$term_link = get_term_link( $term, $cat[1] );
// Print the name and URL
echo '' . $term->name . '';
unset($term); } }
?>
Try this.,
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "names"));
print_r($term_list);
You must use the taxonomy without taxonomy can not get term details.
Thanks you!
I have next code:
<?php
global $post;
$args = array( 'taxonomy' => 'product_cat');
$terms = get_the_terms($category->slug,'product_cat', $args);
$count = count($terms);
if ($count > 0) {
foreach ($terms as $term) {
echo '<div style="direction:rtl;">';
echo $term->description;
echo '</div>';
}
}
?>
The code will display category description. The problem - on sub-category it will display the sub-category description + the parent description.
How i can display the description separate: in parent - the parent description, and on sub - only the sub description?
Try this and let me know if it helped you
add_action( 'woocommerce_after_subcategory_title', 'custom_add_product_description',
12);
function custom_add_product_description ($category) {
$cat_id = $category->term_id;
$prod_term = get_term($cat_id,'product_cat');
$description= $prod_term->description;
echo '<div>'.$description.'</div>';
}
The answer:
<?php
global $post;
$terms = get_the_terms( 'product_cat',$post->ID);
echo '<div style="direction:rtl;">';
echo category_description( get_category_by_slug($terms)->term_id);
echo '</div>';
?>
You can use this code to display the product category description -
<?php global $post, $product;
$categ = $product->get_categories();
$term = get_term_by ( 'name' , strip_tags($categ), 'product_cat' );
echo $term->description; ?>
Anajanas answer works very well, It can also be modified to do what I needed
which was to display a ACF custom field of a woocoommerce product child category on the shop catgegory page.
This is my code to add in functions.php
12);
function xyz ($category) {
$cat_id = $category->term_id;
$prod_term = get_term($cat_id,'product_cat');
echo "<div class='designercatname'>".get_field('designer_name', $prod_term )."</div>";
}```
I'm trying to display a number of custom post taxonomy terms - but each within their own specific list item.
I have a site which is about garages. (developing on localhost so don't have a link yet).
I have a custom post type called Cars. Within this I have a custom post taxonomy with the make of the Car 'Ford' in this case.
Within 'Ford' is a list of custom post taxonomy terms of all the ford cars at that garage. 'GT', 'Sierra', 'Orion'.
Instead of listing the terms: 'GT', 'Sierra', 'Orion'. I want to show a picture of the car within a ul list.
I've created a sprite with all the images and want to loop through these setting the background position for each li item.
The first lot of code below displays a list of of the terms which is all good, but not what I want. Right at the bottom is the code which I've tried to add the list items to but just getting a blank screen...
Any help, much appreciated. Thanks
<?php if ( get_post_type() == cars ) { ?>
<div class="entry-meta-custom">
<?php
$terms = get_the_terms( $post->ID, 'ford' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
?>
<?php echo $on_draught; ?>
<?php endif; ?>
</div><!-- .entry-meta-custom -->
<?php } ?>
And here's where I've tried to add the list items.
<?php if ( get_post_type() == cars ) { ?>
<div class="entry-meta-custom">
<?php
$terms = get_the_terms( $post->ID, 'ford' );
if ( $terms && ! is_wp_error( $terms ) ) :
$draught_links = array();
foreach ( $terms as $term ) {
if ($term->name == 'gt') {
$term->name = '<li class="gt">' . $term->name . '</li>';
}
if ($term->name == 'sierra') {
$term->name = '<li class="sierra">' . $term->name . '</li>';
}
if ($term->name == 'orion') {
$term->name = '<li class="orion">' . $term->name . '</li>';
}
$draught_links[] = $term->name;
}
$on_draught = join( ", ", $draught_links );
?>
<?php echo '<ul>' . $on_draught . '</ul>; ?>
<?php endif; ?>
</div><!-- .entry-meta-custom -->
<?php } ?>
I worked it out. Below is the code for anyone that is looking to replace custom post taxonomy list terms with individual images which are link to the term archive.
Source http://codex.wordpress.org/Function_Reference/get_term_link
<?php if ( get_post_type() == cars ) { ?>
<div class="entry-meta-custom">
<?php
$terms = get_terms('ford');
echo '<ul>';
foreach ($terms as $term) {
echo '<li class="'.$term->name.'"></li>';
}
echo '</ul>'; ?>
</div><!-- .entry-meta-custom -->
<?php } ?>