English 中文(简体)
随机 ACF 彩色形象和使用形象链接
原标题:Display random ACF gallery image and use image custom link

We want to replace a slider with a clickable random image. So far I have half of this working using the below code that pulls one image from the ACF gallery and displays it.

我似乎无法找到的是,如何使用图像的习俗链条(如在把图像传入WP的媒体信条时所分配的),以便图像也能够被点击(在用户装上图像并分配所期望的习俗链接)。

Anyone have any idea how to get this to work?

Cheers!

<?php
$images = get_field( gallery ,   , false);
$size =  full ;
$rand = array_rand($images ?? [null]);


if( $images ): ?>
<?php echo wp_get_attachment_image( $images[$rand], $size, "", $attr ); ?>
<?php endif; ?>
问题回答

影像等附则有四个可读的文本领域:

  • alt-text
  • title
  • caption
  • description

或许不是一种良好做法,但为了记录,如果你利用其中一个领域作为你的图像习惯联系来源,如何迅速利用这些领域:

$images = get_field( gallery ,   , false);
$size =  full ;
$rand = array_rand($images ?? [null]);
$image_id = $images[$rand];

$image_title = get_the_title($image_id);
$image_alt = get_post_meta($image_id,  _wp_attachment_image_alt , TRUE);
$image_caption = wp_get_attachment_caption($image_id);
$image_description = get_post_field( post_content , $image_id);

您确切地澄清说,但我怀疑你实际上使用<代码>ACF的田地组,其图像连接习惯领域被分配到附件表格中。 然后,我猜测其类型。 如果是这样的话,你就能够简单地获得这个领域的价值:

$image_link = get_field( YOUR_CUSTOM_FIELD_NAME , $image_id);

接着,这只是使你的形象被点上新的圆顶:

if ($images) {
  $image_html = wp_get_attachment_image($image_id, $size);
  echo  <a href=" .$image_link. "> .$image_html. </a> ;
}

Solved! 对于那些想要解决问题的人来说,这里的守则是,我齐心协力,使阿农联的随机形象与习俗挂钩。 感谢@MindBorn,因为我大脑工作:

<?php
$random_images = get_field( random_image_row );
shuffle($random_images);
$random_img_url = $random_images[0][ banner ][ url ];
$random_image_link = $random_images[0][ image_link ];
?>

<a href="<?php echo $random_image_link; ?>" > <img src="<?php echo $random_img_url; ?>" /></a>




相关问题
Wrap stray text in <p> tags

Wordpress issue.. how do I wrap stray text in P tags? Example: Before- <div class = "content"> <img src = "hello.jpg"/> <h1>Introduction</h1> Hello! this is ...

Using jQuery Plugins with Wordpress

Having a bit of trouble using jQuery plugins (Superfish, jQuery UI, etc) using Wordpress. Everything works fine in my plain non-Wordpress site, but Wordpress seems to conflict with JQuery. There must ...

WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Why can t I properly style a blockquote in Wordpress?

On the design I just created for my website, I have a blockquote styled with two quote images using the span technique in css: blockquote { background-image: url(images/openquote.jpg); background-...

How does the WordPress <!--nextpage--> tag actually work?

What happens? I m guessing that somehow the post or page is parsed before displaying, and then just split into two methods? I can t seem to find any documentation on how the underlying <?php ...

Wordpress Plug-ins: How-to add custom URL Handles

I m trying to write a Wordpress Plug-in but can t seem to figure out how you would modify how a URL gets handled, so for example: any requests made for: <url>/?myplugin=<pageID> will ...

热门标签