我目前已将该问题提交到Wordpress部分,但没有成功。问题可以在这里找到。
我正在使用此插件将图像附加到自定义分类法。插件的函数返回附加术语的图像,如下所示:
function get_wp_term_image($term_id){
return get_option('_category_image'.$term_id);
}
编辑:get_wp_term_image($term_id)
返回图像的URL。
它工作正常,但我想调整返回图像的大小,因为我在显示所有具有相对拇指的分类法类别的页面中使用它。这意味着,如果您上传一个大图像,您将获得该图像,并且页面加载变得非常繁重。我试图在我的页面循环中添加此钩子但没有成功:
$term_id = $term->term_id;
if (function_exists('get_wp_term_image'))
{
$meta_image = get_wp_term_image($term_id);
//tried this
$term_image = wp_get_attachment_image_src($term_id, 'smalltax');
//and this
$image_id = attachment_url_to_postid($meta_image);
//no success
}
wp_get_attachment_image_src
钩子从任何id都考虑了附件,但是我错了。注意 我没有图像ID,但有image url
和term_id
attachment_url_to_postid
钩子获取$ image_id,然后通过其他一些调整来调整大小。您能提供解决方案吗?甚至帮助修改插件本身。谢谢!
你有没有尝试过?
$term_image = wp_get_attachment_image_src($meta_image, 'smalltax');
我假设$ meta_image是您要查找的图像的ID ...如果是这样,我的行将起作用。您正在将$ term_id传递给该函数,它是术语的ID,而不是图像的ID。
更新
由于您已指定$ meta_image为全尺寸图像的URL,因此为了获得所需的图像,请先获取其ID ...然后调用默认的WP函数以检索所需的图像尺寸。有点办法,但这似乎是您唯一的选择...因此,您将需要一个类似于以下的功能:
// Function to get an image's ID by it's full sized URL
function get_image_id_by_url( $image_url ) {
global $wpdb;
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid='%s';", $image_url ) );
return $attachment[0];
}
然后,您将在以下几行中使用一些东西:
$image_id = get_image_id_by_url( $image_url );
$$term_image = wp_get_attachment_image_src( $image_id, 'smalltax' );
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句