简体   繁体   English

获取数组中返回的最后一个URL

[英]Get last URL returned in Array

I am using a form field to let a user edit a post and upload a profile image. 我正在使用一个表单字段来让用户编辑帖子并上传个人资料图像。

Once they upload the profile image a URL is passed to a hidden custom text field called 'logo_header'. 他们上传个人资料图片后,会将URL传递到名为“ logo_header”的隐藏自定义文本字段。 I read this URL in the PHP template and use it to display the image as follows. 我在PHP模板中读取了此URL,并使用它如下显示图像。

<?php $logo = get_post_meta($post->ID, 'logo_header', true); ?>

<div class="profileLogo"><img src="<?php echo $logo; ?>" /></div>

My Problem is when a user decides to update their photo (basically change the photo) the new URL is just added to the end of the current one, I guess this creates an array. 我的问题是,当用户决定更新其照片(基本上是更改照片)时,新的URL仅添加到当前URL的末尾,我想这会创建一个数组。 so it would be like 所以就像

http://example.com/myprofile.jpg , http://example.com/myprofile2.jpg http://example.com/myprofile.jpghttp://example.com/myprofile2.jpg

Obviously the image src cannot use an array, it needs 1 URL. 显然,图片src无法使用数组,它需要1个URL。

I use gravity forms upload file button to edit the post and upload the image. 我使用重力形式上传文件按钮来编辑帖子并上传图片。 There is no option to delete the current image that already exists (Thats another problem :( ) so the user is only given an upload button. 没有选项可以删除已经存在的当前图像(这是另一个问题:(),因此仅向用户提供一个上传按钮。

My question is 我的问题是

How can I always choose the last URL added to the 'logo_header' field? 如何始终选择添加到“ logo_header”字段中的最后一个URL?

Try $logo = array_pop(get_post_meta($post->ID, 'logo_header', false)); 试试$logo = array_pop(get_post_meta($post->ID, 'logo_header', false));

With the false parameter, you'll get an array of all the meta fields. 使用false参数,您将获得所有元字段的数组。 We can only assume that they'll be inserted and returned in the correct order, so array_pop() will pull the last one in the list. 我们只能假设它们将以正确的顺序插入并返回,因此array_pop()将拉出列表中的最后一个。

Edit - for cases where you don't always have multiple logos, you should use this: 编辑 -对于并非总是有多个徽标的情况,应使用以下命令:

$logo = get_post_meta($post->ID, 'logo_header', false);
$logo = is_array($logo) ? array_pop($logo) : $logo;
$logo = get_post_meta($post->ID, 'logo_header', true);

$logoImgSrc = is_array($logo) ? end($logo) : $logo;

<div class="profileLogo">
    <img src="<?php echo $logoImgSrc ; ?>" />
</div>

I think you shouldn't be using a hidden text field, rather a hidden input field, when the user selects the new image (the files change() function, in jQuery) set the hidden field value to the new url, replacing the old, then post the hidden field name. 我认为您不应该使用隐藏的文本字段,而应该使用隐藏的输入字段,当用户选择新图像(jQuery中的files change()函数)时,请将隐藏字段的值设置为新的url,替换旧的,然后发布隐藏的字段名称。

Edit to possibly help: 编辑可能有帮助:

$("#browse_box").change(function() {
    $("#hidden_field").val($(this).val());
});

Typing from memory, but that would be the basic idea. 从内存中键入,但这将是基本思想。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM