Display multiple category name wordpress - wordpress

Looking to display all category names for wordpress posts on a specific post.
So if a post is in "web design, seo and social media" i want these category names to display on the post - and NOT all the other categories such as "app design" which it is not related too.
<p>
<?php $category = get_the_category();
echo $category[0]->cat_name;
echo " / ";
echo $category[1]->cat_name;
echo " / ";
echo $category[2]->cat_name;
?>
</p>
this works currently but if there is only 1 category then i get extra / on the end of the displayed text.
Can someone else with a loop that works better?

$categories = get_the_category();
$cat_id = $categories[0]->term_id;
foreach ( $categories as $i => $category ) {
echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" >' . esc_html( $category->name ).'</a>';
if ( $i < $count - 1 )
echo $separator;
}
Hope it will help you.

Your code show undefines the $count variable but when I use this code it shows some categories.
<?php
$categories = get_the_category();
$cat_id = $categories[0]->term_id;
foreach ( $categories as $i => $category ) {
echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" >' . esc_html( $category->name ).'</a>';
if ( $i < 'count - 1' )
echo $separator;
}
?>

Related

Page with category list with image (ACF)

I try to create list of child category with images thumbs.
I add new field to taxonomy (ACF plugin) “image_category” and now I try to add this image to the list:
<?php
$args = array('parent' => 3);
$categories = get_categories( $args );
$term = get_queried_object();
$image_category = get_field('image_category', $term);
foreach($categories as $category) {
echo '<div class="col-md-3 col-sm-12 col-xs-12 text-center pb-3"><img src="' . $image_category . '"><h3><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </h3> ';
echo '<p> <strong>Number post:</strong> '. $category->count . '</p></div>';
}
?>
I see category list, name, number post. Only image dont work.
Image link dont show. I have on consol only “img src=(unknown)”. What am I doing wrong?

Taxonomy Term Image 'post_per_page' doesn't work

This code working with image and title but post_per_page doesn’t work and trying
number' =>1
is work but if any taxonomy Less than one Or equal to one Info will not be showing.
<?php
$tax = 'studio';
$types = get_terms( array(
'post_per_page' => 1,
)
);
foreach($types as $type) { ?>
<?php
$term_link = get_term_link( $type );
$image = get_field('studio_avatar', 'studio_' . $type->term_id . '' );
if ( has_term( $type->term_id, 'studio')) {
echo '<a class="author author-avt inline" href="' . esc_url( $term_link ) . '">';
echo '<img class="si-user" src="' . $image['url'] . '" /> ';
echo '<span>'. $type->name . '</span>';
echo '</a>';
echo '<span class="posted-on inline">';
echo '<i class="si-clock"></i><time>'. $post_date = get_the_date("j F Y") .'</time>';
echo '</span>';
}
?>
<?php } ?>

How to display subcategories on click categories page wordpress categories

How to display sub categories on category page if category has subcategories else show posts of that categories wordpress category page
$id = $wp_query->get_queried_object_id(); // now you have the ID of the category
Now check if something is returned and do whatever from there:
$args = array('child_of' => $id);
$subcats = get_terms( $args );
if(!empty($subcats)){
foreach($subcats as $subcat) {
echo get_term_link( $subcat->slug, $subcat->taxonomy ); // for example
}
} else {
// do the usual stuff
}
You can try get_categories() with args child_of function like below.
$args = array('child_of' => 'category_id');
$categories = get_categories( $args );
foreach($categories as $category) {
echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
echo '<p> Description:'. $category->description . '</p>';
echo '<p> Post Count: '. $category->count . '</p>';
}
Also, $category->count ===0 then category has no post. we can use it to check that category's post.

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

Remove posts permalink (href)

I am trying to figure out a way to remove permalink's from posts with the category 'nolink'. I have tried a few java script attempts - but have not managed to get it to work.
<?php
query_posts( 'tag=Client-list' );
while ( have_posts() ) : the_post();
echo '<ul class="client-thumb-wrap">';
echo '<a href="';
the_permalink();
echo '">';
echo '<li class="';
$category = get_the_category( $custompost );
echo $category[0]->cat_name ;
echo ' ';
echo $category[1]->cat_name ;
echo ' ';
echo $category[2]->cat_name ;
echo ' ';
echo $category[3]->cat_name ;
echo '">';
echo '<img src="';
the_field('client_logo');
echo '">';
echo '</li>';
echo '</ul>';
endwhile;
wp_reset_query();
?>
has_term allows you to check if a post has a specific term assigned (or not), try:
$href = ( has_term( 'nolink', 'category' ) ) ? '#' : get_permalink();
echo '<a href="' . $href . '">';

Resources