Как использовать параметр шоркода как переменную?

Добрый вечер!

Подскажите пожалуйста, можно ли использовать параметр шорткода как переменную и создать условие?

У меня есть шорткод:
add_shortcode( 'cases-list', 'case_post_listing_parameters_shortcode' );
function case_post_listing_parameters_shortcode( $atts ) {
    ob_start();
    $args = shortcode_atts( array (
        'type' => 'cases',
        'order' => 'date',
        'orderby' => 'title',
        'posts' => -1,
        'category' => '',
        'post_status' => 'publish',
        'public'   => true,
        'template' => ''
    ), $atts );
    $options = array(
        'post_type' => $args['type'],
        'order' => $args['order'],
        'orderby' => $args['orderby'],
        'posts_per_page' => $args['posts'],
        'category_name' => $args['category'],
        'post__not_in' => array( get_the_ID() ),
        'post_status' => 'publish',
        'template' => $options['template'];
    );

    $query = new WP_Query( $options );
    if ( $query->have_posts() ) { ?>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <?php
                    get_template_part( 'template-parts/content/content-single-case', get_post_format() );
                 ?>
            <?php endwhile;
            wp_reset_postdata(); ?>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}


В нем есть параметр "template", можно ли задать шаблон вывода записи используя условие с параметром template?

Например:

$query = new WP_Query( $options );
    $template = $atts['template'];
    if ( $query->have_posts() ) { ?>
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                <?php
                	if ( $template == "simmple" ) {
                    	get_template_part( 'template-parts/content/content-single-case-simple', get_post_format() );
                    } else {
                   		get_template_part( 'template-parts/content/content-single-case', get_post_format() );
                    }
                 ?>
            <?php endwhile;
            wp_reset_postdata(); ?>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
  • Вопрос задан
  • 36 просмотров
Пригласить эксперта
Ответы на вопрос 1
kumaxim
@kumaxim
Web-программист
У тебя в коде ошибка. Ты в массив $options в ключ template записываешь его же!

$options = array(
        'template' => $options['template'];
 );


Я так понимаю, что ты хотел бы туда писать $args['template'] вот так:
$options = array(
        'template' => $args['template'];
 );


Я прав?
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы