Php WordPress插件-将字符串数据导出到可下载的txt fie
我正在创建的一个新WordPress插件的一部分是,我想有一个选项来下载一个包含基本字符串值数据的txt文件 该特定部件的功能是:Php WordPress插件-将字符串数据导出到可下载的txt fie,php,wordpress,Php,Wordpress,我正在创建的一个新WordPress插件的一部分是,我想有一个选项来下载一个包含基本字符串值数据的txt文件 该特定部件的功能是: 函数获取页面链接() { $pages=get_pages('post_status=publish'); foreach($页为$页) { $pagetitle=$page->post_标题; $pagelink=get_permalink($page->ID); 回显“{$pagetitle}”; 回音“\n”; 回显“{$pagelink}”; } }fil
函数获取页面链接()
{
$pages=get_pages('post_status=publish');
foreach($页为$页)
{
$pagetitle=$page->post_标题;
$pagelink=get_permalink($page->ID);
回显“{$pagetitle}”;
回音“\n”;
回显“{$pagelink}”;
}
}
file\u put\u contents
将创建一个不存在的文件,并将内容写入其中,而不是附加,正如您所知。在循环中调用它将导致它不断覆盖您编写的链接,直到循环结束,并且您将只得到文件中的最后一个链接
下面的代码将在一个文件中写入所有链接,每行一个链接。再次调用此函数将覆盖仍然包含所有链接的文件,以便在您删除页面或创建新页面时更新
function get_page_links()
{
$pages = get_pages( 'post_status=publish' );
// use the code below if your making a plugin
// this will be found on:
// path/to/yourplugin/links.txt
$file = plugin_dir_path(__FILE__) . 'links.txt';
// use the code below if you're making a theme
// this will be found on:
// path/to/yourtheme/links.txt
// $file = get_template_directory() . '/links.txt'
$n_handle_file = fopen($file,'w');
foreach ( $pages as $page )
{
$pagetitle = $page->post_title;
$pagelink = get_permalink( $page->ID );
echo "{$pagetitle}";
echo "\n";
echo "{$pagelink}";
fprintf($n_handle_file, "%s\n", $pagelink);
}
fclose($n_handle_file);
}
file\u put\u contents
将创建一个不存在的文件,并将内容写入其中,而不是附加,正如您所知。在循环中调用它将导致它不断覆盖您编写的链接,直到循环结束,并且您将只得到文件中的最后一个链接
下面的代码将在一个文件中写入所有链接,每行一个链接。再次调用此函数将覆盖仍然包含所有链接的文件,以便在您删除页面或创建新页面时更新
function get_page_links()
{
$pages = get_pages( 'post_status=publish' );
// use the code below if your making a plugin
// this will be found on:
// path/to/yourplugin/links.txt
$file = plugin_dir_path(__FILE__) . 'links.txt';
// use the code below if you're making a theme
// this will be found on:
// path/to/yourtheme/links.txt
// $file = get_template_directory() . '/links.txt'
$n_handle_file = fopen($file,'w');
foreach ( $pages as $page )
{
$pagetitle = $page->post_title;
$pagelink = get_permalink( $page->ID );
echo "{$pagetitle}";
echo "\n";
echo "{$pagelink}";
fprintf($n_handle_file, "%s\n", $pagelink);
}
fclose($n_handle_file);
}
从您的代码中可以看出:
1) 循环中下面的行没有附加到$pagelink的上一个值
$pagelink=get_permalink($page->ID);
应该改成
$pagelink.=get_permalink($page->ID);
2) 或者可能存在文件写入权限问题
试着换线
$file='file.txt';到
$file=插件目录路径(文件)。'txt'文件
请分享完整的代码这似乎来自您的代码:
1) 循环中下面的行没有附加到$pagelink的上一个值
$pagelink=get_permalink($page->ID);
应该改成
$pagelink.=get_permalink($page->ID);
2) 或者可能存在文件写入权限问题
试着换线
$file='file.txt';到
$file=插件目录路径(文件)。'txt'文件
请分享完整的代码Hi-谢谢,它工作得很好,现在正在将链接列表写入.txt文件。Hi-谢谢,它工作得很好,现在正在将链接列表写入.txt文件。