douwei1930 2014-04-22 16:17
浏览 13
已采纳

如何在不使用PHP循环的情况下单独显示第一个和第二个最近的帖子?

I need to display separately the first and second most recent posts from a category without using a loop. Below is what I've tried; the item1 div should display the most recent and the item2 div should display the second most recent.

  <div class = 'item item1'>
  <?php get_posts('numberposts=1&offset=1&category='); ?>
  <?php while (have_posts()) : the_post(); ?>
  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><h3><?php the_title(); ?></h3></a>
  <?php endwhile;?>
    </div>

   <div class = 'item item2'>
  <?php get_posts('numberposts=2&offset=1&category='); ?>
  <?php while (have_posts()) : the_post(); ?>
  <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><h3><?php the_title(); ?></h3></a>
  <?php endwhile;?>
    </div>
  • 写回答

2条回答 默认 最新

  • doumeilmikv7099 2014-04-22 16:22
    关注

    There is a built-in function for this called: wp_get_recent_posts()

    With the following params:

    <?php $args = array(
        'numberposts' => 10,
        'offset' => 0,
        'category' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'include' => ,
        'exclude' => ,
        'meta_key' => ,
        'meta_value' =>,
        'post_type' => 'post',
        'post_status' => 'draft, publish, future, pending, private',
        'suppress_filters' => true );
    
        $recent_posts = wp_get_recent_posts( $args, ARRAY_A );
    ?>
    

    Example:

    <?php
        $args = array( 'numberposts' => '2' );
        $recent_posts = wp_get_recent_posts( $args );
        foreach( $recent_posts as $recent ){
            echo '<div><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" ><h3>' .   $recent["post_title"].'</h3></a></div>';
        }
    ?>
    

    Source: https://codex.wordpress.org/Function_Reference/wp_get_recent_posts

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序