programing

WordPress - 쇼트 코드 덮어쓰기

golfzon 2023. 3. 20. 23:46
반응형

WordPress - 쇼트 코드 덮어쓰기

첫 페이지에 슬라이더를 사용하여 Visual Composer 플러그인을 확장하는 테마가 있습니다.슬라이더에는 5개의 다른 고객으로부터 5개의 추천이 표시됩니다.각 추천의 특집 이미지를 슬라이더에 썸네일로 추가하고 싶습니다.

다음은 부모 테마의 줄임말 코드입니다.

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' ); 

내 기능.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' );

이론상으로는 내 기능에서 나오는 기능이지php 파일은 부모 테마에서 쇼트 코드를 덮어씁니다.하지만 이 코드를 사용해도 아무 일도 일어나지 않는 것 같습니다.내가 뭘 잘못하고 있지?

편집:
이 코드를 사용했지만 여전히 작동하지 않습니다.

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' );로.
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' );해결했습니다.

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');

Child Theme의 함수에 이 코드를 입력해야 합니다.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;
}

언급URL : https://stackoverflow.com/questions/21265624/wordpress-overwriting-a-shortcode

반응형