
1、插件。如Content Index for WordPress
2、像高时银博客那亲通过代码来实现。在主题的functions.php文件中添加如下代码:
function article_index($content) {
$matches = array()
$ul_li = ''
$r = "/<h3>([^<]+)</h3>/im"
if(preg_match_all($r, $content, $matches)) {
foreach($matches[1] as $num =>$title) {
$content = str_replace($matches[0][$num], '<h3 id="title-'.$num.'">'.$title.'</h3>', $content)
$ul_li .= '<li><a href="#title-'.$num.'" title="'.$title.'">'.$title."</a></li>n"
}
$content = "n<div id="article-index">
<strong>文章目录</strong>
<ul id="index-ul">n" . $ul_li . "</ul>
</div>n" . $content
}
return $content
}
add_filter( "the_content", "article_index" )
然后在发表文章时选择要做目录的词语,并用<h3>把它们括起来,如<h3>我是索引标题</h3>。
再就是添加CSS样式就可以了。
这个还不简单吗,点击文章发布或者比选择任意一篇文章,点击编辑按钮进入到文章编辑页面中,将页面滑到文件最底部,将鼠标移到底部单击即可,文章中出现文字光标后就可以输入文字内容了!望采纳!
方法一:标签相关首先获取文章的所有标签,接着获取这些标签下的 n 篇文章,那么这 n 篇文章就是与该文章相关的文章了。现在可以见到的WordPress相关文章插件都是使用的这个方法。下面是实现的代码:
<ul id="tags_related">
<?php
global $post
$post_tags = wp_get_post_tags($post->ID)
if ($post_tags) {
foreach ($post_tags as $tag) {
// 获取标签列表
$tag_list[] .= $tag->term_id
}
// 随机获取标签列表中的一个标签
$post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ]
// 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表
$args = array(
'tag__in' =>array($post_tag),
'category__not_in' =>array(NULL), // 不包括的分类ID
'post__not_in' =>array($post->ID),
'showposts' =>6, // 显示相关文章数量
'caller_get_posts' =>1
)
query_posts($args)
if (have_posts()) {
while (have_posts()) {
the_post()update_post_caches($posts)?>
<li>* <a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title_attribute()?>"><?php the_title()?></a></li>
<?php
}
}
else {
echo '<li>* 暂无相关文章</li>'
}
wp_reset_query()
}
else {
echo '<li>* 暂无相关文章</li>'
}
?>
</ul>
方法二:分类相关
本方法是通过获取该文章的分类id,然后获取该分类下的文章,来达到获取相关文章的目的。
<ul id="cat_related">
<?php
global $post
$cats = wp_get_post_categories($post->ID)
if ($cats) {
$args = array(
'category__in' =>array( $cats[0] ),
'post__not_in' =>array( $post->ID ),
'showposts' =>6,
'caller_get_posts' =>1
)
query_posts($args)
if (have_posts()) {
while (have_posts()) {
the_post()update_post_caches($posts)?>
<li>* <a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title_attribute()?>"><?php the_title()?></a></li>
<?php
}
}
else {
echo '<li>* 暂无相关文章</li>'
}
wp_reset_query()
}
else {
echo '<li>* 暂无相关文章</li>'
}
?>
</ul>
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)