在wordpress程序根目錄下新建一個php文件,粘貼下面的代碼
如下面的代碼注釋,修改$CID這個分類id,就可以獲取這個分類下的文章了。這個查詢需要聯合三個表wp_posts、wp_term_relationships、wp_term_taxonomy,
根據term_taxonomy_id獲取文章標號,post_status = ‘publish’ 是指文章已經發布,post_type=’post’ 是指記錄類型是文章,taxonomy = ‘category’ 是指類型是目錄。
然后運行這個文件,就可以讀取這個分類下的所有的文章了。
<?php include ( "wp-config.php" ) ; require_once (ABSPATH.'wp-blog-header.php'); global $wpdb; $CID = 1;//分類id,只支持一個分類 $sql="SELECT ID,post_title,post_content FROM wp_posts,wp_term_relationships,wp_term_taxonomy WHERE ID=object_id and wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id and post_type='post' and post_status = 'publish' and wp_term_relationships.term_taxonomy_id = $CID and taxonomy = 'category' order by ID desc"; $myrows = $wpdb->get_results($sql); foreach ($myrows as $b) { echo $b->ID."<br />";//這是文章ID echo $b->post_title."<br />";//這是文章標題 echo $b->post_content."<br />";//這是文章內容 } ?>