Solved

wordpress/php tag script help needed

Posted on 2010-11-11
10
267 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
  • 5
  • 5
10 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points -- glad to help, ~Ray
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video teaches users how to migrate an existing Wordpress website to a new domain.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now