0

I made this function that displays tags as an image grid

function execute_taxography() {
    $wpbtags = get_tags();
    $output.= '<div class="grid"><div class="taxography-grid"><ul>';
    foreach($wpbtags as $tag) {
        $output.= '<li class="item"><a href="'. get_tag_link($tag->term_id) .'" style="background-image: url(\'http://localhost/wordpress/wp-content/uploads/tags/' . $tag->slug . '.png\')"><span class="count">'. $tag->count .'</span><span class="taxography-name">'. $tag->name . '</span></a></li>';
    }
    $output.= '</ul></div></div>';
    return $output;
}

Now I made a custom taxonomy named "book" and I want to display it in the same way as the tags but I'm stuck on the loop!

Any ideas?

0

get_tags is just a wrapper around the more general taxonomy function get_terms. So you'll get the result you need with this:

function execute_taxography() {
    $wpbtags = get_terms( 'books' );
    $output.= '<div class="grid"><div class="taxography-grid"><ul>';
    foreach($wpbtags as $tag) {
        $output.= '<li class="item"><a href="'. get_term_link($tag->term_id, 'books' ) .'" style="background-image: url(\'http://localhost/wordpress/wp-content/uploads/tags/' . $tag->slug . '.png\')"><span class="count">'. $tag->count .'</span><span class="taxography-name">'. $tag->name . '</span></a></li>';
    }
    $output.= '</ul></div></div>';
    return $output;
}  

I've also replaced function get_tag_link with get_term_link.

Read about get_terms at https://codex.wordpress.org/Function_Reference/get_terms. Read about 'get_term_link` at https://codex.wordpress.org/Function_Reference/get_term_link

  • No worries! Also, you don't need to use get_tags() at all. get_tags() is basically the same as get_terms( 'post_tag' ). If you can input the taxonomy type as a variable you can find a way to implement both grids without duplicating code. – James Jones Dec 10 '15 at 6:00
  • Thank you for the heads up, much appreciated! BTW any ideas on adding pagination? – PavilionVI Dec 10 '15 at 19:51
  • That's really a separate issue, try asking another question on SO. – James Jones Dec 11 '15 at 1:21
  • This is extremely expensive to run – Pieter Goosen Dec 11 '15 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.