![](/img/trans.png)
[英]Making a Slideshow in Wordpress using the Advanced Custom fields plugin and the repeater field
[英]Wordpress Custom Field using Advanced Custom Fields
我在修改我安裝的WordPress熱門帖子插件時遇到了一些麻煩。
它具有從自定義字段中獲取縮略圖的選項,我已將其輸入為“ image_facebook”。 但是沒有顯示縮略圖。
在檢查代碼時,我發現img src具有發布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>";
}
<img src="5438" width="135" height="135" alt="Alt text" border="0" />
如果這是您唯一的問題,則可以修改ACF圖像字段返回的值。 現在,它可能已設置為“圖像ID”。 嘗試將其設置為圖片網址:
以防萬一,我可以嘗試一下。 請記住,我不了解您的插件如何與ACF交互。 首先,我將設置您的變量:
$my_image = get_field('image_facebook');
$my_title = get_the_title();
然后,我將使用正常運行的ACF代碼替換$thumb .=
每個實例,以進行測試,如下所示:
$thumb .= "<img src=\"" . $my_image . "\" alt=\"" . $my_title . "\" class=\"postImg\" />";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.