[英]Creating a new Page of Posts in Wordpress
我目前正在嘗試使用wordpress制作電子雜志,並且已完成大部分工作。 主頁顯示該電子雜志的版本中包含的“作品”列表。 我要這樣做,以便當一個版本過期(當前使用Post Expirator插件)時,會自動創建一個新頁面,類似於顯示該特定(現已過期)版本索引的首頁。
我對使用PHP的經驗不是很豐富,而且還是wordpress的新手。 我該怎么做?
這個想法就是這樣,您只需要獲取到期日期並為其設置條件即可。 您只需要具備基本的php技能即可。 這是邏輯
if($curdate > $expiration_date){ //you can change the condition into ">=" if you want to create a post on and after expiration date
//sample wordpress wp_insert_post
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
wp_insert_post( $my_post );
}
有關更多信息,請訪問http://codex.wordpress.org/Function_Reference/wp_insert_post
這是我最終完成的工作,以Felipe的建議為起點。 這樣做的方式可能不太復雜,但是正如我所說,我只是一個初學者,所以我想到了:
首先,我創建了一個volnum變量,該變量跟蹤當前的卷號。 然后,我緩存了首頁,以便以后可以將其另存為獨立的html文檔:這是在index.php的開頭,在get_header()之前。
<?php $volnum; ?>
<?php ob_start(); ?>
在首頁上,我有一篇社論,旁邊是內容索引。 我保存的是編輯標簽的標簽(始終為“ voln”,其中“ n”是卷號)卷號(也許因為編輯標簽只有一個標簽,所以不需要foreach):
<?php $tags = get_the_tags();
foreach ($tags as $tag){
$volnum = $tag->name;
}
?>
最后,在文檔末尾的最后一個html之后,我添加了以下代碼:
<?php
$handle = opendir("past_vol/");
$numOfFiles = count($handle);
$volExists = false;
for($i=0;$i<=$numOfFiles;$i++){
$name = readdir($handle);
if($volnum.".html" == ($name)){
$volExists = true;
continue;
}
}
if($volExists == false){
$cachefile = "past_vol/".$volnum.".html";
$fp = fopen($cachefile, 'w');
fwrite($fp, ob_get_contents());
fclose($fp);
}
closedir($handle);
ob_end_flush();
?>
“ past_vol”是我保存過去的html文件的目錄。 這樣就打開了目錄,對文件數量進行了計數,並啟動了遍歷每個文件名的循環。 如果文件與$ volnum同名,則$ volExists為true。 如果循環結束時$ volExists為false,則它將保存緩存的頁面。
再說一次,可能可以對其進行很多優化,但是現在可以了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.