php - 如何使用 functions.php 制作 Wordpress 过滤器/操作
问题描述
我在 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;
这可以做到吗?
解决方案
它很简单,你只需修改你的代码来应用一些像这样的过滤器
$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;
}
推荐阅读
- npm - 试图设置反应但 npm start 命令给我 ENOENT 错误
- java - Java——在两个正则表达式之间获取所有字符串的最佳方式?
- c - 在C中逐字读取文本文件
- python - 正则表达式在python中获取正确的值
- regex - Terraform 在不应该的时候解释当地人的表达
- ruby-on-rails - 如何在rails应用程序中将表格中字符串的长度设置为一定长度?
- sql - 在所有组中加入空表
- java - 当我尝试在 java 中读取控制台输出错误时,没有得到结果
- excel - Worksheet_Change 功能似乎不起作用
- javascript - 使用三元运算符的“else”中的多种效果