首页>技术文章>Wordpress中显示文章链接及内容的5个方法(原创)

在Wordpress中要显示文章链接及内容,可以采用修改模板,或者使用插件的方式,具体有以下5种方法:
1.显示父页面及子页面的链接

//if the post has a parent
if($post->post_parent){
//collect ancestor pages
$relations = get_post_ancestors($post->ID);
//get child pages
$result = $wpdb->get_results( “SELECT ID FROM wp_posts WHERE post_parent = $post->ID AND post_type=’page’” );
if ($result){
foreach($result as $pageID){
array_push($relations, $pageID->ID);
}
}
//add current post to pages
array_push($relations, $post->ID);
//get comma delimited list of children and parents and self
$relations_string = implode(“,”,$relations);
//use include to list only the collected pages.
$sidelinks = wp_list_pages(“title_li=&echo=0&include=”.$relations_string);
}else{
// display only main level and children
$sidelinks = wp_list_pages(“title_li=&echo=0&depth=1&child_of=”.$post->ID);
}

if ($sidelinks) {
the_title();
echo $sidelinks;
}

2.显示子页面的链接

$children = wp_list_pages(“title_li=&child_of=”.$post->ID.”&echo=0″);
if ($children) {
echo $children;
}

3.显示指定页面的链接
wp_list_pages(‘exclude=17,38′ );
展示页面时默认会出现一个标题“页面”,可以用如下语句去除
wp_list_pages(‘exclude=17,38′&title_li=);

4.显示子页面内容
$mypages = get_pages( array( ‘child_of’ => $post->ID, ‘sort_column’ => ‘post_date’, ‘sort_order’ => ‘desc’ ) );

foreach( $mypages as $page ) {
$content = $page->post_content;
if ( ! $content ) // Check for empty page
continue;

$content = apply_filters( ‘the_content’, $content );
echo get_page_link( $page->ID );
echo $page->post_title; ?>
echo $content;
}
5.使用List category posts(推荐)
List category posts功能较为强大,在页面中使用shortcode或在widget中加某类别文章的列表。

本文地址:http://www.caihonger.com/tech16/



请写下您的评论