【问题标题】:WordPress - PHP list of PDF filesWordPress - PDF 文件的 PHP 列表
【发布时间】:2015-07-21 12:11:37
【问题描述】:

我已经写了这段代码:

function userfunc_get_file_links($sub_folder) {
    if ( is_user_logged_in() ) {
        $directory = get_stylesheet_directory()."/files/".$sub_folder;
        $scanned_directory = array_diff(scandir($directory), array('..', '.'));
        echo "<ul class='support_links'>";
        foreach ($scanned_directory as $key => $value) {
            echo "<li><a href='files/".$value."'>".$value."</a></li>";
        }
        echo "</ul>";
    }
}

我已将它添加到我的函数文件中,并且可以通过页面模板成功应用它并显示文件链接列表。

(模板文件):

<?php
/*
Template Name: supportpage
*/
?>
<?php get_header(); the_post(); ?>
    <div id="content" class="inner-page">
        <div class="main-content">
            <div class="brack-cum">
                <div class="inner-small">
                <?php 
                   if (function_exists('wp_bac_breadcrumb')) {wp_bac_breadcrumb();}
                ?>
                </div>
            </div>
            <div class="title">
                <div class="inner-small">
                    <h2><?php the_title(); ?></h2>
                </div>
            </div>
            <div class="content-page inner-small">
                <?php
                    the_content(); 
                ?>
            </div>
        </div>
    <?php get_footer(); }?>

但是,当我单击它们时,它会出现 404 错误,我不知道为什么。我已经尝试重置我的永久链接并检查 FTP 路径(它 100% 正确)。

如果我尝试将我的 /files/ 文件夹移动到服务器的根目录

$directory = home_url()."/htdocs/files".$sub_folder;

返回

警告:scandir(http://localhost/wordpress/files/application_notes): 无法打开目录:未在 C:\xampp\apps\wordpress\htdocs\wp-content\themes\twentythirteen-child\user_functions.php 在第 5 行

这些文件大部分是 pdf 文件(“how_to_do_stuff.pdf”等)。

关于我可以从这里采取的步骤有什么想法吗?

EDIT1:原始代码显示链接,但点击返回 WordPress 404:

var_dump($目录)

string(93) "C:\xampp\apps\wordpress\htdocs/wp-content/themes/twentythirteen-child/files/application_notes"

var_dump($scanned_directory)

array(3) {
  [2]=>
  string(9) "test1.pdf"
  [3]=>
  string(9) "test2.pdf"
  [4]=>
  string(9) "test3.pdf"
}

使用产生警告的代码:

var_dump($目录)

string(57) "http://localhost/wordpress/htdocs/files/application_notes"

var_dump($scanned_directory)

NULL

【问题讨论】:

  • 转储 $directory 和 $scanned_directory,你会得到什么?
  • @Mihai 编辑显示
  • 那个目录对我来说看起来很奇怪,Windows 使用 \ 而不是 linux 切换到 /
  • 您混淆了 URL 和文件路径...
  • 我认为确实是通过 WordPresses 函数获取文件路径“get_stylesheet_directory_uri()”以链接到我的文件的 URI,但我不确定。同样的警告。

标签: php wordpress


【解决方案1】:

你已经接近了......但如果你希望人们能够点击链接,你需要输出 URL:

function userfunc_get_file_links($sub_folder) {
    if ( is_user_logged_in() ) {
        $directory = get_stylesheet_directory()."/files/".$sub_folder;
        $scanned_directory = array_diff(scandir($directory), array('..', '.'));
        echo "<ul class='support_links'>";
        foreach ($scanned_directory as $key => $value) {
            echo "<li><a href='".get_stylesheet_directory_uri() . "/files/" . $sub_folder . "/" . $value . "'>".$value."</a></li>";
        }
        echo "</ul>";
    }
}

这是每个部分应该做的:

get_stylesheet_directory_uri() . 
// http://example.com/wp-content/themes/yourtheme

'/files/' . $sub_folder . '/' . $value
// /files/subfolder/filename.txt

【讨论】:

  • 我不敢相信我错过了这么明显的东西!!非常感谢,非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-12
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
相关资源
最近更新 更多