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

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!

我有一个代码,可以从名为“pavideo”的自定义帖子类型中获取所有缩略图,并作为短代码使用: p>

  //获取所有视频的缩略图并创建分页的短代码
 
函数pavideo_get_thumbnails_and_pagination(){
 
n_start(); 
 $ content =''; 
 $ query = new  WP_Query(array('post_type'=&gt;'pavideo','orderby'=&gt;'desc','posts_per_page'=&gt; 4)); 
 while($ query-&gt; have_posts()):$ query  - &gt; the_post(); 
 //检查帖子是否有缩略图
 if(has_post_thumbnail()){
 echo'&lt; div class =“other-video-block”&gt; 
&lt; a href  =“'; 
 the_permalink(); 
 echo'”title =“'; 
 the_title(); 
 echo'”&gt;'; 
 echo get_the_post_thumbnail($ post-&gt; ID); 
  echo'&lt; / a&gt;&lt; br&gt;'; 
 echo'&lt; / div&gt;'; 
} 
endwhile; 
 $ content = ob_get_contents(); 
 ob_end_clean(); 
返回$ content  ; 
} 
 
 //定义 umbnail和分页短代码
add_shortcode('pavideo_thumb','pavideo_get_thumbnails_and_pagination'); 
  code>  pre> 
 
 

我需要获得4个缩略图,而不是创建一个分页内容:1, 2,3等等。 p>

我怎么能这样做? p>

谢谢! p> div>

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! :)