Показывать содержимое страницы WordPress с помощью шорткода и показывать встроенный шорткод - PullRequest
0 голосов
/ 13 мая 2019

Я использую пользовательский тип сообщения для отображения баннеров на моем сайте.Контент может быть текстом, изображениями, а также шорткодами (например, шорткодом кнопки).

Если я отображаю контент с помощью своего шорткода, все выглядит хорошо.Кроме шорткодов внутри самого баннера.

Есть ли способ отрисовки этого шорткода?

Вот мой shordcode:

// [banner id="" class=""]
function shortcode_banner( $atts, $content ){
    extract(shortcode_atts(array(
        'id'    => '',
        'class' => '',
    ), $atts));

    $banner_id  = $id;
    $content    = get_post_field('post_content', $banner_id);

        return '<div class="'.$class.'">'.wpautop($content).'</div>';

}
add_shortcode( 'banner', 'shortcode_banner' );

1 Ответ

1 голос
/ 13 мая 2019

Попробуйте следующий код:

// [banner id="" class=""]
function shortcode_banner( $atts, $content ) {
    extract(shortcode_atts( array(
        'id'    => '',
        'class' => '',
    ), $atts ) );

    $banner_id = $id;
    $content   = get_post_field( 'post_content', $banner_id );

    return '<div class="'.$class.'">'.wpautop( do_shortcode( $content ) ).'</div>';
}

или если вы ожидаете иметь рекурсивные шорткоды:

function recursively_do_shortcode( $content ) {
    $content2 = $content;
    do{
       $content = $content2;
       $content2 = do_shortcode( $content );
    } while( $content2 !== $content ); // presumably you can test if shortcodes exist in content as well
    return $content2;
}

// [banner id="" class=""]
function shortcode_banner( $atts, $content ){
    extract( shortcode_atts( array(
        'id'    => '',
        'class' => '',
    ), $atts ) );

    $banner_id = $id;
    $content   = get_post_field( 'post_content', $banner_id );

    return '<div class="'.$class.'">'.wpautop( recursively_do_shortcode( $content ) ).'</div>';
}

Ссылка: do_shortcode

...