簡體   English   中英

寫入文件在functions.php - Wordpress中不起作用

[英]Writing to file doesn't work in functions.php - Wordpress

我想在Wordpress中創建函數,它會將最新的帖子標題寫入文件,但找不到有效的代碼組合。 鈎子對嗎? 我究竟做錯了什么? 該文件位於主目錄中。

我試圖把文件放在其他目錄中,google的所有內容都是這樣的:bloginfo('template_directory')?> / new_post_check.txt

add_action('publish_post' , 'alert_new_post');

function alert_new_post(){

    $path = 'new_post_check.txt';

    file_put_contents( $path, "a" ); 
}

沒有錯誤消息。

試試這段代碼,請注意正在寫入的文本文件位於wordpress安裝的根目錄中,每次發布帖子時都會被覆蓋,因此它只有一個上一個發布帖子的標題:

add_action('publish_post', 'alert_new_post', 10, 2);

function alert_new_post($ID, $post){ 
    $path = 'new_post_check.txt';
    file_put_contents($path, $post->post_title); 
}

如果您希望繼續附加到列表,並將最新的發布帖子添加到列表中,那么您首先必須閱讀文件的輸入,如下所示:

add_action('publish_post', 'alert_new_post', 10, 2);

function alert_new_post($ID, $post){
    $path = 'new_post_check.txt';
    $post_titles = file_get_contents($path);
    // Append a new title to the file
    $post_titles .= $post->post_title."\n";
    file_put_contents($path, $post_titles); 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM