WordPress调用最新、随机、受欢迎的、指定的分类代码

福利资源

发表文章数:936

热门标签

, , ,
首页 » 技术文档 » WP教程 » 正文
福利购

WordPress调用最新、随机、受欢迎的、指定的分类代码

1.调用最新文章

<?<span%20class="pln">php query_posts<span%20class="pun">(<span%20class="str">'showposts=6&cat=-111'<span%20class="pun">); ?><span%20class="pln">  // 显示篇数和排除分类

<span%20class="tag"><ul>  

<?<span%20class="pln">php <span%20class="kwd">while (<span%20class="pln">have_posts<span%20class="pun">()) :<span%20class="pln"> the_post<span%20class="pun">(); ?>  

<li><span%20class="pln"><a href="<span%20class="pun"><?<span%20class="pln">php the_permalink<span%20class="pun">() ?><span%20class="pln">"><span%20class="pun"><?<span%20class="pln">php the_title<span%20class="pun">(); ?><span%20class="tag"></a></li>  

<?<span%20class="pln">php endwhile<span%20class="pun">;?>  

</ul>

2.调用指定分类文章

<ul>

<?<span%20class="pln">php

    $args<span%20class="pun">=<span%20class="pln">array<span%20class="pun">(

        'cat' => 1<span%20class="pun">,   // 分类ID

        'posts_per_page' => 10<span%20class="pun">, // 显示篇数

    );<span%20class="pln">

    query_posts<span%20class="pun">(<span%20class="pln">$args<span%20class="pun">);

    if<span%20class="pun">(<span%20class="pln">have_posts<span%20class="pun">()) : while (<span%20class="pln">have_posts<span%20class="pun">()) :<span%20class="pln"> the_post<span%20class="pun">();

?>

    <li><span%20class="pln">

        <a href="<span%20class="pun"><?<span%20class="pln">php the_permalink<span%20class="pun">(); ?><span%20class="pln">"><span%20class="pun"><?<span%20class="pln">php the_title<span%20class="pun">(); ?><span%20class="tag"></a> 

    </li>

<?<span%20class="pln">php  endwhile<span%20class="pun">;<span%20class="pln"> endif<span%20class="pun">;<span%20class="pln"> wp_reset_query<span%20class="pun">(); ?>

</ul>

3.调用整站随机文章

<?<span%20class="pln">php

<span%20class="kwd">global<span%20class="pln"> $post<span%20class="pun">;<span%20class="pln">

$postid <span%20class="pun">=<span%20class="pln"> $post<span%20class="pun">-><span%20class="pln">ID<span%20class="pun">;<span%20class="pln">

$args <span%20class="pun">=<span%20class="pln"> array<span%20class="pun">( <span%20class="kwd">orderby<span%20class="pun">’ => <span%20class="pln">rand<span%20class="pun">’, <span%20class="pln">post__not_in<span%20class="pun">’ =><span%20class="pln"> array<span%20class="pun">(<span%20class="pln">$post<span%20class="pun">-><span%20class="pln">ID<span%20class="pun">), <span%20class="pln">showposts<span%20class="pun">’ => 10<span%20class="pun">); // 显示篇数<span%20class="pln">

$query_posts <span%20class="pun">= new<span%20class="pln"> WP_Query<span%20class="pun">();<span%20class="pln">

$query_posts<span%20class="pun">-><span%20class="pln">query<span%20class="pun">(<span%20class="pln">$args<span%20class="pun">);

?>

<?<span%20class="pln">php <span%20class="kwd">while (<span%20class="pln">$query_posts<span%20class="pun">-><span%20class="pln">have_posts<span%20class="pun">()) :<span%20class="pln"> $query_posts<span%20class="pun">-><span%20class="pln">the_post<span%20class="pun">(); ?>

<li><span%20class="pln"><a href="<span%20class="pun"><?<span%20class="pln">php the_permalink<span%20class="pun">(); ?><span%20class="pln">"><span%20class="pun"><?<span%20class="pln">php the_title<span%20class="pun">(); ?><span%20class="tag"></a></li>

<?<span%20class="pln">php endwhile<span%20class="pun">; ?>

</ul> 

4.调用同分类随机文章

<ul>

<?<span%20class="pln">php

$cat <span%20class="pun">=<span%20class="pln"> get_the_category<span%20class="pun">();

foreach<span%20class="pun">(<span%20class="pln">$cat <span%20class="kwd">as<span%20class="pln"> $key<span%20class="pun">=><span%20class="pln">$category<span%20class="pun">){<span%20class="pln">

$catid <span%20class="pun">=<span%20class="pln"> $category<span%20class="pun">-><span%20class="pln">term_id<span%20class="pun">;}<span%20class="pln">

$args <span%20class="pun">=<span%20class="pln"> array<span%20class="pun">(<span%20class="str">'orderby' => 'rand'<span%20class="pun">,<span%20class="str">'showposts' => 8<span%20class="pun">,<span%20class="str">'cat' =><span%20class="pln"> $catid <span%20class="pun">); // 显示篇数<span%20class="pln">

$query_posts <span%20class="pun">= new<span%20class="pln"> WP_Query<span%20class="pun">();<span%20class="pln">

$query_posts<span%20class="pun">-><span%20class="pln">query<span%20class="pun">(<span%20class="pln">$args<span%20class="pun">);

while (<span%20class="pln">$query_posts<span%20class="pun">-><span%20class="pln">have_posts<span%20class="pun">()) :<span%20class="pln"> $query_posts<span%20class="pun">-><span%20class="pln">the_post<span%20class="pun">();?>

<li><span%20class="pln"><a href="<span%20class="pun"><?<span%20class="pln">php the_permalink<span%20class="pun">(); ?><span%20class="pln">"><span%20class="pun"><?<span%20class="pln">php the_title<span%20class="pun">(); ?><span%20class="tag"></a></li>

<?<span%20class="pln">php endwhile<span%20class="pun">;?>

<?<span%20class="pln">php wp_reset_query<span%20class="pun">(); ?>

</ul>

5.调用整站热门文章(按评论数)

<ul>

<?<span%20class="pln">php

$post_num <span%20class="pun">= 10<span%20class="pun">; // 显示篇数<span%20class="pln">

$args <span%20class="pun">=<span%20class="pln"> array<span%20class="pun">(

<span%20class="pln">post_password<span%20class="pun">’ => ”,

<span%20class="pln">post_status<span%20class="pun">’ => <span%20class="pln">publish<span%20class="pun">’, // 只选公开的文章.

<span%20class="pln">post__not_in<span%20class="pun">’ =><span%20class="pln"> array<span%20class="pun">(<span%20class="pln">$post<span%20class="pun">-><span%20class="pln">ID<span%20class="pun">),<span%20class="com">//排除当前文章

<span%20class="pln">caller_get_posts<span%20class="pun">’ => 1<span%20class="pun">, // 排除置顶文章.

<span%20class="kwd">orderby<span%20class="pun">’ => <span%20class="pln">comment_count<span%20class="pun">’, // 依评论数排序.

<span%20class="pln">posts_per_page<span%20class="pun">’ =><span%20class="pln"> $post_num

<span%20class="pun">);<span%20class="pln">

$query_posts <span%20class="pun">= new<span%20class="pln"> WP_Query<span%20class="pun">();<span%20class="pln">

$query_posts<span%20class="pun">-><span%20class="pln">query<span%20class="pun">(<span%20class="pln">$args<span%20class="pun">);

while<span%20class="pun">(<span%20class="pln"> $query_posts<span%20class="pun">-><span%20class="pln">have_posts<span%20class="pun">() ) {<span%20class="pln"> $query_posts<span%20class="pun">-><span%20class="pln">the_post<span%20class="pun">(); ?>

<li><span%20class="pln"><a href="<span%20class="pun"><?<span%20class="pln">php the_permalink<span%20class="pun">(); ?><span%20class="pln">"><span%20class="pun"><?<span%20class="pln">php the_title<span%20class="pun">(); ?><span%20class="tag"></a></li>

未经允许不得转载:作者:福利资源, 转载或复制请以 超链接形式 并注明出处 福利资源网
原文地址:《WordPress调用最新、随机、受欢迎的、指定的分类代码》 发布于2019-10-08

分享到:
赞(0) 打赏 生成海报

评论 抢沙发

7 + 2 =


长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu4.0主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录
切换登录

注册