WordPress纯代码制作HTML网站地图

来自:互联网
时间:2018-08-28
阅读:

xml网站地图直接用插件?Google XML Sitemaps,HTML网站地图可以调用WordPress文章存档页面来实现

方法一:SQL读取数据库

方法一展示的存档页面效果就是本站的Sitemap效果,其实就是创建一个自定义的页面模板,操作起来也很简单,不需要你懂太多的编程技术,唯一要配置的就是修改sitemap.xml的链接。方法一的优点是,我使用的是SQL直接从数据库中读取数据,而且只进行两次数据库连接,避免了使用WordPress内置函数,速度相对来说会快那么一点点;缺点是,你要复制的代码相对来说长点。下面看看具体步骤:

1、在当前主题目录下新建一个php文件,命名为sitemap.php

2、头部加入以下代码

<?php
/*
* Template Name: sitemap
* @author: Ludou
* @Blog : http://www.ludou.org/
*/

3、从数据库调用所有分类,所有文章

<div>
<?php
$categoryPosts = $wpdb->get_results("
SELECT post_title, ID, post_name, slug, wp_terms.term_id AS catID, wp_terms.name AS categoryname
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE wp_posts.ID = wp_term_relationships.object_id
AND wp_terms.term_id = wp_term_taxonomy.term_id
AND wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id
AND wp_term_taxonomy.taxonomy = "category"
AND wp_posts.post_status = "publish"
AND wp_posts.post_type = "post"
ORDER BY wp_terms.term_id, wp_posts.post_date DESC");$postID = 0;
if ( $categoryPosts ) :
$category = $categoryPosts[0]->catID;
foreach ($categoryPosts as $key => $mypost) :
if($postID == 0) {
echo "<h3>分类: <a title="".$mypost->categoryname."" href="".get_category_link($mypost->catID)."">".$mypost->categoryname."</a></h3>";
echo "<ul>";
}if($category == $mypost->catID) {
?>
<li><a title="<?php echo $mypost->post_title; ?>" href="<?php echo get_permalink( $mypost->ID ); ?>"><?php echo $mypost->post_title; ?></a></li>
<?php
$category = $mypost->catID;
$postID++;
}
else {
echo "</ul>";
echo "<h3>分类: <a title="".$mypost->categoryname."" href="".get_category_link($mypost->catID)."">".$mypost->categoryname."</a></h3>";
echo "<ul>";
?>
<li><a title="<?php echo $mypost->post_title; ?>" href="<?php echo get_permalink( $mypost->ID ); ?>"><?php echo $mypost->post_title; ?></a></li>
<?php
$category = $mypost->catID;
$postID = 1;
}
endforeach;
endif;
echo "</ul>";
?>
<h3>页面</h3>
<ul>
<?php
// 读取所有页面
$mypages = $wpdb->get_results("
SELECT post_title, post_name, ID
FROM {$wpdb->prefix}posts
WHERE post_status = "publish"
AND post_type = "page"");if ( $mypages ) :
foreach ($mypages as $mypage) :
?>
<li><a title="<?php echo $mypage->post_title; ?>" href="<?php echo get_permalink( $mypage->ID ); ?>"><?php echo $mypage->post_title; ?></a></li>
<?php endforeach;endif; ?>
</ul>
<p><a href="http://www.sjyhome.com/sitemap.xml">sitemap.xml</a></p>
</div>

创建一个页面,标题为网站地图,页面地址为sitemap,内容不要填,右侧模板选择 sitemap,大功告成!

方法二:使用wp_get_archives函数

把上面调用数据库内容的代码改成

echo?"<ul>";
wp_get_archives("type=postbypost&show_post_count=1");
echo?"</ul>";

不过这里只是纯粹地列出所有的文章,建议还是用第一种方法。

参考:http://www.ludou.org/make-archives-of-wordpress-easily.html

返回顶部
顶部