在 wordpress 简码中允许自定义元字段值在简码中有多个值
Allow multiple value in shortcode for custom meta field value in wordpress shortcode
我有一个简码,如果我将 "rank" 的单个值放在简码中,它就可以正常工作。
[coaches_list 类别="dummy" number="3" rank="2"]
但我想在 "rank" 中传递多个值作为 [coaches_list category="dummy" number="3" rank="2", "6".
'rank' 是与 post 关联的数字类型自定义字段。
过去两天我在互联网上搜索了很多,但没有找到任何结果。请让我知道我哪里出错了。
这是我构建的一段简码:
$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();
我会选择 [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;
}
或者您可以创建一个查询,给出所有 post with rank=2 AND/OR rank=6.
我有一个简码,如果我将 "rank" 的单个值放在简码中,它就可以正常工作。
[coaches_list 类别="dummy" number="3" rank="2"]
但我想在 "rank" 中传递多个值作为 [coaches_list category="dummy" number="3" rank="2", "6".
'rank' 是与 post 关联的数字类型自定义字段。
过去两天我在互联网上搜索了很多,但没有找到任何结果。请让我知道我哪里出错了。
这是我构建的一段简码:
$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();
我会选择 [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;
}
或者您可以创建一个查询,给出所有 post with rank=2 AND/OR rank=6.