Как указано в предыдущих ответах, вы можете использовать WP_Query
, чтобы создать собственный запрос для сообщений, пользовательских типов сообщений (CPT) и страниц:
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
// other args here
) );
И использовать Цикл для отображения сообщений:
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//
// Post Content here
//
} // end while
} // end if
Теперь имеется в виду следующее:
Частично работающий код запроса WP
Вот мой сайт, где этот код появится, но, кажется, он не работает
Я думаю, вы хотели сказать, что "1022 * нумерация страниц не работает", верно?Потому что это не так:
Поскольку вы используете глобальный $wp_query
объект с нумерацией страниц.
В вашей конструкции WP_Query
,Вы не установили параметр paged
, который необходим для правильной работы нумерации страниц.
Итак, вот как это должно быть:
$current_page = max( 1, get_query_var( 'paged' ) ); // the current page
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $current_page,
// other args here
) );
И затемиспользуйте $current_page
с paginate_links()
- вы также можете увидеть здесь, я использовал $the_query
, а не $wp_query
при получении / указании максимального количества страниц:
echo paginate_links( array(
'current' => $current_page,
'total' => $the_query->max_num_pages, // here I don't use $wp_query
// other args here
) );
А ниже приведен рабочий код, который вы можете использовать вместо своего частично рабочего кода (код между <!-- START of WP Query -->
и <!-- END of WP Query -->
):
<?php
$current_page = max( 1, get_query_var( 'paged' ) );
$the_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $current_page,
// other args here
) );
if ( $the_query->have_posts() ) :
$count = 1;
while ( $the_query->have_posts() ) : $the_query->the_post();
if ( 1 === $count ) :
?>
<div class="item item1" style="background: red; color: #fff;">
<span>Post 1</span> <?php the_title( '<h3>', '</h3>' ); ?>
</div>
<?php
elseif ( 2 === $count ) :
?>
<div class="item item2" style="background: orange; color: #fff;">
<span>Post 2</span> <?php the_title( '<h3>', '</h3>' ); ?>
</div>
<?php
// other conditions here
else :
?>
<div class="item item3" style="background: yellow; color: #666;">
<span>Post <?php echo $count; ?></span>
<?php the_title( '<h3>', '</h3>' ); ?>
</div>
<?php
endif;
$count++;
endwhile;
?>
<p>Pagination:</p>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'total' => $the_query->max_num_pages,
'type' => 'list',
'end_size' => 3,
) );
?>
<?php else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
wp_reset_postdata();
?>