wordpress 获取置顶文章 获取随机文章 获取指定标签文章 获取指定分类文章

wordpress获取随机文章

<?php
$randargs = array(
‘ignore_sticky_posts’ => 1 ,//取消文章置顶 如果不加这一行,输出的文章将包括设置的置顶文章
‘showposts’ => 10,//文章数量
‘orderby’ => ‘rand’ //随机排序

);
query_posts($randargs );
while ( have_posts() ) : the_post(); ?>
<div class=”TopTitle”><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></div>
<?php
endwhile;
wp_reset_query(); ?>

 

wordpress获取置顶文章

<?php

/* 获取所有置顶文章 */
$sticky = get_option( ‘sticky_posts’ );

/* 对这些文章排序, 日期最新的在最上 */
rsort( $sticky );

/* 获取5篇文章 */
$sticky = array_slice( $sticky, 0, 6 );

/* 输出这些文章 */
query_posts( array( ‘post__in’ => $sticky, ‘ignore_sticky_posts’ => 1 ) );
while ( have_posts() ) : the_post(); ?>
<div class=”TopTitle”><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></div>
<?php
endwhile;
wp_reset_query(); ?>

wordpress获取指定标签文章

<?php
$tags = get_the_tags();//获取当前文章标签数组  get_the_tags() 改成 get_the_category()就是获取当前文章的标签了 下文也要做相应修改 百度一堆
foreach ($tags as $tag){
$tagname = $tag->name;//标签名
}
$argssingle = array(
‘showposts’ => 5,
‘orderby’ => ‘rand’,//随机排序 如果rand改成desc就是降序排列 asc升序
‘tag’ => $ tagname
);

query_posts($argssingle);

if (have_posts()) : while (have_posts()) : the_post();?>

<div class=”TopTitle”><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></div>

<?php
endwhile;
endif; ?>

未经允许不得转载:前端撸码笔记 » wordpress 获取置顶文章 获取随机文章 获取指定标签文章 获取指定分类文章

上一篇:

下一篇: