Как сделать Wordpress фильтр / действие с помощью functions.php - PullRequest
0 голосов
/ 21 февраля 2019

Я довольно бесполезен при создании фильтров и действий в Wordpress functions.php.

Вот мой запрос ..

Я хочу изменить 2 строки кода, не меняя основной темыфайлы.

Это код -

if ( ! function_exists( 'sf_portfolio_thumbnail' ) ) {
    function sf_portfolio_thumbnail( $display_type = "gallery", $multi_size = "", $multi_size_ratio = "1/1", $columns = "2", $hover_show_excerpt = "no", $excerpt_length = 20, $gutters = "yes", $fullwidth = "no" ) {`

        global $post, $sf_options;

        $portfolio_thumb = $thumb_image_id = $thumb_image = $thumb_gallery = $video = $item_class = $link_config = $port_hover_style = $port_hover_text_style = '';
        $thumb_width     = 400;
        $thumb_height    = 300;
        $video_height    = 300;

        if ( $columns == "1" ) {
            $thumb_width  = 1200;
            $thumb_height = 900;
            $video_height = 900;
        } else if ( $columns == "2" ) {
            $thumb_width  = 800;
            $thumb_height = 600;
            $video_height = 600;
        } else if ( $columns == "3" || $columns == "4" ) {
            if ( $fullwidth == "yes" ) {
                $thumb_width  = 500;
                $thumb_height = 375;
                $video_height = 375;
            } else {
                $thumb_width  = 400;
                $thumb_height = 300;
                $video_height = 300;
            }
        }

Это строка, которую я хочу изменить.

        } else if ( $columns == "2" ) {
            **$thumb_width  = 1280;
            $thumb_height = 1024;**
            $video_height = 600;

Можно ли это сделать?

Ответы [ 2 ]

0 голосов
/ 21 февраля 2019

Когда вы пишете функцию в своей дочерней теме, которую хотите переопределить в родительской теме, вы просто даете ей то же имя, что и в родительской теме:

function sf_portfolio_thumbnail($display_type = "gallery", $multi_size = "", $multi_size_ratio = "1/1", $columns = "2", $hover_show_excerpt = "no", $excerpt_length = 20, $gutters = "yes", $fullwidth = "no") {

    global $post, $sf_options;

    $portfolio_thumb = $thumb_image_id = $thumb_image = $thumb_gallery = $video = $item_class = $link_config = $port_hover_style = $port_hover_text_style = '';
    $thumb_width = 400;
    $thumb_height = 300;
    $video_height = 300;

    if ($columns == "1") {
        $thumb_width = 1200;
        $thumb_height = 900;
        $video_height = 900;
    } else if ($columns == "2") {
        $thumb_width = 800;
        $thumb_height = 600;
        $video_height = 600;
    } else if ($columns == "3" || $columns == "4") {
        if ($fullwidth == "yes") {
            $thumb_width = 500;
            $thumb_height = 375;
            $video_height = 375;
        } else {
            $thumb_width = 400;
            $thumb_height = 300;
            $video_height = 300;
        }
    } else if ($columns == "2") {
        $thumb_width = 1280;
        $thumb_height = 1024;
        $video_height = 600;
    }
}

WordPress сначала запустит функцию в дочерней теме , а когда она перейдет к родительской теме, она проверит, существует ли она, и, поскольку она существует, не запустит ее.

Счастливое кодирование !!

0 голосов
/ 21 февраля 2019

Это просто, вы просто измените свой код, чтобы применить какой-то фильтр, подобный этому

$thumb_width    = apply_filter( 'thumb_width', 800 ); // here 'thumb_width' is the filter name, which can be hooked on. and 800 is the default value.
$thumb_height   = apply_filter( 'thumb_height', 600 );
$video_height   = apply_filter( 'video_height', 600 );

Теперь вы можете использовать фильтры в functions.php для изменения значений.

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

add_filter('thumb_width', 'my_custom_thumb_width', 10, 1); //'10' is the priority of the filter if hooked multiple time. lower numbered one will be executed first. '1' is the number of argument passed.
function my_custom_thumb_width($thumb_width){
    $thumb_width = 1280; //modify the value as your requirement
    return $thumb_width;
}

add_filter('thumb_height', 'my_custom_thumb_height', 10, 1);
function my_custom_thumb_width($thumb_width){
    $thumb_height = '1024'; //modify the value as your requirement
    return $thumb_height;
}

add_filter('video_height', 'my_custom_video_height', 10, 1);
function my_custom_thumb_width($thumb_width){
    $video_height = '600'; //modify the value as your requirement
    return $video_height;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...