Wordpress Meta Box 插件,从自定义 post 类型检索图像 url
Wordpress Meta Box plugin, retrieving image url from custom post type
使用 Wordpress Meta Box 插件有点麻烦,特别是从添加到自定义 post 类型的图像中检索图像 url。
我正在自定义插件中创建元框,如下所示:
add_filter( 'rwmb_meta_boxes', 'xxx_meta_register_meta_boxes' );
function xxx_meta_register_meta_boxes( $meta_boxes )
{
$prefix = 'xxx_meta_';
$meta_boxes[] = array(
'title' => esc_html__( 'Retailer Information', '' ),
'id' => 'advanced',
'post_types' => array( 'xxx_retailers' ),
'autosave' => true,
'fields' => array(
// PLUPLOAD IMAGE UPLOAD (WP 3.3+)
array(
'name' => esc_html__( 'Retailer Logo', '' ),
'id' => "{$prefix}plupload",
'type' => 'plupload_image',
'max_file_uploads' => 1,
),
// URL
array(
'name' => esc_html__( 'Link', '' ),
'id' => "{$prefix}url",
'desc' => esc_html__( 'Clicking the retailer logo will take the user to this URL', '' ),
'type' => 'url',
'std' => 'xxx',
),
)
);
return $meta_boxes;
}
目前一切顺利,这些框与自定义 post 类型相关 'xxx_retailers'。
检索此数据时出现问题。我想在小部件中显示我的零售商。我已经切碎并更改了我以前使用过的另一段代码,但它没有返回图像 URL,只是返回 ID。不幸的是,我知道的不够 php 无法弄清楚原因。
// Create Retailers Widget
// Create the widget
class Retailers_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// base ID of the widget
'retailers_widget',
// name of the widget
__('XXX Retailers List', '' ),
// widget options
array (
'description' => __( 'Shows a list of retailer logos', '' )
)
);
}
function widget( $args, $instance ) {
// kick things off
extract( $args );
echo $before_widget;
echo $before_title . 'Retailers' . $after_title;
// Pull through Retailers
$xxxretailers = get_posts(array(
'post_type' => 'xxx_retailers',
'orderby' => 'title',
'order' => 'asc',
));
// Display for each Retailer
foreach ($xxxretailers as $xxxretailer) {
$custom = get_post_custom($xxxretailer->ID);
$meta_ret_img = $custom["xxx_meta_plupload"][0];
$meta_ret_url = $custom["xxx_meta_url"][0];
// Display Retailers
echo "<li><a href='{$meta_ret_url}'><img src='{$meta_ret_img}' /></a></li>";
}
}
};
// Register widget
function register_retailers_widget() {
register_widget( 'Retailers_Widget' );
}
add_action( 'widgets_init', 'register_retailers_widget' );
URL正确通过,所以我知道这是线路问题
$meta_ret_img = $custom["xxx_meta_plupload"][0];
但我不知道如何从我认为存储为数组的数据中获取图像 URL。有什么想法吗?
编辑:
我应该提到,在单个 post 中,我可以通过以下方式获得单个图像:
$images = rwmb_meta( 'xxx_meta_plupload', 'size=medium' );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo "<img src='{$image['url']}' />";
}
}
但我想显示来自我的所有零售商 post 类型的图像,以创建徽标列表。
替换此语句:
$meta_ret_img = $custom["xxx_meta_plupload"][0];
有了这个:
$meta_ret_img_array = wp_get_attachment_image_src($custom["xxx_meta_plupload"][0]);
$meta_ret_img = $meta_ret_img_array[0];
另外请从代码中的 src
和 href
属性中删除所有这些花括号。
如果您对图像的任何特定尺寸感兴趣,请参阅 wp_get_attachment_image_src()
函数的官方文档 here。
例如对于 medium 大小的图像,您可以将其写为:
wp_get_attachment_image_src($custom["xxx_meta_plupload"][0], 'medium');
使用 Wordpress Meta Box 插件有点麻烦,特别是从添加到自定义 post 类型的图像中检索图像 url。
我正在自定义插件中创建元框,如下所示:
add_filter( 'rwmb_meta_boxes', 'xxx_meta_register_meta_boxes' );
function xxx_meta_register_meta_boxes( $meta_boxes )
{
$prefix = 'xxx_meta_';
$meta_boxes[] = array(
'title' => esc_html__( 'Retailer Information', '' ),
'id' => 'advanced',
'post_types' => array( 'xxx_retailers' ),
'autosave' => true,
'fields' => array(
// PLUPLOAD IMAGE UPLOAD (WP 3.3+)
array(
'name' => esc_html__( 'Retailer Logo', '' ),
'id' => "{$prefix}plupload",
'type' => 'plupload_image',
'max_file_uploads' => 1,
),
// URL
array(
'name' => esc_html__( 'Link', '' ),
'id' => "{$prefix}url",
'desc' => esc_html__( 'Clicking the retailer logo will take the user to this URL', '' ),
'type' => 'url',
'std' => 'xxx',
),
)
);
return $meta_boxes;
}
目前一切顺利,这些框与自定义 post 类型相关 'xxx_retailers'。
检索此数据时出现问题。我想在小部件中显示我的零售商。我已经切碎并更改了我以前使用过的另一段代码,但它没有返回图像 URL,只是返回 ID。不幸的是,我知道的不够 php 无法弄清楚原因。
// Create Retailers Widget
// Create the widget
class Retailers_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// base ID of the widget
'retailers_widget',
// name of the widget
__('XXX Retailers List', '' ),
// widget options
array (
'description' => __( 'Shows a list of retailer logos', '' )
)
);
}
function widget( $args, $instance ) {
// kick things off
extract( $args );
echo $before_widget;
echo $before_title . 'Retailers' . $after_title;
// Pull through Retailers
$xxxretailers = get_posts(array(
'post_type' => 'xxx_retailers',
'orderby' => 'title',
'order' => 'asc',
));
// Display for each Retailer
foreach ($xxxretailers as $xxxretailer) {
$custom = get_post_custom($xxxretailer->ID);
$meta_ret_img = $custom["xxx_meta_plupload"][0];
$meta_ret_url = $custom["xxx_meta_url"][0];
// Display Retailers
echo "<li><a href='{$meta_ret_url}'><img src='{$meta_ret_img}' /></a></li>";
}
}
};
// Register widget
function register_retailers_widget() {
register_widget( 'Retailers_Widget' );
}
add_action( 'widgets_init', 'register_retailers_widget' );
URL正确通过,所以我知道这是线路问题
$meta_ret_img = $custom["xxx_meta_plupload"][0];
但我不知道如何从我认为存储为数组的数据中获取图像 URL。有什么想法吗?
编辑:
我应该提到,在单个 post 中,我可以通过以下方式获得单个图像:
$images = rwmb_meta( 'xxx_meta_plupload', 'size=medium' );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo "<img src='{$image['url']}' />";
}
}
但我想显示来自我的所有零售商 post 类型的图像,以创建徽标列表。
替换此语句:
$meta_ret_img = $custom["xxx_meta_plupload"][0];
有了这个:
$meta_ret_img_array = wp_get_attachment_image_src($custom["xxx_meta_plupload"][0]);
$meta_ret_img = $meta_ret_img_array[0];
另外请从代码中的 src
和 href
属性中删除所有这些花括号。
如果您对图像的任何特定尺寸感兴趣,请参阅 wp_get_attachment_image_src()
函数的官方文档 here。
例如对于 medium 大小的图像,您可以将其写为:
wp_get_attachment_image_src($custom["xxx_meta_plupload"][0], 'medium');