按顺序在WordPress上显示自定义字段



我在这里遇到一种情况,我得到的数组自定义字段不按顺序排列。我可以知道如何按顺序排列所有自定义字段吗?

我用来获取自定义数组的函数是:

$title = get_post_meta($post_id, "cap-display_name", false);
foreach($title as $a){
    echo 'hello '.$a.'<br><br>';
}

但是,我得到的输出是:

hello this is first 
hello this is second 
hello this is third
hello this is six 
hello this is four 
hello this is five 
hello this is seven

假定的输出是:

hello this is first
hello this is second
hello this is third
hello this is four
hello this is five
hello this is six
hello this is seven

我可以知道如何获得上述输出吗?

print_r($title) 将得到这个:

数组 ( [0

] =>这是第一个 [1] =>这是第二个 [2] =>这是第三个 [3] =>这是六个 [4] =>这是四个 [5] =>这是五个 [6] =>这是七个)

我认为您需要为此运行自定义SQL。像这样:

   global $wpdb; // your DB object
   $sql = "SELECT m.meta_value FROM wp_postmeta m where m.meta_key = 'cap-display_name' and m.post_id = {$post_id} order by m.meta_id";
   $results = $wpdb->get_results( $sql );
   foreach( $results as $result )
   {
      echo 'hello '.$result->meta_value.'<br><br>';
   }

相关内容

  • 没有找到相关文章

最新更新