我建议为帖子与其“tracks”之间的关系创建一个新的自定义分类法,或者如果您愿意的话,可以创建一个快捷码,使用快捷码一次查询所有需要的曲目,而不是反复调用您的快捷码并对其进行排序。您可以在track中创建一个自定义字段,以便在查询中按该字段排序,从而可能是这样的(这假设您有一个名为“post_tracks“而且所有的帖子跟踪都被添加到该分类法的同一术语中,并且您有一个自定义字段来对跟踪进行排序,该字段名为”in_order“:
function get_tracks($atts, $content = null) {
extract(shortcode_atts(array(
"post_tracks" => \'\',
"tracks" => \'\',
), $atts));
//if post_tracks relation term was passed:
if ($atts[\'post_tracks\'] != \'\'){
$tracks = NEW WP_Query(array(\'post_type\' => \'track\', \'post_tracks\' => $atts[\'post_tracks\'], \'orderby\' => \'meta_value\', \'meta_key\' => \'in_order\' ));
while($tracks->have_posts()){
$tracks->the_post();
//do whatever you want with each track eg:
$out .= \'<div class="track">
<h3>\'.get_the_title($post->ID).\'<h3>
<div class="track_inner">
<div class="track_img">\'. get_the_post_thumbnail($post->ID, \'thumbnail\').\'</div>
<div class="track_content">\'.apply_filters(\'the_content\',get_the_content()).\'<div>
</div></div>\';
}
return $out;
}
//if its a single track you want:
$tracks = NEW WP_Query(array(\'post_type\' => \'track\',\'post__in\' => array($tracks) ));
while($tracks->have_posts()){
$tracks->the_post();
//do whatever you want with each track eg:
$out .= \'<div class="track">
<h3>\'.get_the_title($post->ID).\'<h3>
<div class="track_inner">
<div class="track_img">\'. get_the_post_thumbnail($post->ID, \'thumbnail\').\'</div>
<div class="track_content">\'.apply_filters(\'the_content\',get_the_content()).\'<div>
</div></div>\';
}
return $out;
}
add_shortcode(\'tracks\',\'get_tracks\');
有了它,您可以像这样简单地调用您的短代码:
[tracks post_tracks="relation_tern_name"]
或者一次只播放一首曲目:
[tracks tracks="track_id"]