使用高级自定义字段的Wordpress自定义字段

时间:2013-11-13 08:26:50

标签: php wordpress

我在修改我安装的插件WordPress热门帖子时遇到了一些问题。

它可以选择从自定义字段中获取缩略图,我将其输入为“image_facebook”。但是缩略图没有显示出来。

在检查代码时,我发现img src有post id而不是返回图像URL。

"<img src="5438" width="135" height="135" alt="Alt text" border="0" />"

我已将问题缩小到我已安装http://wordpress.org/plugins/advanced-custom-fields/的另一个插件,当它处于活动状态时,我必须使用“the_field()”来获取自定义字段内容而不是常规WordPress“get_post_meta”它是记录在此http://www.advancedcustomfields.com/resources/functions/the_field/

我需要编辑WordPress热门帖子文件中的以下代码才能使用the_field()函数。 WordPress-popular-posts.php中的代码说: -

                    // POST THUMBNAIL
                if ($instance['thumbnail']['active'] && $this->thumb) {

                    $tbWidth = $instance['thumbnail']['width'];
                    $tbHeight = $instance['thumbnail']['height'];

                    $thumb = "<a href=\"". $permalink ."\" title=\"{$title}\" target=\"".$this->user_ops['tools']['link']['target']."\">";

                    if ( $this->user_ops['tools']['thumbnail']['source'] == "custom_field" ) { // get image from custom field

                        $path = get_post_meta($p->id, $this->user_ops['tools']['thumbnail']['field'], true);

                        if ( $path != "" ) {

                            if ( $this->user_ops['tools']['thumbnail']['resize'] ) {

                                $thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );

                            } else {
                                $thumb .= "<img src=\"{$path}\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" alt=\"{$title}\" border=\"0\" />";
                            }

                        } else {
                            $thumb .= "<img src=\"". $this->default_thumbnail ."\" alt=\"{$title}\" border=\"0\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" />";
                        }

                    } else { // get image from post / Featured Image
                        $thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );
                    }

                    //$thumb .= "</a>";
                }

在我的主题文件中,我可以通过以下代码检索图像URL: -

<img src="<?php echo get_field('image_facebook'); ?>" alt="<?php the_title(); ?>" class="postImg" />

请帮我把这个功能放在上面的插件代码中。

更新

好的,使用下面的代码,获取图片网址,但是它为所有10个热门帖子提取相同的图片网址。

                    // POST THUMBNAIL
                if ($instance['thumbnail']['active'] && $this->thumb) {
                    $my_image = get_field('image_facebook');
                    $my_title = get_the_title();
                    $tbWidth = $instance['thumbnail']['width'];
                    $tbHeight = $instance['thumbnail']['height'];

                    $thumb = "<a href=\"". $permalink ."\" title=\"{$title}\" target=\"".$this->user_ops['tools']['link']['target']."\">";

                    if ( $this->user_ops['tools']['thumbnail']['source'] == "custom_field" ) { // get image from custom field

                        $path = get_post_meta($p->id, $this->user_ops['tools']['thumbnail']['field'], true);

                        if ( $path != "" ) {

                            if ( $this->user_ops['tools']['thumbnail']['resize'] ) {

                                $thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );

                            } else {
                                //$thumb .= "<img src=\"{$path}\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" alt=\"{$title}\" border=\"0\" />";
                                $thumb .= "<img src=\"" . $my_image . "\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" alt=\"" . $my_title . "\" border=\"0\" />";
                                }

                        } else {
                            $thumb .= "<img src=\"". $this->default_thumbnail ."\" alt=\"{$title}\" border=\"0\" width=\"{$tbWidth}\" height=\"{$tbHeight}\" />";
                        }

                    } else { // get image from post / Featured Image
                        $thumb .= $this->get_img( $p->id, array($tbWidth, $tbHeight), $this->user_ops['tools']['thumbnail']['source'] );
                    }

                    //$thumb .= "</a>";
                }

1 个答案:

答案 0 :(得分:0)

<img src="5438" width="135" height="135" alt="Alt text" border="0" />

如果这是您唯一的问题,您可以修改ACF图像字段返回的值。现在它可能设置为图像ID。请尝试将其设置为图片网址:see here

如果没有帮助,我会尝试这个。请记住,我不明白您的插件如何与ACF交互。首先,我会设置你的变量:

$my_image = get_field('image_facebook');
$my_title = get_the_title();

然后我会用你正在运行的ACF代码替换$thumb .=的每个实例,只是为了测试,就像这样:

$thumb .= "<img src=\"" . $my_image . "\" alt=\"" . $my_title . "\" class=\"postImg\" />";
相关问题