【问题标题】:Wordpress Custom Field using Advanced Custom Fields使用高级自定义字段的 Wordpress 自定义字段
【发布时间】:2013-11-25 18:16:04
【问题描述】:

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

【问题讨论】:

  • get_field() 必须位于循环内,因为它默认为“当前”帖子。你必须弄清楚你的插件是如何遍历你的 10 个帖子的,并确保每个帖子都调用一次 get_field

标签: php wordpress


【解决方案1】:

&lt;img src="5438" width="135" height="135" alt="Alt text" border="0" /&gt;

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

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

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

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

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

【讨论】:

  • 我可以确认它设置为显示图像 url,因为我可以通过 " alt=" php the_title(); ?>" class="postImg" />
  • 好的。您的插件在四个地方生成$thumb,具体取决于if 条件。您需要测试哪个生成“5438”并使用get_field() 修改那个。
  • 好的,它在某种程度上起作用了。我已经用新代码更新了这个问题。它为所有帖子显示相同的缩略图。
猜你喜欢
  • 2017-08-28
  • 2012-08-10
  • 1970-01-01
  • 2016-06-25
  • 2015-01-21
  • 2017-05-01
  • 2018-06-07
  • 2018-09-02
  • 1970-01-01
相关资源
最近更新 更多