有办法改变/改变核心has_post_thumbnail函数吗?
在代码查找post图像附件之前,我需要先检查服务器上带有图像的文件夹。
我看到了很多过滤器来改变图片html,但是它们要么影响单个post页面,要么影响循环缩略图,还有woocommerce的其他几个地方,加上管理表,等等。
我需要将服务器目录检查放在它命中_post_thumbnail()或get_image之前,所以看起来has_post_thumbnail是第一次检查,除非我错了。
请帮忙,建议能做些什么。
谢谢
编辑:尝试将if语句放入post-thumbnail-template.php has_post_thumbnail函数中,但没有工作。在哪里可以尽早检查文件夹中的图像,并且可能不会编辑WordPress核心文件?
编辑:我认为它有点工作。问题是,在我的例子中,我需要将目录中的图像名与另一个post meta,woocommerce产品相匹配。
我首先检查自定义文件夹中的图像,然后查看post附件,这里是我到目前为止是如何完成产品模板文件的工作的:
global $post;
$picname = get_post_meta( get_the_ID(), '_box_upc', true );
$file = '/var/www/html/images/products/' . $picname . '.jpg';
if (file_exists($file)) {
$filesrc = home_url( '/' ).'images/products/' . $picname . '.jpg';
//show the image
$result = '';
}else{
//return post attachment if exists code
}使用@krzysiek-dróżdż代码,这是我所拥有的,但似乎无法使用全局的ż来获取其他post元数据以匹配文件名,也不能使用post标题给服务器带来错误:
function my_override_has_post_thumbnail( $result, $object_id, $meta_key, $single ) {
global $post;
// Get post other meta, neither one works so I uncommeneted
//$picname = get_post_meta( $post->get_id(), '_box_upc', true );
//$picname = get_post_meta( get_the_ID(), '_box_upc', true );
if ( '_thumbnail_id' === $meta_key ) {
// perform your checks and return some ID if thumbnail exists
// Get image from custom folder
$filelocation = '/var/www/html/images/products/' . $picname . '.jpg';
if (file_exists($filelocation)) {
$filesrc = home_url( '/' ).'images/products/' . $picname . '.jpg';
//show the image
$result = '';
}
}
return $result;
}
add_filter( 'get_post_metadata', 'my_override_has_post_thumbnail', 10, 4 );发布于 2018-04-15 06:19:52
是的,有一种方法.让我们看一看has_post_thumbnail函数本身:
function has_post_thumbnail( $post = null ) {
return (bool) get_post_thumbnail_id( $post );
}正如您所看到的,它真正做的就是获取post缩略图ID并检查它是否存在。但这里没有过滤器。让我们走得更深:
function get_post_thumbnail_id( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return '';
}
return get_post_meta( $post->ID, '_thumbnail_id', true );
}仍然没有过滤器,但仍有希望:
function get_post_meta( $post_id, $key = '', $single = false ) {
return get_metadata('post', $post_id, $key, $single);
}最后在get_metadata..。
function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
if ( ! $meta_type || ! is_numeric( $object_id ) ) {
return false;
}
$object_id = absint( $object_id );
if ( ! $object_id ) {
return false;
}
/**
* Filters whether to retrieve metadata of a specific type.
*
* The dynamic portion of the hook, `$meta_type`, refers to the meta
* object type (comment, post, or user). Returning a non-null value
* will effectively short-circuit the function.
*
* @since 3.1.0
*
* @param null|array|string $value The value get_metadata() should return - a single metadata value,
* or an array of values.
* @param int $object_id Object ID.
* @param string $meta_key Meta key.
* @param bool $single Whether to return only the first value of the specified $meta_key.
*/
$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
if ( null !== $check ) {
if ( $single && is_array( $check ) )
return $check[0];
else
return $check;
}
...看起来我们可以使用get_post_metadata钩子来覆盖has_post_thumbnail结果。唯一需要记住的是,它也会改变get_post_thumbnail_id的行为.
像这样的事情应该能起作用:
function my_override_has_post_thumbnail( $result, $object_id, $meta_key, $single ) {
if ( '_thumbnail_id' === $meta_key ) {
// perform your checks and return some ID if thumbnail exists
}
return $result;
}
add_filter( 'get_post_metadata', 'my_override_has_post_thumbnail', 10, 4 );发布于 2018-04-15 07:07:27
@Krzysiek Dróżdż的答案对你所问的问题是非常正确的,但你应该问问自己,你是否在问正确的问题.
Wordpress假设一定数量的图像相关信息被正确地存储在DB中,以便它的API能够正确地处理它们。数据库中没有的图像是外部的,API只是不适用于它们。现在您已经确定了一个给您带来问题的API,但是您的代码变得越复杂,或者站点上使用的第三方代码越多,您就会遇到更多不适合您的图像的API。
因此,正确的答案可能是首先避免这个问题,并适当地将图像导入wordpress。
https://wordpress.stackexchange.com/questions/300807
复制相似问题