如何使用functions.php制作Wordpress过滤器/操作

时间:2019-02-21 10:57:33

标签: php wordpress function

我对在Wordpress functions.php中创建过滤器和操作毫无用处。

这是我的要求。

我希望更改两行代码,而不更改核心主题文件。

这是代码-

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 :(得分:0)

非常简单,您只需修改代码即可应用此类过滤器

$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;
}

答案 1 :(得分:0)

当您要在子主题中编写要覆盖父主题中的功能的函数时,只需为其命名与父主题中的功能相同:

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首先将在child theme中运行该功能,当涉及到父主题中的功能时,它将检查它是否已经存在,并且因为存在,因此将不会运行它。 / p>

快乐编码!