有插件或其他方式吗...基本上在wordpress的后端,我希望用户能够从下拉列表中选择一个文件或页面,然后它显示URL,以便他们可以复制和粘贴它以便于链接?
例:
they select a file
document.pdf
and in a box below it displays the URL
/wp-content/uploads/2013/10/document.pdf
那么他们可以将该 URL 复制并粘贴到他们的内容中吗?一直在寻找一段时间来解决这个问题,但到目前为止还没有运气!
我也在使用高级自定义字段,如果这有帮助?
//========================================================================================//此代码获取选择字段 (#acf-field-select_content) 的 ID 并获取当前所选选项的值。然后,它将值放在文本字段(#acf-field-show_content_url)中,但在ID前面,我回显了"SERVER_NAME"和"?p=",这是Wordpress的默认永久链接选项。可悲的是,这种方式不会直接链接到文件,而是链接到附件页面,在这种情况下这不是什么大问题
$serverName = $_SERVER['SERVER_NAME'];
?>
<script>
jQuery(document).ready(function () {
jQuery("#acf-field-select_content").change(function() {
var str = "http://<?php echo $serverName; ?>/?p=";
jQuery("option:selected", this).each(function() {
str += jQuery(this).val();
});
jQuery("#acf-field-show_content_url").val(str);
})
.trigger("change");
});
</script>
<?php }
add_filter('admin_head', 'add_admin_code');
- 使用"文件名"=>"文件 url"的关联数组。
- 创建下拉列表,显示每个循环使用的数组的所有"文件名"元素。
- 使用全局$customFields保存所选"文件名"的匹配"文件 url"值(在后端称为 $custom。
- 使用 $customFields 在前端显示"文件 URL"vlue。
您绝对可以使用高级自定义字段来执行此类操作。您可以创建"文件上传"类型的字段。如果您也有"中继器"的付费附加组件,那就太好了。转发器将允许您拥有可重复的文件列表,而无需为每个上传的文件创建单独的字段。
看看吧!ACF 中继器附加组件 [链接]。
但是,如果没有插件 - 您需要为每个上传的文件创建一个单独的字段并执行以下操作:
<?php
// Get file url and title
$uploaded_file_1 = get_field('uploaded_file_1');
$uploaded_file_1_url = wp_get_attachment_url( $uploaded_file_1 );
$uploaded_file_1_title = get_the_title( $uploaded_file_1 );
?>
<!-- create dropdown with list of file names and urls -->
<select id="files">
<option value="">
Select a file
</option>
<option value="<?php echo $uploaded_file_1_url; ?>">
<?php echo $uploaded_file_1_title; ?>
</option>
</select>
<input type="text" id="file-info">
<script>
// bind change event to dropdown, when you change the dropdown
// get the value of the selected option and put it in the input field
document.getElementById('files').onchange = function(){
document.getElementById('file-info').value = document.getElementById('files').value;
}
</script>
JSFIDDLE: http://jsfiddle.net/N7ujB/
如果您有中继器字段附加组件
您将创建一个中继器字段,其字段名称类似于"文件"。在转发器字段中,您将为"文件标题"和"文件上传"创建一个字段。
接下来,您将遍历转发器字段以显示所有上传的文档。
<!-- create dropdown with list of file names and urls -->
<select id="files">
<option value="">
Select a file
</option>
<?php
if ( get_field('files') ) {
// loop through the uploaded files
while(has_sub_field('files')) {
// set variables for file title/uri
$file = get_sub_field('file_upload');
$file_uri = wp_get_attachment_url( $file );
$file_title = ( get_sub_field('file_title') ) ? get_sub_field('file_title') : get_the_title( $file );
// generate code for each option, including the file uri and title
echo '<option value="' . $file_uri . '">' . $file_title . '</option>';
}
}
?>
</select>