user3006974
user3006974

Reputation: 1

I need to know if it's possible how to get_the_tags to array?

I need to know if it's possible how to get_the_tags() to array?

i want to like this

$myarray  = array('one', 'two', 'three', 'four', 'five', 'six');

i want use this code with "replace the_content" like this

<?php
function replace_content($content){
  foreach(get_the_tags() as $tag) {
    $out .= $tag->name .',';
    $csv_tags .= '"<a href="/' . $tag->slug . '">' . $tag->name . '</a>"';
  }
  $find  = array($out);
  $replace = array($csv_tags);
  $content = str_replace($find, $replace, $content);
  return $content;
}
add_filter('the_content', 'replace_content');
?>

find tag in content and replace with link

Upvotes: 0

Views: 748

Answers (2)

Cyclonecode
Cyclonecode

Reputation: 30071

You should be able to do something like this:

global $wpdb;
// get all term names in an indexed array
$array = $wpdb->get_results("SELECT name FROM wp_terms", ARRAY_N);
// walk over the array, use a anonymous function as callback
array_walk($array, function(&$item, $key) { $item = "'".$item[0]."'"; });

Notice that anonymous functions are only available since PHP 5.3

You should be able to do the same thing using get_the_tags() if you only want tags for a specific post:

$tags = get_the_tags();
array_walk($tags, function(&$item, $key) { $item = "'".$item->name."'"; });

Judging from your updated question you don't need any of the above code, the only thing you need to to in order to get single quotes around each tag is:

foreach(get_the_tags() as $tag) {
  $out .= $tag->name . ',';
  $csv_tags .= '"<a href="/' . $tag->slug . '">' . "'".$tag->name."'" . '</a>"';
} 

Upvotes: 0

Obmerk Kronen
Obmerk Kronen

Reputation: 15959

$posttags = get_the_tags();
$my_array = array();
if ($posttags) {
  foreach($posttags as $tag) {
    $my_array[] = $tag->name ; 
  }

.. and if your final goal is to output it like you wrote above then :

echo implode(',', $my_array);

.. and by the type of question , I was not sure if by one,two.. you might be meaning ID , so :

$posttags = get_the_tags();
$my_array = array();
if ($posttags) {
  foreach($posttags as $tag) {
    $my_array[] = $tag->term_id ; 
  }

BTW - a quick look at the codex would have shown you that ...

Upvotes: 1

Related Questions