[英]WordPress: get list of all custom post pages and add pagination
我有一個代碼,可從名為“ pavideo”的自定義帖子類型獲取所有縮略圖,並用作簡碼:
// 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');
我需要獲取4個縮略圖,然后創建諸如1,2,3之類的分頁對象。
我該怎么辦?
謝謝!
好,我知道了! :)
我使用了wp-pagenavi插件來顯示1,2,3之類的分頁等等。 我的代碼現在看起來像這樣:
// 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');
而且效果很好! :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.