你有两个主要问题
您正在构建的字符串是正确的,但它在循环的每次迭代中都会被覆盖,最后只保存最后一篇文章中的信息。您需要在循环外定义变量,然后在整个循环中将信息连接到该变量,以便显示所有帖子中的所有postdata
您返回的信息错误。您将在循环的第一次迭代之后返回post id。您应该在循环外返回包含帖子信息的字符串
我认为你的代码应该是这样的:
add_shortcode(\'sedan-tabs\',\'build_sedan_tabs\');
function build_sedan_tabs(){
$querySedans = \'\';
$args1 = array(
\'post_type\' => \'inventory\',
\'meta_key\' => \'vehicle-type\',
\'meta_value\' => \'Sedan\',
\'posts_per_page\' => \'4\'
);
$query1 = new WP_Query($args1);
while ( $query1->have_posts() ) {
$query1->the_post();
$currentPostID = get_the_id();
$vehicleImg = get_post_meta($currentPostID,\'vehicle-images\', true);
$querySedans .= \'<div class="col-sm-3"><div class="display-car"><img src="\' . $vehicleImg . \'"/>\' .
\'<h3>\' . get_post_meta($currentPostID,\'vehicle-year\', true) . \' \' .
get_post_meta($currentPostID,\'vehicle-make\', true) . \' \' .
get_post_meta($currentPostID,\'vehicle-model\', true) .\'</h3><p>\' .
get_post_meta($currentPostID,\'vehicle-mileage\', true) . \' \' .
get_post_meta($currentPostID,\'vehicle-transmission\', true) . \'<br /><strong>\' .
get_post_meta($currentPostID,\'vehicle-retail-price\', true) . \'</strong></p></div></div><a href="\' . get_permalink($currentPostID, false) . \'"></a>\';
}
return $querySedans;
}