0

I am using Taxonomy Thumbnail plugin to display category thumbnail, the plugin is working fine but I just want to get the url of image so that I can display it as a background image.

I have tried the following code but still it gives this format:

<img width="1024" height="618" src="https://example.com/wp-content/uploads/2019/07/Kathmandu-lumbini.jpg" class="attachment-post-thumbnail size-post-thumbnail" alt="kathmandu Pokhara Lumbini tour" srcset="https://example.com/wp-content/uploads/2019/07/Kathmandu-lumbini.jpg 1024w, https://example.com/wp-content/uploads/2019/07/Kathmandu-lumbini-300x181.jpg 300w, https://example.com/wp-content/uploads/2019/07/Kathmandu-lumbini-768x464.jpg 768w" sizes="(max-width: 1024px) 100vw, 1024px">

$background = wp_get_attachment_url(the_term_thumbnail( $term_taxonomy_id, $size = 'post-thumbnail', $attr = '' ));

My expected output would be

https://example.com/wp-content/uploads/2019/07/Kathmandu-lumbini.jpg
sticky bit
  • 36,626
  • 12
  • 31
  • 42

2 Answers2

0

From this post Regular Expression to extract src attribute from img tag you can do it using preg_match:

preg_match('/src\s*=\s*"(.+?)"/', $background, $matches);
$background_url = $matches[1];
Giacomo M
  • 4,450
  • 7
  • 28
  • 57
0

If you check the plugin code, in inc/template-tags.php there is a function get_term_thumbnail_id.

Pass the taxonomy term id to that, then you will get the thumbnail id back. You can then use that with wp_get_attachment_url.

misorude
  • 3,381
  • 2
  • 9
  • 16