Solved

wordpress/php tag script help needed

Posted on 2010-11-11
10
281 Views
Last Modified: 2012-08-13
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
Comment
Question by:St_Aug_Beach_Bum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34120388
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
 

Author Comment

by:St_Aug_Beach_Bum
ID: 34121559
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
 

Author Comment

by:St_Aug_Beach_Bum
ID: 34121596
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34121679
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
 

Author Comment

by:St_Aug_Beach_Bum
ID: 34121996
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34122308
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
 

Author Comment

by:St_Aug_Beach_Bum
ID: 34122891
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
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34122943
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
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 34123332
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34123344
Thanks for the points -- glad to help, ~Ray
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question