如何将帖子ID用作短代码属性?

时间:2017-04-24 15:29:51

标签: php wordpress function shortcode

我在WordPress中创建了一个自定义帖子类型。我想在短代码中使用帖子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 =&#34; 401&#34;],作为一个例子。最重要的是我不知道如何在函数中写出它以获取ID。

1 个答案:

答案 0 :(得分:2)

您需要将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');

使用如下的短代码:[mbgc_gallery id =&#34; 123&#34;]。

我个人不喜欢使用extract() - 这会将数组转换为单个变量,根据数组元素键命名,并且可以使调试变得困难。相反,只需按照以下方式访问您的ID:$attributes['id'],如上所示。

祝你好运!