How can i display the wordpress taxonomy image into front-end? - php

I'm trying to display the taxonomy images into front-end, But I don't get how to use the code. Here I'm using a plugin Taxonomy Images [https://wordpress.org/plugins/taxonomy-images/] Please, can someone help me?
Here is my code below:
<?php $terms = get_terms('category');
foreach($terms as $term):
$args = array(
'orderby' => 'title',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array($term->slug)
)
));
$tag_query = new WP_Query( $args );
if($tag_query->have_posts()):
$image_url = apply_filters( 'taxonomy-images-queried-term-image-url', '' );
echo '<li>
' . wp_get_attachment_image( $term->image_id, 'detail' ) . '
'.esc_html($term->name).'
<p>'.esc_html($term->description).'</p>
</li>';
while($tag_query->have_posts()):$tag_query->the_post();
endwhile;
endif;
wp_reset_postdata();
endforeach;
?>

If you are using Taxonomy image plugin then use wp_get_attachment_image method to get the image.
Below is complete code for you.
check below code this will help you.
$terms = apply_filters( 'taxonomy-images-get-terms', '' );
if ( ! empty( $terms ) ) {
print '<ul>';
foreach ( (array) $terms as $term ) {
print '<li>' . wp_get_attachment_image( $term->image_id, 'detail' ) . ' <a href="#">'.esc_html($term->name).'
<p>'.esc_html($term->description).'</p></li>';
}
print '</ul>';
}

Use wp_get_attachment_image() funxtion to show taxnomy images at front-end.
wp_get_attachment_image( $term->image_id, 'detail' )
Note: Pass image_id in wp_get_attachment_image() function.
Reference
Updated Answer:
Yes, You can also get category name and description inside the loop.
$term->name
$term->description

Related

Exclude a specific taxonomy name

So I'm using wordpress and ACF. I created a CPT called products and a custom taxonomy named product_types. I have a loop that displays all product_type sections and each section contains products tied to it. So I'm looking for ways to easily filter new products or popular products thats why I come up with adding a dedicated category for it but I dont want it to exclude it from my post loop. pls see img attached
enter image description here
Here's the code I'm working with atm
<?php
$cpt_name = 'products';
$taxonomy_name = 'product_type';
// Retrieve the terms in the above taxonomy.
$terms = get_terms( $taxonomy_name );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
return;
}
echo '<div class="products-group">';
foreach( $terms as $term ) {
echo '<div class="products-category">';
echo '<h2 class="title-category">' . $term->name . '</h2>';
$args = array(
'post_type' => $cpt_name,
'tax_query' => array(
array(
'taxonomy' => $taxonomy_name,
'field' => 'slug',
'terms' => array( $term->slug ),
),
),
'posts_per_page' => -1,
'category__not_in' => array( 30 ),
);
$query = new WP_Query( $args );
echo '<div class="products-list">';
while ( $query->have_posts() ) {
$query->the_post();
$attachment_id = get_field('header_bg_image');
$size = "wide-thumbnail"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
if ( ! empty( $image ) ) {
$image_html = esc_url( $image[0] );
} else {
$image_html = 'https://rasons.ltd/wp-content/uploads/2020/02/interior-exterior-finish-800x400.jpg';
}
printf( '%s', get_the_permalink(), esc_attr( the_title_attribute( 'echo=0' ) ), get_the_title() );
}
echo '</ul>';
wp_reset_query();
echo '</div></div>';
} // end foreach.
echo '</div>';
?>

Wordpress Display Child Term of the Current Category Page?

Hi I am trying to display the child term of the current post category page.
However I can figure out what am missing here. Thanks
<?php
$current_tax = get_query_var( 'category' );
$current_term = get_queried_object()->term_id;
$child_terms = get_terms( array('category' => $current_tax,
'child_of' => $current_term,
'orderby' => 'name' ));
if ( ! empty( $child_terms ) && ! is_wp_error( $child_terms ) ) {
echo '<div class="post-index__cate">';
foreach( $child_terms as $child ) {
printf( '<a class="category-name" href="%s">', get_term_link( $child, $child->taxonomy ));
echo $child->name. '</a>';
}
echo '</div>';
}
?>
In your code the taxonomy should be “category”, so your arguments for get_terms() should have “taxonomy” => “category”. $current_tax is irrelevant in this code then.

Retrieve posts by custom taxonomy child on parent taxonomy page

So from the parent taxonomy view:
Child 1
post
post
post
Child 2
post
post
post
post
Child 3
post
post
post
post
I've scoured the internet for a solution to this, but nothing seems to be working. I'm able to successfully echo the term ID, but when I pass that into the query it returns nothing.
<?php
$terms = get_terms( 'ht_kb_category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<ul>';
foreach ( $terms as $term ) {
if ($term->parent != 0) {
echo '<li><h1>' . $term->name . '</h1></li>';
$the_query = new WP_Query( array(
'post_type' => 'post',
'tax_query' => array(
array (
'taxonomy' => 'ht_kb_category',
'field' => 'slug',
'terms' => $term->slug,
)
),
) );
while ( $the_query->have_posts() ) :
echo '<p>'. the_title() .'</p>';
endwhile;
/* Restore original Post Data
* NB: Because we are using new WP_Query we aren't stomping on the
* original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();
}
}
echo '</ul>';
} ?>
Finally found the solution after some detective work using the custom taxonomy URL. I was missing the 'post_type' and needed to query 'tag_id' instead of 'term_id'.
<?php $current_term_id = $hkb_current_term_id ?>
<?php
$myposts = get_posts(array(
'showposts' => -1,
'post_type' => 'ht_kb',
'tax_query' => array(
array(
'taxonomy' => 'ht_kb_category',
'field' => 'tag_id',
'terms' => $current_term_id)
))
);
echo '<ul>';
foreach ($myposts as $mypost) { ?>
<li><?php echo $mypost->post_title ?></li>
<?php }
echo '</ul>';
?>

Tag list filter for posts

I am trying to add a filter based on tags but im not sure how to proceed I have the tags being displayed in my post list with their anchor tags.
<?php
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
foreach ( (array) $tags as $tag ) {
echo '' . $tag->name . ' (' . $tag->count . ') ';
}
?></p>
I am using the following for my loop to get my posts
<?php
$myposts = get_posts('numberposts=-1&offset=$debut');
foreach ($myposts as $post):
setup_postdata($post);
?>
So My Questions
1 How do i go about adjusting my get_posts to apply a filter based on tag selection. Would this be easy to do with ajax
2 What is best way to generate the tag link list so that the above can be performed.
Edit
What im wanting to do is make sure I just get the url slug from tag/tag-3 how do i get this.
Edit
Ok i have made it a bit futher but its still only showing all posts in my tag page even though the single tag title is not blank so what gives ?.
<div class="post-list" style="width:80%;float:left">
<?php
$tag = single_tag_title('', false);
echo '<h2>Tag: '.$tag.'</h2>';
$args = array(
'taxonomy' => $tag,
'terms' => $tag,
);
$myposts = get_posts($args);
foreach ($myposts as $post):
setup_postdata($post);
?>
<div id="dateInfo" style="float:right;">
<?php the_date('Y.m.j'); ?> |
<?php comments_number( '0 hozzászólás', '1 hozzászólás', '% hozzászólás' ); ?>.
</div>
<div id="title_wrapper">
<h2><?php the_title(); ?></h2>
</div>
Try this put your taxonomy and term name here
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'genre',
'field' => 'slug',
'terms' => 'jazz'
)
)
);
$postslist = get_posts( $args );
if you want this done by ajax call then you will have to run an onclick function on <a> tags and call the above code in ajax response
this will return you with the tag filtered posts..
Need to pass taxonomy and terms by onclick function
Ok so as you have list of <a> tags, modify it to :
Get you taxonomy name and term ID is already coming here. it will be
passed in onclick function.
<?php
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC') );
foreach ( (array) $tags as $tag ) {
$term_ID = $tag->term_id;
$taxonomy_name = 'GET TAXONOMY NAME HERE';
echo '<a onclick="show_filter_tags('.$term_ID.','.$taxonomy_name.')" href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . ' (' . $tag->count . ') </a>';
}
?>
define the onclick function in the <a> tag
<script type="text/javascript">
function show_filter_tags(term_id,taxonomy_name){
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
$.post(
ajaxurl,
{
'action' : 'get_tagged_posts',
'term_id' : term_id,
'taxonomy_name' : taxonomy_name,
},
function(output){
console.log(output)
});
}
</script>
Now here is the function where query needs to be written which will filter the posts on basis of tags. Pu this in functions.php file in active theme folder
add_action('wp_ajax_get_tagged_posts', 'get_tagged_posts');
add_action('wp_ajax_nopriv_get_tagged_posts', 'get_tagged_posts');
function get_tagged_posts() {
$args = array(
'tax_query' => array(
array(
'taxonomy' => $_REQUEST['taxonomy_name'],
'field' => 'term_id',
'terms' => $_REQUEST['term_id'],
'operator' => 'IN',
)
)
);
$postslists = get_posts( $args );
foreach ($postslists as $postslist) {
# code...
}
}

WordPress: Get all tags from custom post type

I'm trying to get all the tags that are within my custom post type "resource".
The problem is that I'm outside of the loop and struggling to find a way to get the functionality to work with the custom post type.
I have the category setup also as "resource_category"
My current code is:
$tax = 'post_tag';
$terms = get_terms( $tax );
$count = count( $terms );
if ( $count > 0 ): ?>
<div class="post-tags">
<?php
foreach ( $terms as $term ) {
$term_link = get_term_link( $term, $tax );
echo '' . $term->name . ' ';
} ?>
</div>
<?php endif;
Can anyone help?
you are asking for the tags right, because the answer from 2015 is listing categories not tags
$args = array(
'type' => get_post_type(),
'orderby' => 'name',
'order' => 'ASC'
);
$tags = get_tags($args);
foreach($tags as $tag) {
var_dump($tag->name);
}
Tags are also WordPress taxonomy. So, you can get all the tags as like you get all terms
Read More
$tags = get_terms([
'taxonomy' => 'YOUR_CUSTOM_TAXONOMY',
'hide_empty' => false
]);
var_dump($tags);
You can also copy custom post taxonomy from Tags page URL.
http://localhost/wp-admin/edit-tags.php?taxonomy=YOUR_CUSTOM_POST_TAG_TAXONOMY_NAME&post_type=custom-post-type
$args = array(
'type' => 'resource',
'orderby' => 'name',
'order' => 'ASC'
);
$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> ';
}

Categories