Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

wordpress/php tag script help needed

I need a php script that will grab the tags on a post, array them, and make a tag cloud by taking the first tag entered and surrounding it with an h1 tag, take the next 2 tags and surround them with h2 tags, then take the remaining and surround them in h3 tags. Then display them in random order, seperated by ", "

It should just be the tags for the post, not the whole system or catagory. And the first (h1)tag should be the first tag (keyword) entered into the tag section on the edit page page, going down in order. I've noticed that wp usually lists them in alphabetical order.

It will be placed in my 'sidebar.php', not a widget.

Is that something someone can put together for me?

Thanks very much,  Chris
0
St_Aug_Beach_Bum
Asked:
St_Aug_Beach_Bum
  • 5
  • 5
1 Solution
 
Ray PaseurCommented:
Hey, Beach Bum.  I got a "neglected question" alert on this one.

I've written some code to do this (years ago) that was driven off the most popular search terms.  We kept a data base table with popular search terms.  By adding the letter 'S' to the end of the term (essentially making all of them plurals) and storing the metaphone() string as the search key, we had pretty good consolidation of results.  So we got "bulletin" and "bulletins" matched up.  We kept a count of popular terms and a DATETIME so that we could age the popularity.  Christmas will be coming soon, and Easter will arrive in the spring.  The aging let us keep the most popular terms current.  It was fun to write and see it in action, but in the end, we dropped the cloud from the site in favor of a more prosaic "popular search" list.

It was not a WP feature, but I can tell you the central parts - we kept the count so we could choose the popularity and use that to determine which words got the H1, H2, etc.  And we used a random order for our selection.  A few lines of PHP and some CSS rounded it out into a nice little sidebar box.  Wish I still had the code around, but maybe the narrative is enough to stimulate your thinking.

Hopefully some of these search results will be useful...
http://lmgtfy.com?q=Wordpress+tag+cloud 

Best regards, ~Ray
0
 
St_Aug_Beach_BumAuthor Commented:
Hi Ray,

Good to see you again :)

Well, I know you are good with arrays, so maybe you can help me work this out - cause my knowledge of manipulating arrays is close to null...

I found I can get the information I need by doing this:

<? $postid = $post->ID;  
 print_r (get_the_tags($postid));
?>

That give me this:

Array ( [552] => stdClass Object ( [term_id] => 552 [name] => Life [slug] => Life [term_group] => 0 [term_taxonomy_id] => 555 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 1 [object_id] => 1931 ) [551] => stdClass Object ( [term_id] => 551 [name] => sally [slug] => sally [term_group] => 0 [term_taxonomy_id] => 554 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 1 [object_id] => 1931 ) [550] => stdClass Object ( [term_id] => 550 [name] => John [slug] => John [term_group] => 0 [term_taxonomy_id] => 553 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 1 [object_id] => 1931 ) [554] => stdClass Object ( [term_id] => 554 [name] => food [slug] => food [term_group] => 0 [term_taxonomy_id] => 557 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 1 [object_id] => 1931 ) [553] => stdClass Object ( [term_id] => 553 [name] => Heath [slug] => Heath [term_group] => 0 [term_taxonomy_id] => 556 [taxonomy] => post_tag [description] => [parent] => 0 [count] => 1 [object_id] => 1931 ) )
so all the information I need is there, Just not sure how to change it into what I need.

I'll need to get the id number (552), the name (Death), and slug (death) for each section. (example of first section.

Then put them in order of the number, descending, with the first loop being tagged with the H1 tag, the second with the H2 tag, the the remaining in h3 tags (the volume of remaining tags will vary).

So in the end, I have:

<h1 class="mytags"><a href="http://www.sure-start.com/tag/John/" rel="tag">John</a></h1><br />
<h2 class="mytags"><a href="http://www.sure-start.com/tag/sally/" rel="tag">sally</a></h2><br />
<h3 class="mytags"><a href="http://www.sure-start.com/tag/Life/" rel="tag">Life</a></h3><br />
<h3 class="mytags"><a href="http://www.sure-start.com/tag/Heath/" rel="tag">Heath</a></h3><br />
<h3 class="mytags"><a href="http://www.sure-start.com/tag/food/" rel="tag">food</a></h3><br />

There is addtional info in that array that I don't need.

Is it possible to put that together here, or too much for this forum?

Thanks again very much,  Chris
0
 
St_Aug_Beach_BumAuthor Commented:
The 'slug' being the part included in the url, the 'name' being the anchor text.  And I made an 'opps' in the first example, ie " id number (552), the name (Death), and slug (death) for each section", as I changed that to 'life' in the rest of the thread so it didn't look so ominous!!
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Ray PaseurCommented:
Regarding this...

<? $postid = $post->ID;  
 print_r (get_the_tags($postid));
?>

Do me a favor, please.  Put this into the code right before the print_r()

echo "<pre>";

Then change the print_r() to var_dump() and run it again.  Post that output in the code snippet.  I think we have everything we need to make it work and that will make it easy for me to read.  I can't test the code, but I can try to give you something worth testing!

Cheers, ~Ray
0
 
St_Aug_Beach_BumAuthor Commented:
ah, ok, here that is..
array(5) {
  [552]=>
  object(stdClass)#170 (10) {
    ["term_id"]=>
    string(3) "552"
    ["name"]=>
    string(5) "life"
    ["slug"]=>
    string(5) "life"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "555"
    ["taxonomy"]=>
    string(8) "post_tag"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "1"
    ["object_id"]=>
    string(4) "1931"
  }
  [551]=>
  object(stdClass)#169 (10) {
    ["term_id"]=>
    string(3) "551"
    ["name"]=>
    string(6) "Sally"
    ["slug"]=>
    string(6) "sally"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "554"
    ["taxonomy"]=>
    string(8) "post_tag"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "1"
    ["object_id"]=>
    string(4) "1931"
  }
  [550]=>
  object(stdClass)#167 (10) {
    ["term_id"]=>
    string(3) "550"
    ["name"]=>
    string(4) "John"
    ["slug"]=>
    string(4) "john"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "553"
    ["taxonomy"]=>
    string(8) "post_tag"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "1"
    ["object_id"]=>
    string(4) "1931"
  }
  [554]=>
  object(stdClass)#166 (10) {
    ["term_id"]=>
    string(3) "554"
    ["name"]=>
    string(13) "food"
    ["slug"]=>
    string(13) "food"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "557"
    ["taxonomy"]=>
    string(8) "post_tag"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "1"
    ["object_id"]=>
    string(4) "1931"
  }
  [553]=>
  object(stdClass)#165 (10) {
    ["term_id"]=>
    string(3) "553"
    ["name"]=>
    string(7) "Life"
    ["slug"]=>
    string(7) "life"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(3) "556"
    ["taxonomy"]=>
    string(8) "post_tag"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    string(1) "0"
    ["count"]=>
    string(1) "1"
    ["object_id"]=>
    string(4) "1931"
  }
}

Open in new window

0
 
Ray PaseurCommented:
Thanks.  See if this tests out OK.  I named all the vars starting with "$my_" because I was not sure of the namespace requirements.  Feel free to change that if it causes any variable collisions.
// GRAB THE ARRAY OF OBJECTS
$my_arr = get_the_tags($postid);

// PROCESS INTO AN ARRAY OF SORTABLE LINKS
$my_new = array();
foreach ($my_arr as $my_obj)
{
    $my_term = (string)$my_obj->term_id;
    $my_name = (string)$my_obj->name;
    $my_slug = (string)$my_obj->slug;
    $my_link
    = '<h??? class="mytags">'
    . '<a href="http://www.sure-start.com/tag/'
    . $my_slug
    . '/" rel="tag">'
    . $my_name
    . '</a></h1><br />'
    ;
    $my_akey = 'X' . str_pad($my_term, 8, '0', STR_PAD_LEFT);
    $my_new[$my_akey] = $my_link;
}
// SORT THE ARRAY BY KEY DESCENDING
krsort($my_new);

// SLICE OFF THE <h1> AND <h2> ELEMENTS
$my_tags = NULL;
$my_tags .= str_replace('???', '1', array_shift($my_new)) . PHP_EOL;
$my_tags .= str_replace('???', '2', array_shift($my_new)) . PHP_EOL;
$my_tags .= str_replace('???', '2', array_shift($my_new)) . PHP_EOL;

// ROUND UP THE REST OF THE <h3> ELEMENTS
foreach ($my_new as $my_link)
{
    $my_tags .= str_replace('???', '3', $my_link) . PHP_EOL;
}

Open in new window

0
 
St_Aug_Beach_BumAuthor Commented:
First run didn't display anything, so I chaged this:

$my_tags .= str_replace('???', '3', $my_link) . PHP_EOL;

to

echo $my_tags .= str_replace('???', '3', $my_link) . PHP_EOL;

correct?

The ending h tag will have to change with each one also, do I just use the ??? in the . '</a></h1><br />' line?

And it seems to be looping through twice. In my testing, I have five tags. It gives me the first four, then the first four again and the fifth at the end... does that make sense?

0
 
Ray PaseurCommented:
See if this covers the issues.  We just echo $my_tags once, after the string is fully constructed.

Good catch on the ??? for the closing tag - I missed that the first time through!
// GRAB THE ARRAY OF OBJECTS
$my_arr = get_the_tags($postid);

// PROCESS INTO AN ARRAY OF SORTABLE LINKS
$my_new = array();
foreach ($my_arr as $my_obj)
{
    $my_term = (string)$my_obj->term_id;
    $my_name = (string)$my_obj->name;
    $my_slug = (string)$my_obj->slug;
    $my_link
    = '<h??? class="mytags">'
    . '<a href="http://www.sure-start.com/tag/'
    . $my_slug
    . '/" rel="tag">'
    . $my_name
    . '</a></h???><br />'
    ;
    $my_akey = 'X' . str_pad($my_term, 8, '0', STR_PAD_LEFT);
    $my_new[$my_akey] = $my_link;
}
// SORT THE ARRAY BY KEY DESCENDING
krsort($my_new);

// SLICE OFF THE <h1> AND <h2> ELEMENTS
$my_tags = NULL;
$my_tags .= str_replace('???', '1', array_shift($my_new)) . PHP_EOL;
$my_tags .= str_replace('???', '2', array_shift($my_new)) . PHP_EOL;
$my_tags .= str_replace('???', '2', array_shift($my_new)) . PHP_EOL;

// ROUND UP THE REST OF THE <h3> ELEMENTS
foreach ($my_new as $my_link)
{
    $my_tags .= str_replace('???', '3', $my_link) . PHP_EOL;
}

// DISPLAY THE TAGS
echo $my_tags;

Open in new window

0
 
St_Aug_Beach_BumAuthor Commented:
Wow, just excellent!  

Thank you for all your help with this and my other question. You go above a way beyond the call of duty! Wish I had more than 500 to give you for this one!
0
 
Ray PaseurCommented:
Thanks for the points -- glad to help, ~Ray
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now