在wordpress短代码中为自定义元字段值的短代码中允许多个值

时间:2016-03-28 11:24:42

标签: wordpress shortcode custom-fields

我有一个短代码,如果iI将“rank”的单个值放在shortcode中,它可以正常工作。

[coaches_list category =“dummy”number =“3”rank =“2”]

但我想在“rank”中传递多个值为[coaches_list category =“dummy”number =“3”rank =“2”,“6”]。

'rank'是与post关联的数字类型自定义字段。

我在过去2天在互联网上搜索了很多但没有找到结果。请让我知道我在哪里有错误。

以下是我所提供的短代码:

$args = array(
        'number' => '-1',
        'orderby' => 'id',
        'order' => 'desc',
        'category' => '',
        'meta_key'    => '', 
        'rank'  => '', 

    ), $atts )
);

 global $post;


  $html = "";
  $my_query = new WP_Query( array('post_type' => 'post',
       'posts_per_page' => $number, 'orderby' => $orderby, 'order' => $order, 'category' =>$category, 'meta_key' => 'rank', 'meta_value' =>  $rank ));

  if( $my_query->have_posts() ) : while( $my_query->have_posts() ) : $my_query->the_post();

1 个答案:

答案 0 :(得分:0)

我去[coaches_list category="dummy" number="3" rank="2,6"]

然后你可以创建一个数组$ranks = explode(',',$rank);

function my_shortcode($atts){
  extract(shortcode_atts( $args = array( 
    'number' => '-1', 
    'orderby' => 'id', 
    'order' => 'desc', 
    'category' => '', 
    'meta_key' => '', 
    'rank' => '', 
  ),$atts)); 

  $ranks = explode(',',$rank); 
  $html = '<ul>';

  $my_posts = get_posts(array(
    'post_type' => 'post',
    'posts_per_page' => $number, 
    'orderby' => $orderby, 
    'order' => $order, 
    'category' =>$category, 
    'meta_query' => array(
        'key'=>'rank',
        'value'=>$ranks,
        'compare'=>'IN'
    )
  ));

  foreach($my_posts as $rankpost){
    $html .= '<li>'.$rankpost->ID.': '.get_the_title($rankpost->ID).' - Rank: '.get_post_meta($rankpost->ID,'rank',true).'</li>';
  }
  $html .= '</ul>';
  return $html;
}

或许你可以创建一个查询,给出所有帖子,其中rank = 2 AND / OR rank = 6。