我在WordPress中創建了一個自定義帖子類型。我想在短代碼中使用帖子ID,以便我可以將特定帖子拉出來輸出數據,這是使用高級自定義字段中的字段的圖庫。如何使用帖子ID作爲短代碼屬性?
這裏是我的功能至今:
function mbgc_gallery_shortcode($atts) {
// $post_id = $atts['id'];
extract(shortcode_atts(
array(
'id' => '',
), $atts)
);
$html_out = '123';
// $post = get_post($id);
// if ($post) :
if(have_rows('mbgc_gallery')):
$html_out .= '<div class="mbgc-gallery owl-carousel owl-theme">';
while(have_rows('mbgc_gallery')): the_row();
// vars
$image = get_sub_field('mbgc_image');
$caption = get_sub_field('mbgc_caption');
$title = get_sub_field('mbgc_title');
$sub_title = get_sub_field('mbgc_sub_title');
if ($image) :
$html_out .= '<div class="mbgc-gallery-item">';
if ($caption) :
$html_out .= '<div class="mbgc-slide-caption">';
$html_out .= '<h4>' . $caption . '</h4>';
$html_out .= '<div class="mbgc-slide-titles">';
$html_out .= '<h6>' . $title . '</h6>';
$html_out .= '<h6>' . $sub_title . '</h6>';
$html_out .= '</div>';
$html_out .= '</div>';
endif;
$html_out .= '<div class="mbgc-slide">';
$html_out .= '<img src="' . $image['url'] . '" alt="' . $image['alt'] . '" />';
$html_out .= '</div>';
$html_out .= '</div>';
endif;
endwhile;
$html_out .= '</div>';
endif;
// endif;
return $html_out;
}
add_shortcode('mbgc_gallery', 'mbgc_gallery_shortcode');
我希望簡碼可以像[mbgc_gallery ID = 「401」],作爲一個例子。最主要的是我不知道如何在函數中正確寫入以拉取ID。
你的功能看起來不錯(拉出ID),究竟是不是工作? –
前端沒有輸出內容。在一個隨機頁面上,我把我在原始文章中提到的那個短代碼添加進去,我只看到'123',但沒有別的。即使'var_dump'也不能爲我的變量返回任何東西。 –