Ask your WordPress questions! Pay money and get answers fast! (more info)

Echo parent (ancestor maybe) category within tag cloud function. WordPress

  • SOLVED

First of all. Below is a lot of code, but everything is working. I only need to echo an ancestor category (or main parent, i'm not sure) Because at the moment, it echo's the subcategory, which i don't want.

I want to have a tag cloud based on a specific category.
There already exist such function online:

<?php
$custom_query = new WP_Query('posts_per_page=-1&category_name=<strong>CATEGORYNAME</strong>');
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$all_tags[] = $tag->term_id;
}
}
endwhile;
endif;

$tags_arr = array_unique($all_tags);
$tags_str = implode(",", $tags_arr);

$args = array(
'smallest' => 12,
'largest' => 12,
'unit' => 'px',
'number' => 0,
'format' => 'flat',
'separator' => ' &bull; ',
'include' => $tags_str
);
wp_tag_cloud($args);
?>


But i do not want it to be a static "categoryname". It should be dynamic, based on the category the user is in.

A category has this structure Fruit > Apples
And a post in Apples has a tag "red"
By default wordpress directs a tag "red" to domain.com/tag/red

However, i want it to direct it to domain.com/fruit/?tag=red
(one level higher than "apples")

Currently, below i already have the redirection set up and working.

BUT, it works as domain.com/fruit/apples/?tag=red


The answer i need is only for the correct parent or ancestor category to be echoed "fruit", instead of the sub category "apples"

I have this in my functions.php file:

add_action ('wp_print_scripts', 'FilterTagCloudByCat' ) ;
function FilterTagCloudByCat()
{
$skipTheseCats = array ( 'Uncatagorized', 'Some other bad catagory' ) ;

global $post ;
if ( function_exists ( 'get_the_catagory' ) )
{
$catsToOutput = get_the_catagory ( $post->ID ) ;
}
else
{
$terms = get_the_terms ( $post->ID, 'category' ) ;
$catsToOutput = array () ;
}
if ( $terms && ! is_wp_error ( $terms ) )
{
foreach ( $terms as $term )
{
$cat = $term->slug ;
if ( ! in_array ( $cat, $skipTheseCats ) )
{
// output to jQuery
array_push ( $catsToOutput, $cat ) ;
}
}
}

if ( count ( $catsToOutput ) > 0 )
{
print "<script>\n" ;
print "var currentPageCats = [] ; \n" ;
foreach ( $catsToOutput as $cat )
{
print "currentPageCats.push ( '$cat' ) ; \n" ;
}
print "</script>\n" ;

}
wp_enqueue_script ( 'tag-cloud-catagory', get_template_directory_uri() . '/js/tag-cloud-catagory.js', array ( 'jquery' ) ) ;
}


?>



Inside the tag-cloud-catagory.js file, i have this function:


function repaceTagWithCat ( cat )
{
jQuery('a[class*="tag-link"]').each ( function() {
var oldUrl = jQuery(this).attr ( 'href' ) ;
var posTag = oldUrl.lastIndexOf ( "/tag/" ) ;
var baseUrl = oldUrl.substr ( 0, posTag ) ;
var tag = oldUrl.substr ( posTag + 5 ) ;
tag = tag.substr ( 0, tag.length - 1 ) ;
<strong> var newUrl = baseUrl + "/" + cat + "/?tag=" + tag ; </strong>
jQuery(this).attr ( 'href', newUrl ) ;
}) ;
}




You can see at "<strong>var newUrl = baseUrl + "/" + cat + "/?tag=" + tag ;</strong> " is where it rewrites the url.
So i think the main issue is to just rewrite the "cat" call so that it echo's the ancestor and not the subcategory.


And then finally, in my template file, where i want the tags to appear, i have this code:


<?php
// we fetch all posts in our category
global $post ;
$skipTheseCats = array ( "uncategorized" ) ;
if ( function_exists ( 'get_the_category' ) )
{
$catObjs = get_the_category ( $post->ID ) ;
}
else
{
$catObjs = get_the_terms ( $post->ID, 'category' ) ;
}

$cats = array () ;
if ( ! is_wp_error ( $catObjs ) && count ( $catObjs ) > 0 )
{
foreach ( $catObjs as $catObj )
{
$cat = $catObj->slug ;
if ( ! in_array ( $cat, $skipTheseCats ) )
{
array_push ( $cats, $cat ) ;
}
}
}
// fudge: we only grab the first cat, as there is not enough time to redevelop system to handle multiple
$cat = $cats[0] ;
$custom_query = new WP_Query("posts_per_page=-1&category_name=$cat");
if ($custom_query->have_posts()) :
while ($custom_query->have_posts()) : $custom_query->the_post();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$all_tags[] = $tag->term_id;
}
}
endwhile;
endif;

$tags_arr = array_unique($all_tags);
$tags_str = implode(",", $tags_arr);

$args = array(
'smallest' => 12,
'largest' => 12,
'unit' => 'px',
'number' => 0,
'format' => 'flat',
'separator' => ' &bull; ',
'include' => $tags_str
);
wp_tag_cloud($args);
?>

Answers (2)

2013-03-07

Arnav Joy answers:

try this

$catid = get_query_var('cat');
$catidp = pa_category_top_parent_id ($catid);
$cat_link = get_category_link( $catidp );

if( !empty( $all_tags ) ){
echo '<h6>TAGS CATEGORIES</h6><br>';
$tags_arr = array_unique($all_tags);
$tags_str = implode(",", $tags_arr);

$catid = get_query_var('cat');
$catidp = pa_category_top_parent_id ($catid);
$cat_link = get_category_link( $catidp );
foreach($tags_arr as $tag) {
$term = get_term( $tag, 'post_tag' );
$link = $cat_link.'?tag='.$term->name;
$str .= '&nbsp;<a href="'.$link.'" class="tag-link-'.$term->term_id.'" style="font-size: 12px;" >'.$term->name.'</a> &bull;';
}
$str = substr($str,0,strlen($str)-6);
echo $str ;


define this at functions.php

function pa_category_top_parent_id ($catid) {
while ($catid) {
$cat = get_category($catid);
$catid = $cat->category_parent;
$catParent = $cat->cat_ID;
}
return $catParent;
}

2013-03-06

WisdmLabs answers:

Hi oomskaap,

Try adding following lines into your 'tag-cloud-catagory.js' file.

if (cat.indexOf('/') != -1) {

var newCat = cat.substr(0, cat.indexOf('/'));
cat = newCat;
}


So, your final code will look like

function repaceTagWithCat ( cat )

{

jQuery('a[class*="tag-link"]').each ( function() {

var oldUrl = jQuery(this).attr ( 'href' ) ;

var posTag = oldUrl.lastIndexOf ( "/tag/" ) ;

var baseUrl = oldUrl.substr ( 0, posTag ) ;

var tag = oldUrl.substr ( posTag + 5 ) ;

tag = tag.substr ( 0, tag.length - 1 ) ;

<strong>if (cat.indexOf('/') != -1) {

var newCat = cat.substr(0, cat.indexOf('/'));
cat = newCat;
} </strong>

var newUrl = baseUrl + "/" + cat + "/?tag=" + tag ;

jQuery(this).attr ( 'href', newUrl ) ;

}) ;

}


Suppose, your 'cat' variable has value 'fruit/apples', it will replace it to 'fruit'


oomskaap comments:

The above code does not work.
If anyone can rewrite the 'cat' variable, I can test it.
Thanks.