Last active
October 8, 2022 01:28
-
-
Save AchalJ/0337767c92820b4169fb2a7f7d184706 to your computer and use it in GitHub Desktop.
PP Taxonomy Term - Custom Shortcode to get term image URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // ignore this | |
// Copy the code below to your current theme's functions.php file. | |
// Uses: [get_term_image_url] or [get_term_image_url term_id="PROVIDE TERM ID HERE"] | |
add_shortcode( 'get_term_image_url', function( $atts ) { | |
$term_id = 0; | |
if ( isset( $atts['term_id'] ) && ! empty( $atts['term_id'] ) ) { | |
$term_id = $atts['term_id']; | |
} else { | |
$queried_object = get_queried_object(); | |
// Maybe term archive. | |
if ( is_object( $queried_object ) && isset( $queried_object->term_id ) ) { | |
$term_id = $queried_object->term_id; | |
} | |
} | |
$id = get_term_meta( $term_id, 'taxonomy_thumbnail_id', true ); | |
$image = wp_get_attachment_image_src( $id, $settings->size ); | |
$url = ! empty( $image ) ? $image[0] : ''; | |
return $url; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment