Php 如何使用中的post ID作为快捷码属性?
我在WordPress中创建了一个自定义帖子类型。我想在短代码中使用post ID,这样我就可以提取特定的post来输出数据,这是一个使用高级自定义字段字段的库 到目前为止,我的功能如下:Php 如何使用中的post ID作为快捷码属性?,php,wordpress,function,shortcode,Php,Wordpress,Function,Shortcode,我在WordPress中创建了一个自定义帖子类型。我想在短代码中使用post ID,这样我就可以提取特定的post来输出数据,这是一个使用高级自定义字段字段的库 到目前为止,我的功能如下: function mbgc_gallery_shortcode( $atts ) { // $post_id = $atts['id']; extract( shortcode_atts( array( '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\u gallery\u短码($atts){
//$post_id=$atts['id'];
提取(短码)(
排列(
“id'=>”,
),$atts)
);
$html_out='123';
//$post=获取邮政($id);
//如果(员额):
如果(有行(“mbgc画廊”):
$html_out.='';
while(have_rows('mbgc_gallery')):the_row();
//瓦尔斯
$image=get_sub_字段('mbgc_image');
$caption=get_sub_字段('mbgc_caption');
$title=get_sub_字段('mbgc_title');
$sub_title=get_sub_字段('mbgc_sub_title');
如果($image):
$html_out.='';
如果($标题):
$html_out.='';
$html_out.=''.$caption';
$html_out.='';
$html_out.=''.$title';
$html_out.=''.$sub_title'.';
$html_out.='';
$html_out.='';
endif;
$html_out.='';
$html_out.='';
$html_out.='';
$html_out.='';
endif;
结束时;
$html_out.='';
endif;
//endif;
返回$html_out;
}
添加_短码('mbgc_gallery','mbgc_gallery_短码');
举个例子,我希望短代码看起来像[mbgc_gallery id=“401”]。主要的问题是,我不知道如何准确地将其写入函数以提取ID。您需要将ID从快捷码属性传递到ACF
have_rows()
函数,以便检索正确的数据
<?php
function mbgc_gallery_shortcode( $atts ) {
$attributes = shortcode_atts(
array(
'id' => null
), $atts );
// $attributes['id'] is now the passed-in ID
$html_out = '';
if( have_rows('mbgc_gallery', $attributes['id']) ):
$html_out .= '<div class="mbgc-gallery owl-carousel owl-theme">';
while( have_rows('mbgc_gallery', $attributes['id']) ): 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;
return $html_out;
}
add_shortcode('mbgc_gallery', 'mbgc_gallery_shortcode');
您的函数似乎很好(用于提取ID),到底是什么不起作用?前端没有任何输出。在一个随机页面上,我把我在原始帖子中提到的那个短代码放进去,我只看到了123
,其他什么都看不到。即使是var\u dump
也不能为我的变量返回任何内容。