我需要知道是否有可能将get_the_tags排列成数组吗?

问题描述 投票:0回答:2

我需要知道如何将get_the_tags()数组化吗?

我想要这样

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

我想将此代码与“ replace the_content”一起使用,像这样

<?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');
?>

找到内容标签并替换为链接

wordpress wordpress-plugin
2个回答
1
投票
$posttags = get_the_tags();
$my_array = array();
if ($posttags) {
  foreach($posttags as $tag) {
    $my_array[] = $tag->name ; 
  }

..如果您的最终目标是像上面写的那样输出它,则:

echo implode(',', $my_array);

..并按问题的类型,我不确定是否一二..您可能是ID的意思,所以:

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

顺便说一句-快速浏览codex会向您显示...


0
投票

您应该可以执行以下操作:

codex

注意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]."'"; }); 仅从PHP 5.3起可用

如果您只想要特定帖子的标签,则应该可以使用anonymous functions执行相同的操作:

get_the_tags()

从更新后的问题来看,您不需要上面的任何代码,为了使每个标签周围都有单引号,您需要做的唯一一件事情是:

$tags = get_the_tags();
array_walk($tags, function(&$item, $key) { $item = "'".$item->name."'"; });
© www.soinside.com 2019 - 2024. All rights reserved.