【发布时间】:2016-08-28 21:22:12
【问题描述】:
对 Wordpress Meta Box 插件有点麻烦,特别是从添加到自定义帖子类型的图像中检索图像 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;
}
到目前为止一切顺利,这些框与自定义帖子类型“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' );
网址正确通过,所以我知道这是线路的问题
$meta_ret_img = $custom["xxx_meta_plupload"][0];
但我不知道如何从我认为存储为数组的数据中获取图像 URL。有什么想法吗?
编辑:
我应该提到,在一篇文章中我可以得到一张图片:
$images = rwmb_meta( 'xxx_meta_plupload', 'size=medium' );
if ( !empty( $images ) ) {
foreach ( $images as $image ) {
echo "<img src='{$image['url']}' />";
}
}
但我想显示来自我所有零售商帖子类型的图片,以创建徽标列表。
【问题讨论】:
-
你能在这里添加
var_dump($custom);的输出吗 -
当然,结果如下:array(6) { ["_edit_lock"]=> array(1) { [0]=> string(12) "1472408521:1" } ["_edit_last "]=> array(1) { [0]=> string(1) "1" } ["_revision-control"]=> array(1) { [0]=> string(25) "a:1: {i:0;s:8:"defaults";}" } ["_yoast_wpseo_content_score"]=> array(1) { [0]=> string(2) "30" } ["xxx_meta_url"]=> array( 1) { [0]=> string(163) "[Redacted]" } ["xxx_meta_plupload"]=> array(1) { [0]=> string(4) "1862" } }
-
另外我想知道你为什么在
img标签的src属性中使用花括号,还请添加以下语句的输出echo $custom["xxx_meta_url"][0]; -
大括号,除了它们出现在我第一次遵循的代码示例中之外,没有什么特别的原因。正如我所说,我不太了解 php,所以这是从教程中拼凑而成的。 $custom["xxx_meta_url"] 输出在我上面的第一个代码示例中的 url 元框中输入的 url。这就像输入一样出来。
-
不,不幸的是它是一样的。 href url 正在通过,但 img src 的 url 没有。我只是得到图像 ID,这是一个 4 位数字。不是代码不行,是我想不通得到我想要的,是图片url,不是id。
标签: php wordpress meta-boxes