duanmorong9597 2015-10-01 11:47 采纳率: 100%
浏览 30
已采纳

Wordpress自定义查询和粘贴帖子

I'm trying to display the posts of the current month keeping the sticky post on the top.

This is my code:

$today = getdate();

$args = array( 
    'monthnum' => $today["mon"], 
    'year' => $today["year"]
);

query_posts( $args );

while (have_posts() ) : the_post();

    // some code...

endwhile;

The result is a list of the posts in chronological order without holding the sticky post on the top.

How can I fix it?

  • 写回答

1条回答 默认 最新

  • dongyi1748 2015-10-01 13:47
    关注

    Here's my chip in on this, I haven't tested as I'm at work but let me know if you have any problems and I'll test and fix when I get home.

    <?php
    /*--- Create a sticky loop ---*/
    
    $sticky = get_option( 'sticky_posts' );
    
    // These args will return only one sticky post
    $stickyArgs = array(
        'post__in'  => $sticky,
        // remove these to return all sticky posts
        'posts_per_page' => 1,
        'ignore_sticky_posts' => 1
    );
    
    // create your query
    $stickyQuery = new WP_Query( $stickyArgs );
    
        if ( isset($sticky[0]) ) {
            //
            // Post Content here
            //
        }
    
    
    wp_reset_query();
    
    // I haven't comented this section because it was your code
    $today = getdate();
    
    $todayArgs = array( 
      'monthnum' => $today["mon"], 
      'year' => $today["year"]
    );
    
    $todayQuery = new WP_Query( $todayArgs );
    
        if ($todayQuery->have_posts()) {
            while($todayQuery->have_posts()) {
                $todayQuery->the_post();
                    //
                    // Post Content here
                    //
            } // end while
        } // end if
    
    // reset the query again.. this is optional, I don't know what else is on your page.
    wp_reset_query();
    ?>
    

    Let me know how you get on :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失