【问题标题】:A drop down list of all content in wordpress and show urlwordpress 中所有内容的下拉列表并显示 url
【发布时间】:2013-10-21 14:45:26
【问题描述】:

是否有插件或其他方式... 基本上在 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 复制并粘贴到他们的内容中吗? 一直在寻找解决这个问题的方法,但目前还没有运气!

如果这有帮助,我也在使用高级自定义字段?

//===================================== 已解决========= ============================// 此代码获取选择字段的 ID (#acf-field-select_content) 并获取当前选择的选项的值。然后它将值放在文本字段(#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');

【问题讨论】:

    标签: php jquery database wordpress plugins


    【解决方案1】:
    1. 使用 'file name'=>'file url' 的关联数组。
    2. 使用每个循环创建显示数组的所有“文件名”元素的下拉列表。
    3. 使用全局 $customFields 保存所选“文件名”的匹配“文件 url”值(在后端称为 $custom。
    4. 使用 $customFields 在前端显示“文件 url”vlue。

    【讨论】:

      【解决方案2】:

      您绝对可以将高级自定义字段用于此类事情。您可以使用“文件上传”类型创建一个字段。如果您也有“Repeater”的付费插件,那就太好了。转发器将允许您拥有可重复的文件列表,而无需为每个上传的文件创建单独的字段。

      看看吧! ACF Repeater Add-on [link]

      但是,如果没有插件 - 您需要为每个上传的文件创建一个单独的字段并执行以下操作:

      <?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>
      

      【讨论】:

      • 我确实有中继器字段!我怎样才能做到这一点? :)
      • 我已经更新了我原来的答案。如果您使用该代码,您需要确保将转发器字段命名为“文件”、“文件上传”和“文件标题”才能正常工作。
      • 遗憾的是,在重新阅读您的答案后,它不是我想要的!我希望这仅在“用户”的后端可见..这是为了快速参考,因此他们可以选择文件或页面并显示 url,以便他们可以将其复制并粘贴到内容字段中跨度>
      猜你喜欢
      • 2021-09-25
      • 2019-09-27
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多