-1

I'm using a plugin called "Multi-Column Tag Map" that lets you display all tags that were used and arrange them in a alphabetical order. This plugin works fine in a regular post-type but It's not working on a custom post-type that I created. I created a new post-type called 'books' and has an additional taxonomy(Tags) of 'Writers' and 'Tags'.

I followed this to create my custom post type: http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress

How do i customize this to fetch my tags from my custom post-type?

This is the plugin code:

-- code removed, updated version below.

Update:

The purpose of this plugin on my site is to list all tags that we're used for the books. So user can just go to that page and browse articles/books by tags that were used.

I also tried Helgatheviking solution, It's similar to the plugin (Multi-Column Tag Map). But I'll just be sticking on customizing the plugin for the meantime as the plugin already provide some formatting. I just need to deal with get_terms().

My current problem is when using Helgatheviking or the Multi-Column Tag Map plugin solution, they're both using get_terms(). and get_terms() only fetch tags that in my posts and not in my custom post-type ('books'). I wanted to fetch tags from my custom post type.

So I did a little search and found this get_terms_by_post_type() by Bainternet. I think this is the solution in fetching tags to a certain post-type get_terms().

So this is what I did (code below), Unfortunately I have an error in my foreach(). It said "Warning: Invalid argument supplied for foreach() in mctagmap.php on line 366"

Additional Details:

post-type i'm targeting = 'the_books'

taxonomy i'm targeting = 'book_tags', 'book_writers' -- This might sound dumb, but I use 'post_tag' below instead of any of those two because I want to target the two of them.

mctagmap.php (modified version)

<?php
/*
Plugin Name: Multi-column Tag Map 
Plugin URI: http://tugbucket.net/wordpress/wordpress-plugin-multi-column-tag-map/
Description: Multi-column Tag Map displays a columnized  and alphabetical (English) listing of all tags used in your site similar to the index pages of a book.
Version: 8.0
Author: Alan Jackson
Author URI: http://tugbucket.net
*/

/*  Copyright 2009-2011  Alan Jackson (alan[at]tugbucket.net)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/


/* get terms limited to post type 
 @ $taxonomies - (string|array) (required) The taxonomies to retrieve terms from. 
 @ $args  -  (string|array) all Possible Arguments of get_terms http://codex.wordpress.org/Function_Reference/get_terms
 @ $post_type - (string|array) of post types to limit the terms to
 @ $fields - (string) What to return (default all) accepts ID,name,all,get_terms. 
 if you want to use get_terms arguments then $fields must be set to 'get_terms'
*/

function get_terms_by_post_type($taxonomies,$args,$post_type,$fields = 'all'){
    $args = array(
        'post_type' => (array)$post_type,
        'posts_per_page' => -1
    );
    $the_query = new WP_Query( $args );
    $terms = array();
    while ($the_query->have_posts()){
        $the_query->the_post();
        $curent_terms = wp_get_object_terms( $post->ID, $taxonomy);
        foreach ($curent_terms as $t){
          //avoid duplicates
            if (!in_array($t,$terms)){
                $terms[] = $c;
            }
        }
    }
    wp_reset_query();
    //return array of term objects
    if ($fields == "all")
        return $terms;
    //return array of term ID's
    if ($fields == "ID"){
        foreach ($terms as $t){
            $re[] = $t->term_id;
        }
        return $re;
    }
    //return array of term names
    if ($fields == "name"){
        foreach ($terms as $t){
            $re[] = $t->name;
        }
        return $re;
    }
    // get terms with get_terms arguments
    if ($fields == "get_terms"){
        $terms2 = get_terms( $taxonomies, $args );
        foreach ($terms as $t){
            if (in_array($t,$terms2)){
                $re[] = $t;
            }
        }
        return $re;
    }
}


function wp_mcTagMap($options='') {

    $ns_options = array(
                    "columns" => "2",
                    "more" => "View More",
                    "hide" => "no",
                    "num_show" => "5",
                    "toggle" => "no",
                    "show_empty" => "yes",
                   );

    if(strpos($options, '|')) {        
    $options = explode("|",$options);
    } else {
    $options = explode("&",$options);
    }

    foreach ($options as $option) {

        $parts = explode("=",$option);
        $options[$parts[0]] = $parts[1];

    }

    if ($options['columns']) {
    $ns_options['columns'] = $options['columns'];
    } else {
    $options['columns'] = 2;
    }

    if ($options['more']) {
    $ns_options['more'] = htmlentities($options['more'], ENT_QUOTES);
    } else {
    $options['more'] = "View more";
    }

    if ($options['hide']) {
    $ns_options['hide'] = $options['hide'];
    } else {
    $options['hide'] = "no";
    }

    if ($options['num_show']) {
    $ns_options['num_show'] = $options['num_show'];
    } else {
    $options['num_show'] = 5;
    }

    if ($options['toggle']) {
    $ns_options['toggle'] = $options['toggle'];
    } else {
    $options['toggle'] = "no";
    }

    if ($options['show_empty']) {
    $ns_options['show_empty'] = $options['show_empty'];
    } else {
    $options['show_empty'] = "yes";
    }

    $show_empty = $options['show_empty'];
    if($show_empty == "yes"){
        $show_empty = "0";
    }
    if($show_empty == "no"){
        $show_empty = "1";
    }
    $list = '<!-- begin list --><div id="mcTagMap">';
    $gtargs = array('orderby' => 'count', 'order' => 'ASC',  'hide_empty' => $show_empty);
    $terms = get_terms_by_post_type('post_tag',$gtargs,'the_books','get_terms');
    $groups = array();



    if( $tags && is_array( $tags ) ) {
        foreach( $tags as $tag ) {
            $first_letter = strtoupper( $tag->name[0] );
            $groups[ $first_letter ][] = $tag;
        }
    if( !empty ( $groups ) ) {  
        $count = 0;
        $howmany = count($groups);

        // this makes 2 columns
        if ($options['columns'] == 2){
        $firstrow = ceil($howmany * 0.5);
        $secondrow = ceil($howmany * 1);
        $firstrown1 = ceil(($howmany * 0.5)-1);
        $secondrown1 = ceil(($howmany * 1)-0);
        }


        //this makes 3 columns
        if ($options['columns'] == 3){
        $firstrow = ceil($howmany * 0.33);
        $secondrow = ceil($howmany * 0.66);
        $firstrown1 = ceil(($howmany * 0.33)-1);
        $secondrown1 = ceil(($howmany * 0.66)-1);
        }

        //this makes 4 columns
        if ($options['columns'] == 4){
        $firstrow = ceil($howmany * 0.25);
        $secondrow = ceil(($howmany * 0.5)+1);
        $firstrown1 = ceil(($howmany * 0.25)-1);
        $secondrown1 = ceil(($howmany * 0.5)-0);
        $thirdrow = ceil(($howmany * 0.75)-0);
        $thirdrow1 = ceil(($howmany * 0.75)-1);
        }

        //this makes 5 columns
        if ($options['columns'] == 5){
        $firstrow = ceil($howmany * 0.2);
        $firstrown1 = ceil(($howmany * 0.2)-1);
        $secondrow = ceil(($howmany * 0.4));
        $secondrown1 = ceil(($howmany * 0.4)-1);
        $thirdrow = ceil(($howmany * 0.6)-0);
        $thirdrow1 = ceil(($howmany * 0.6)-1);
        $fourthrow = ceil(($howmany * 0.8)-0);
        $fourthrow1 = ceil(($howmany * 0.8)-1);
        }

        foreach( $groups as $letter => $tags ) { 
            if ($options['columns'] == 2){
            if ($count == 0 || $count == $firstrow || $count ==  $secondrow) { 
                if ($count == $firstrow){
                $list .= "\n<div class='holdleft noMargin'>\n";
                $list .="\n";
                } else {
                $list .= "\n<div class='holdleft'>\n";
                $list .="\n";
                }
                }
                }
            if ($options['columns'] == 3){
            if ($count == 0 || $count == $firstrow || $count ==  $secondrow) { 
                if ($count == $secondrow){
                $list .= "\n<div class='holdleft noMargin'>\n";
                $list .="\n";
                } else {
                $list .= "\n<div class='holdleft'>\n";
                $list .="\n";
                }
                }
                }
            if ($options['columns'] == 4){              
            if ($count == 0 || $count == $firstrow || $count ==  $secondrow || $count == $thirdrow) { 
                if ($count == $thirdrow){
                $list .= "\n<div class='holdleft noMargin'>\n";
                $list .="\n";
                } else {
                $list .= "\n<div class='holdleft'>\n";
                $list .="\n";
                }
                }
                }
            if ($options['columns'] == 5){
            if ($count == 0 || $count == $firstrow || $count ==  $secondrow || $count == $thirdrow || $count == $fourthrow ) { 
                if ($count == $fourthrow){
                $list .= "\n<div class='holdleft noMargin'>\n";
                $list .="\n";
                } else {
                $list .= "\n<div class='holdleft'>\n";
                $list .="\n";
                }
                }
                }

    $list .= '<div class="tagindex">';
    $list .="\n";
    $list .='<h4>' . apply_filters( 'the_title', $letter ) . '</h4>';
    $list .="\n";
    $list .= '<ul class="links">';
    $list .="\n";           
    $i = 0;
    foreach( $tags as $tag ) {
        $url = attribute_escape( get_tag_link( $tag->term_id ) );
        $name = apply_filters( 'the_title', $tag->name );
    //  $name = ucfirst($name);
        $i++;
        $counti = $i;
        if ($options['hide'] == "yes"){
        $num2show = $options['num_show'];
        $num2show1 = ($options['num_show'] +1);
        $toggle = ($options['toggle']);

        if ($i != 0 and $i <= $num2show) {
            $list .= '<li><a title="' . $name . '" href="' . $url . '">' . $name . '</a></li>';
            $list .="\n";
            }
        if ($i > $num2show && $i == $num2show1 && $toggle == "no") {
            $list .=  "<li class=\"morelink\">"."<a href=\"#x\" class=\"more\">".$options['more']."</a>"."</li>"."\n";
            }
        if ($i >= $num2show1){
               $list .= '<li class="hideli"><a title="' . $name . '" href="' . $url . '">' . $name . '</a></li>';
               $list .="\n";
        }
        } else {
            $list .= '<li><a title="' . $name . '" href="' . $url . '">' . $name . '</a></li>';
            $list .="\n";
        }   

    } 
        if ($options['hide'] == "yes" && $toggle != "no" && $i == $counti && $i > $num2show) {
            $list .=  "<li class=\"morelink\">"."<a href=\"#x\" class=\"more\">".$options['more']."</a>"."<a href=\"#x\" class=\"less\">".$options['toggle']."</a>"."</li>"."\n";
        }    
    $list .= '</ul>';
    $list .="\n";
    $list .= '</div>';
    $list .="\n\n";
        if ($options['columns'] == 3 || $options['columns'] == 2){
        if ( $count == $firstrown1 || $count == $secondrown1) { 
            $list .= "</div>"; 
            }   
            }
        if ($options['columns'] == 4){
        if ( $count == $firstrown1 || $count == $secondrown1 || $count == $thirdrow1) { 
            $list .= "</div>"; 
            }   
            }
        if ($options['columns'] == 5){      
        if ( $count == $firstrown1 || $count == $secondrown1 || $count == $thirdrow1 || $count == $fourthrow1) { 
            $list .= "</div>"; 
            }   
            }

        $count++;
            } 
        } 
    $list .="</div>";
    $list .= "<div style='clear: both;'></div></div><!-- end list -->";
        }
    else $list .= '<p>Sorry, but no tags were found</p>';

print $list ;

}
// end long code

// short code begins
function sc_mcTagMap($atts, $content = null) {
        extract(shortcode_atts(array(
                    "columns" => "2",
                    "more" => "View More",
                    "hide" => "no",
                    "num_show" => "5",
                    "toggle" => "no",
                    "show_empty" => "yes",
                    "name_divider" => "|", // added 09.02.11
                    "tag_count" => "no",
                    "exclude" => "",
                    "descriptions" => "no",
                    "width" => "",
                    "equal" => "no",
        ), $atts));

    if($show_empty == "yes"){
        $show_empty = "0";
    }
    if($show_empty == "no"){
        $show_empty = "1";
    }

    if($width){
        $tug_width = "style=\"width: ". $width ."px;\"";
    }
    if($equal == "yes" && $columns != "1"){ 
        $equalize = 'mcEqualize';
    }
    if($toggle != "no"){
        $toggable = "toggleYes";
    } else {
        $toggable = "toggleNo";
    }
    $list = '<!-- begin list --><div id="mcTagMap" class="'.$equalize.' '.$toggable.'">';
    //$tags = get_terms('post_tag', 'order=ASC&hide_empty='.$show_empty.''); // new code!
    $gtargs = array('orderby' => 'count', 'order' => 'ASC',  'hide_empty' => $show_empty);
    $terms = get_terms_by_post_type('post_tag',$gtargs,'the_books','get_terms');

    /* exclude tags */  
    foreach($tags as $tag){
        $fl = mb_substr($tag->name,0,1);
        $ll = mb_substr($tag->name,1);
        $tag->name = $fl.$ll;
        if (preg_match('/(?<=^|[^\p{L}])' . preg_quote($tag->name,'/') . '(?=[^\p{L}]|$)/ui', $exclude)) {
            unset($tag->name);
        }
    }

    $groups = array();
    if( $tags && is_array( $tags ) ) {
        foreach( $tags as $tag ) {  
        /* exclude tags */
        if(isset($tag->name)){  
            // added 09.02.11
            if (strlen(strstr($tag->name, $name_divider))>0) {
                $tag->name = preg_replace("/\s*([\\".$name_divider."])\s*/", "$1", $tag->name);
                $tagParts = explode($name_divider, $tag->name);
                $tag->name = $tagParts[1].', '.$tagParts[0];
            }

            $first_letter = mb_strtoupper( mb_substr($tag->name,0,1) ); /* Thanks to Birgir Erlendsson */
            $groups[ $first_letter ][] = $tag;
            ksort($groups);
        }
        }
    if( !empty ( $groups ) ) {  
        $count = 0;
        $howmany = count($groups);

        // this makes 2 columns
        if ($columns == 2){
        $firstrow = ceil($howmany * 0.5);
        $secondrow = ceil($howmany * 1);
        $firstrown1 = ceil(($howmany * 0.5)-1);
        $secondrown1 = ceil(($howmany * 1)-0);
        }


        //this makes 3 columns
        if ($columns == 3){
        $firstrow = ceil($howmany * 0.33);
        $secondrow = ceil($howmany * 0.66);
        $firstrown1 = ceil(($howmany * 0.33)-1);
        $secondrown1 = ceil(($howmany * 0.66)-1);
        }

        //this makes 4 columns
        if ($columns == 4){
        $firstrow = ceil($howmany * 0.25);
        $secondrow = ceil(($howmany * 0.5)+1);
        $firstrown1 = ceil(($howmany * 0.25)-1);
        $secondrown1 = ceil(($howmany * 0.5)-0);
        $thirdrow = ceil(($howmany * 0.75)-0);
        $thirdrow1 = ceil(($howmany * 0.75)-1);
        }

        //this makes 5 columns
        if ($columns == 5){
        $firstrow = ceil($howmany * 0.2);
        $firstrown1 = ceil(($howmany * 0.2)-1);
        $secondrow = ceil(($howmany * 0.4));
        $secondrown1 = ceil(($howmany * 0.4)-1);
        $thirdrow = ceil(($howmany * 0.6)-0);
        $thirdrow1 = ceil(($howmany * 0.6)-1);
        $fourthrow = ceil(($howmany * 0.8)-0);
        $fourthrow1 = ceil(($howmany * 0.8)-1);
        }




        foreach( $groups as $letter => $tags ) { 
            if ($columns == 2){
            if ($count == 0 || $count == $firstrow || $count ==  $secondrow) { 
                if ($count == $firstrow){
                $list .= "\n<div class='holdleft noMargin' ". $tug_width .">\n";
                $list .="\n";
                } else {
                $list .= "\n<div class='holdleft' ". $tug_width .">\n";
                $list .="\n";
                }
                }
                }
            if ($columns == 3){
            if ($count == 0 || $count == $firstrow || $count ==  $secondrow) { 
                if ($count == $secondrow){
                $list .= "\n<div class='holdleft noMargin' ". $tug_width .">\n";
                $list .="\n";
                } else {
                $list .= "\n<div class='holdleft' ". $tug_width .">\n";
                $list .="\n";
                }
                }
                }
            if ($columns == 4){             
            if ($count == 0 || $count == $firstrow || $count ==  $secondrow || $count == $thirdrow) { 
                if ($count == $thirdrow){
                $list .= "\n<div class='holdleft noMargin' ". $tug_width .">\n";
                $list .="\n";
                } else {
                $list .= "\n<div class='holdleft' ". $tug_width .">\n";
                $list .="\n";
                }
                }
                }
            if ($columns == 5){
            if ($count == 0 || $count == $firstrow || $count ==  $secondrow || $count == $thirdrow || $count == $fourthrow ) { 
                if ($count == $fourthrow){
                $list .= "\n<div class='holdleft noMargin' ". $tug_width .">\n";
                $list .="\n";
                } else {
                $list .= "\n<div class='holdleft' ". $tug_width .">\n";
                $list .="\n";
                }
                }
                }

    $list .= '<div class="tagindex">';
    $list .="\n";
    $list .='<h4>' . apply_filters( 'the_title', $letter ) . '</h4>';
    $list .="\n";
    $list .= '<ul class="links">';
    $list .="\n";           
    $i = 0;

    uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') ); // addded 09.02.11

    foreach( $tags as $tag ) {
        /* exclude tags */
        if(isset($tag->name)){
        // added 9.28.11
        if($tag_count == "yes"){
            $mctagmap_count = ' <span class="mctagmap_count">('.$tag->count.')</span>';
        }

        $url = attribute_escape( get_tag_link( $tag->term_id ) );
        $name = apply_filters( 'the_title', $tag->name );
        if($descriptions == "yes"){
            $mctagmap_description = '<span class="tagDescription">' . $tag->description . '</span>';
        }
        //$name = ucfirst($name);
        $i++;
        $counti = $i;
        if ($hide == "yes"){
        $num2show = $num_show;
        $num2show1 = ($num_show +1);
        //$toggle = ($options['toggle']);

        if ($i != 0 and $i <= $num2show) {
            $list .= '<li><a title="' . $name . '" href="' . $url . '">' . $name . '</a>'. $mctagmap_count . $mctagmap_description . '</li>';
            $list .="\n";
            }
        if ($i > $num2show && $i == $num2show1 && $toggle == "no") {
            $list .=  "<li class=\"morelink\">"."<a href=\"#x\" class=\"more\">".$more."</a>"."</li>"."\n";
            }
        if ($i >= $num2show1){
               $list .= '<li class="hideli"><a title="' . $name . '" href="' . $url . '">' . $name . '</a>' . $mctagmap_count . $mctagmap_description . '</li>';
               $list .="\n";
        }
        } else {
            $list .= '<li><a title="' . $name . '" href="' . $url . '">' . $name . '</a>' . $mctagmap_count . $mctagmap_description . '</li>';
            $list .="\n";
        }   
        }   

    }
        if ($hide == "yes" && $toggle != "no" && $i == $counti && $i > $num2show) {
            $list .=  "<li class=\"morelink\">"."<a href=\"#x\" class=\"more\">".$more."</a>"."<a href=\"#x\" class=\"less\">".$toggle."</a>"."</li>"."\n";
        }    

    $list .= '</ul>';
    $list .="\n";
    $list .= '</div>';
    $list .="\n\n";
        if ($columns == 3 || $columns == 2){
        if ( $count == $firstrown1 || $count == $secondrown1) { 
            $list .= "</div>"; 
            }   
            }
        if ($columns == 4){
        if ( $count == $firstrown1 || $count == $secondrown1 || $count == $thirdrow1) { 
            $list .= "</div>"; 
            }   
            }
        if ($columns == 5){     
        if ( $count == $firstrown1 || $count == $secondrown1 || $count == $thirdrow1 || $count == $fourthrow1) { 
            $list .= "</div>"; 
            }   
            }

        $count++;
            } 
        } 
    $list .="</div>";
    $list .= "<div style='clear: both;'></div></div><!-- end list -->";
        }
    else $list .= '<p>Sorry, but no tags were found</p>';

return $list;

}

add_shortcode("mctagmap", "sc_mcTagMap");
// end shortcode



function mctagmap_donate($links, $file) {
$plugin = plugin_basename(__FILE__);
// create link
if ($file == $plugin) {
return array_merge( $links, array( sprintf( '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=GX8RH7F2LR74J" target="_blank">Donate to mctagmap development</a>', $plugin, __('Donate') ) ));
}
return $links;
}
add_filter( 'plugin_row_meta', 'mctagmap_donate', 10, 2 );

// the JS and CSS
add_action('wp_head', 'mcTagMapCSSandJS');
function mcTagMapCSSandJS(){
$mctagmapVersionNumber = "8.0";
$mctagmapCSSpath = './wp-content/themes/'.get_template().'/multi-column-tag-map/mctagmap.css';
    echo "\n";
    if(file_exists($mctagmapCSSpath)){
        echo '<link rel="stylesheet" href="'.$mctagmapCSSpath.'?ver='.$mctagmapVersionNumber.'" type="text/css" media="screen" />';
    } else {
        echo '<link rel="stylesheet" href="'.WP_PLUGIN_URL.'/multi-column-tag-map/mctagmap.css?ver='.$mctagmapVersionNumber.'" type="text/css" media="screen" />';
    }
    echo "\n";
    echo '<script type="text/javascript" src="'.WP_PLUGIN_URL.'/multi-column-tag-map/mctagmap.js?ver='.$mctagmapVersionNumber.'"></script>';
    echo "\n\n";
}


// overwrite single_tag_title()
add_filter('single_tag_title', 'mctagmap_single_tag_title', 1, 2);
function mctagmap_single_tag_title($prefix = '') {
    global $wp_query;
    if ( !is_tag() )
        return;

    $tag = $wp_query->get_queried_object();

    if ( ! $tag )
        return;

    $my_tag_name = str_replace('|', '', $tag->name);
    if ( !empty($my_tag_name) ) {
        if ( $display )
            echo $prefix . $my_tag_name;
        else
            return $my_tag_name;
    }
}

?>

1 Answer 1

2

i think you are getting downvotes b/c your question is crazy vague. if i understood from your themeshaper post then you are trying to group all your terms alphabetically like so:

A

  • anvil
  • apple
  • art

B

  • bacon
  • banana

D

  • darth vader
  • daytime

and so on. if this is the case, you can achieve it with get_terms() and a foreach loop

$terms = get_terms( 'category', array('orderby'=>'slug','order'=>'DSC'));

$xletter = NULL;

foreach($terms as $term){

   $letter = substr($term->slug, 0, 1); //gets first letter of slug

   if($letter != $xletter){  //if the letter has  changed then print it out
    echo '<h2>' .$letter . '</h2>';
}

$xletter = substr($term->slug, 0, 1); //gets first letter of slug

echo $term->name . '';  //echo the term name

}
2
  • Hi Helga, I was also playing around with this code. the get_terms() above only spits tag/category on the native post-type. Is there a way i can fetch tags to a certain post-type?
    – Pennf0lio
    Commented Jan 14, 2012 at 15:36
  • look at get_terms() codex.wordpress.org/Function_Reference/get_terms i am only getting categories, b/c that's all i had locally to test the rest on. get_terms will get whatever taxonomy you pass into it. replace category with your tax Commented Jan 14, 2012 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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