Php wordpress和ShowURL中所有内容的下拉列表
是否有插件或其他方式。。。 基本上,在wordpress的后端,我希望用户能够从下拉列表中选择一个文件或页面,然后它显示URL,以便他们可以复制和粘贴它,以便轻松链接 例如: 这样他们就可以复制并粘贴那个URL到他们的内容中了? 一直在寻找一段时间,以解决这个问题,但没有运气到目前为止 我也在使用高级自定义字段,如果可以的话 //==========================================================已解决====================================// 此代码获取选择字段acf-field-select\u内容的ID,并获取当前所选选项的值。然后它将该值放在文本字段acf-field-show_content_url中,但在ID前面我回显“SERVER_NAME”和“p=”,这是Wordpress的默认永久链接选项。。不幸的是,这种方式并没有直接链接到文件,而是链接到附件页,在这种情况下,附件页并不是什么大问题Php wordpress和ShowURL中所有内容的下拉列表,php,jquery,database,wordpress,plugins,Php,Jquery,Database,Wordpress,Plugins,是否有插件或其他方式。。。 基本上,在wordpress的后端,我希望用户能够从下拉列表中选择一个文件或页面,然后它显示URL,以便他们可以复制和粘贴它,以便轻松链接 例如: 这样他们就可以复制并粘贴那个URL到他们的内容中了? 一直在寻找一段时间,以解决这个问题,但没有运气到目前为止 我也在使用高级自定义字段,如果可以的话 //==========================================================已解决=======================
$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');
您完全可以将高级自定义字段用于类似的内容。您可以创建“文件上载”类型的字段。如果您也有“Repeater”的付费附加组件,那就太好了。中继器允许您拥有可重复的文件列表,而无需为每个上传的文件创建单独的字段 看看 但是,如果没有插件,您需要为每个上传的文件创建一个单独的字段,并执行如下操作:
<?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:
如果您有repeater字段附加模块
您将创建一个字段名为“Files”的repeater字段。在repeater字段中,您将为“文件标题”和“文件上载”创建一个字段
接下来,您将循环通过repeater字段来显示所有上载的文档
<!-- 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>
使用“文件名”=>“文件url”的关联数组。
创建下拉列表,显示使用for each循环的数组的所有“文件名”元素。
使用后端的全局$customFields(称为$custom)保存所选“文件名”的匹配“文件url”值。
使用$customFields在前端显示“文件url”vlue。
我有中继器字段!我该怎么做我已经更新了我的原始答案。如果你使用该代码,你需要确保你的转发器字段命名为“文件”、“文件上传”和“文件标题”才能正常工作。遗憾的是,在重读你的答案后,这不是我想要的!我期待这是只有在后端可见的“用户”。。这是为了快速参考,这样他们就可以选择文件或页面,并显示url,这样他们就可以复制并粘贴到内容字段中
<!-- 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>