dongqiao2077 2014-07-30 05:12
浏览 156
已采纳

WordPress:获取所有自定义帖子页面的列表并添加分页

I have a code that gets all thumbnails from custom post type named 'pavideo' and works as a shortcode:

// Shortcode to get thumbnails of all videos and create pagination
function pavideo_get_thumbnails_and_pagination(){
   ob_start();
   $content = '';
   $query = new WP_Query( array( 'post_type' => 'pavideo', 'orderby' => 'desc', 'posts_per_page' => 4 ) );
   while ( $query->have_posts() ) : $query->the_post();
   // Check if post has thumbnail first
   if ( has_post_thumbnail() ) {
       echo '<div class="other-video-block">
           <a href="';
           the_permalink();
           echo '" title="';
           the_title();
           echo '">';
           echo get_the_post_thumbnail( $post->ID );
           echo '</a><br>';
    echo '</div>';
} 
endwhile;
   $content = ob_get_contents();
   ob_end_clean();
   return $content;
}

// Define thumbnail and pagination shortcode
add_shortcode('pavideo_thumb','pavideo_get_thumbnails_and_pagination');

I need to get 4 thumbnails, and than create a pagination thing like: 1,2,3 and so on.

How could I do that?

Thank you!

  • 写回答

1条回答 默认 最新

  • duanji7182 2014-07-30 06:41
    关注

    Ok, I got it! :)

    I used wp-pagenavi plugin to display pagination like 1,2,3 and so on. My code now looks like that:

    // Shortcode to get thumbnails of all videos and create pagination
    function pavideo_get_thumbnails_and_pagination(){
       ob_start();
    
       $paged = (get_query_var('page')) ? get_query_var('page') : 1;
    
       $content = '';
    
       // The query
       $query = new WP_Query('post_type=pavideo&order=desc&posts_per_page=3&paged='.$paged);
    
       echo '<div class="pavideo-hp-videos">';
    
       while ( $query->have_posts() ) : $query->the_post();
       // Check if post has thumbnail first
       if ( has_post_thumbnail() ) {
           echo '<div class="pavideo-hp-single-video">
               <a href="';
               the_permalink();
               echo '" title="';
               the_title();
               echo '">';
               echo get_the_post_thumbnail( $post->ID );
               echo '</a>';
           echo '</div>';
       } 
       endwhile;
    
       echo '</div>';
    
       echo '<div class="pavideo-pagination">';
       wp_pagenavi( array( 'query' => $query ) );
       echo '</div>';
    
       $content = ob_get_contents();
       ob_end_clean();
       return $content;
    }
    
    // Define thumbnail and pagination shortcode
    add_shortcode('pavideo_thumb','pavideo_get_thumbnails_and_pagination');
    

    And it works just fine! :)

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

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器