Get link to category - wordpress

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 ) . '';
}

Related

Get a Value in the Author Page from a Custom Taxonomy

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; ?>

ACF: How to get sub field in category.php page?

I have created fields for terms using ACF. I have also created category.php using the below code:
<?php
// load all 'category' terms for the post
$terms = get_the_terms( get_the_ID(), 'category' );
// we will use the first term to load ACF data from
if ( !empty( $terms ) ) {
$term = array_pop($terms);
if ( have_rows( 'page_banner' ) ) :
// loop through the rows of data
while ( have_rows( 'page_banner' ) ) : the_row();
$custom_field = get_sub_field( 'small_title', $term );
echo $custom_field;
endwhile;
else :
endif;
}
?>
But I am not getting the results, Need your help. Thank you.
I just checked the following code. Use the following code.
<?php
// get the current taxonomy term
$term = get_queried_object();
if ( have_rows( 'page_banner', $term ) ) {
while( have_rows( 'page_banner', $term ) ) {
the_row();
the_sub_field( 'small_title' );
echo '<br />';
}
}
Reference
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
https://www.advancedcustomfields.com/resources/have_rows/

Get taxonomy terms without taxonomy 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!

Using an ACF image object for a custom taxonomy within an echo block loop

I can't seem to pull a ACF image object for a custom taxonomy when used in this loop, which is made of echo blocks:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'product_type' );
$image = get_field('product_type_tax_image');
$hero_image = $image['sizes']['medium'];
foreach ( $terms as $term ) {
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
var_dump($hero_image);
// We successfully got a link. Print it out.
echo '<a href="' . esc_url( $term_link ) . '" class="product-grid- block" style="' . $hero_image[0] . '">';
echo '<div class="product-grid-inner-txt-wrap">';
echo '<h4>' . $term->name . '</h4>';
echo '<p>' . $term->description . '</p>';
echo '</div>';
echo '</a>';
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
The var dump just returns NULL. I've looked at this and this but don't seem to help. What am I doing wrong?
Take a look at this article:
http://www.advancedcustomfields.com/resources/get-values-from-a-taxonomy-term/
To pull a custom field from a Taxonomy Term, you need to add a second item to your get_field function call. Like this:
$image = get_field('product_type_tax_image', $term );
Also, it looks like you're trying to loop through $terms, but you're grabbing the custom field's value outside of your foreach loop, so that should get moved inside that loop, like so:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$terms = get_terms( 'product_type' );
foreach ( $terms as $term ) {
//get $term's image
$image = get_field('product_type_tax_image', $term );
$hero_image = $image['sizes']['medium'];
// The $term is an object, so we don't need to specify the $taxonomy.
$term_link = get_term_link( $term );
// If there was an error, continue to the next term.
if ( is_wp_error( $term_link ) ) {
continue;
}
var_dump($hero_image);
// We successfully got a link. Print it out.
echo '<a href="' . esc_url( $term_link ) . '" class="product-grid- block" style="' . $hero_image[0] . '">';
echo '<div class="product-grid-inner-txt-wrap">';
echo '<h4>' . $term->name . '</h4>';
echo '<p>' . $term->description . '</p>';
echo '</div>';
echo '</a>';
}
?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

wrap each custom post taxonomy term within an li - using a foreach loop

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 } ?>

Resources