0

我在修改我安装的插件 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" />

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

更新

好的,使用下面的代码,图像 URL 被获取,但它为所有 10 个热门帖子获取相同的图像 URL。

                    // 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>";
                }
4

1 回答 1

0

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

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

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

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

然后我会$thumb .=用你的功能 ACF 代码替换每个实例,只是为了测试,如下所示:

$thumb .= "<img src=\"" . $my_image . "\" alt=\"" . $my_title . "\" class=\"postImg\" />";
于 2013-11-13T09:34:10.810 回答