WordPress - переписывание короткого кода - программирование
Подтвердить что ты не робот

WordPress - переписывание короткого кода

У меня есть тема, которая расширяет плагин Visual Composer с помощью слайдера на первой странице. Ползунок покажет пять отзывов от пяти разных клиентов. Я хочу добавить отображаемый образ каждого свидетельства в виде миниатюры в слайдере.

Здесь укороченный код из родительской темы:

function jo_customers_testimonials_slider( $atts ) {
    extract( shortcode_atts( array( 'limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000" ), $atts ) );
    $content = "";
    $loopArgs = array( "post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1 );

    $postsLoop = new WP_Query( $loopArgs );
    $content = "";

    $content .= '...';
    $content .= '...';
    $content .= '...';

    wp_reset_query();
    return $content;
}
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider' ); 

Мой файл functions.php:

function jo_customers_testimonials_slider_with_thumbnail( $atts ) {
    extract( shortcode_atts( array( 'limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000" ), $atts ) );
    $content = "";
    $loopArgs = array( "post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1 );

    $postsLoop = new WP_Query( $loopArgs );
    $content = "";

    $content .= '...';
    $content .= get_the_post_thumbnail( get_the_ID(), 'thumbnail' );
    $content .= '...';
    $content .= '...';

    wp_reset_query();
    return $content;
}
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail' );

В теории функция из моего файла functions.php должна перезаписывать короткий код из родительской темы. Но ничего не происходит, когда я использую этот код. Что я делаю неправильно?

Edit:
Пробовал этот код, но он все равно не сработает.

function wpa_add_child_shortcodes(){
remove_shortcode('jo_customers_testimonials_slider');
    add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail' );
}
add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' );

Также изменено
add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' ); to
add_action( 'init', 'wpa_add_child_shortcodes' );
, но никакой разницы в результатах.

Изменить 2 (с помощью решения):

Изменение add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' ); до add_action( 'wp_loaded', 'wpa_add_child_shortcodes' ); разрешило его.

4b9b3361

Ответ 1

вам нужно вызвать remove_shortcode(); как это:

remove_shortcode('jo_customers_testimonials_slider');' 

Прежде чем добавить новый шорткод с тем же именем, чтобы "перезаписать" его.

Вам также нужно будет вызвать его после запуска родительской темы, поэтому мы запускаем ловушку действия с именем wp_loaded.

function overwrite_shortcode() {

    function jo_customers_testimonials_slider_with_thumbnail($atts) {
        extract(shortcode_atts(array('limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000"), $atts));
        $content = "";
        $loopArgs = array("post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1);

        $postsLoop = new WP_Query($loopArgs);
        $content = "";

        $content .= '...';
        $content .= get_the_post_thumbnail(get_the_ID(), 'thumbnail');
        $content .= '...';
        $content .= '...';

        wp_reset_query();
        return $content;
    }

    remove_shortcode('jo_customers_testimonials_slider');
    add_shortcode('jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail');
}

add_action('wp_loaded', 'overwrite_shortcode');

Ответ 2

Вы должны написать этот код в своей дочерней теме functions.php

add_action( 'after_setup_theme', 'calling_child_theme_setup' );

function calling_child_theme_setup() {
   remove_shortcode( 'parent_shortcode_function' );
   add_shortcode( 'shortcode_name', 'child_shortcode_function' );
}

function child_shortcode_function( $atts) {
    $atts = shortcode_atts( array(
        'img'  => '',
        'cat'  => '',
        'capt' => '',
        'link' => ''
    ), $atts );

//YOUR OWN CODE HERE

    $imgSrc = wp_get_attachment_image_src( $atts['img'], 'delicious-gallery' );

    $imgFull = wp_get_attachment_image_src( $atts['img'], 'full' );



    $b = '<div class="screen-item" data-groups=\'["'.strtolower(str_replace(' ', '_', $atts["cat"])).'", "all"]\'>'.

        '<a href="'.$atts["link"].'" data-title="'.$atts["capt"].'" target="_blank"><img src="'.$imgSrc[0].'" alt="SCREEN" class="screenImg" /></a>'.

        '<span>'.$atts["capt"].'</span>'.

    '</div>';

//YOUR OWN CODE HERE

    return $b;
}