0
我有我的靜態主頁上的2 WP_Query
循環與分頁。分頁適用於兩個循環,但是當我轉到第一個循環的第二個頁面時,我的第二個循環也會轉到第二個頁面。我可以分開兩個循環的分頁嗎?WordPress:分頁靜態頁面上的多個循環
我已經使用page
參數,而不是paged
,因爲我在靜態主頁上。
第二個問題是,我添加了一個id到兩個部分作爲分頁時的錨點,但這似乎也不起作用。
首先查詢:
<section class="section" id="event">
<div class="container">
<div class="row">
<div class="col-xs-12 text-center">
<h2>Events</h2>
<p class="lead">Some of our future events</p>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<?php
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array (
'post_type' => 'events',
'posts_per_page' => 3,
'meta_key' => 'datum',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'paged' => $paged,
);
$the_query = new WP_Query($args); ?>
<?php if ($the_query->have_posts()) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<div class="event">
<div class="row">
<div class="col-sm-6 col-md-4">
<?php if (has_post_thumbnail()): ?>
<?php the_post_thumbnail('event-img'); ?>
<?php endif; ?>
</div>
<div class="col-sm-6 col-md-8 event-body">
<h4><?php the_title(); ?></h4>
<p class="lead"><?php the_field('datum') ?> @<?php the_field('locatie') ?></p>
<p><?php the_content(); ?></p>
</div>
</div>
</div>
<?php endwhile; ?>
<!-- end of the loop -->
</div>
<div class="col-xs-12">
<nav class="text-center">
<ul class="pagination">
<li>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%#event',
'current' => max(1, get_query_var('page')),
'total' => $the_query->max_num_pages,
'prev_text' => __('« '),
'next_text' => __(' »'),
));
?>
</li>
</ul>
</nav>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Sorry, Geen evenementen gevonden!.'); ?></p>
<?php endif; ?>
</div>
</div>
</section>
第二個查詢
<section class="section" id="gallery">
<div class="container">
<div class="row">
<div class="col-xs-12 text-center">
<h2>Gallery</h2>
<p class="lead">Some of our past events</p>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<?php
$paged_gallery = (get_query_var('page')) ? get_query_var('page') : 1;
$gallery_args = array (
'post_type' => 'gallerij',
'posts_per_page' => 6,
'paged' => $paged_gallery,
);
$the_query_gallery = new WP_Query($gallery_args); ?>
<?php if ($the_query_gallery->have_posts()) : ?>
<div class="row">
<!-- pagination here -->
<!-- the loop -->
<?php while ($the_query_gallery->have_posts()) : $the_query_gallery->the_post(); ?>
<div class="col-sm-6 col-md-4 album">
<div class="cover-text">
<a href="<?php (has_post_thumbnail()) ? the_post_thumbnail_url() : "" ?>" data-lightbox="<?php the_ID(); ?>">
<?php if (has_post_thumbnail()): ?>
<?php the_post_thumbnail('gallery-img', array('class' => 'img-rounded')); ?>
<?php endif; ?>
</a>
<h3 class="album-title"><?php the_title(); ?></h3>
</div>
<div class="gallery-container">
<?php
$images = get_field('album');
if($images): ?>
<?php foreach($images as $image): ?>
<a href="<?php echo $image['url']; ?>" data-lightbox="<?php the_ID(); ?>"></a>
<?php endforeach; ?>
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
</div>
</div>
<div class="col-xs-12">
<nav class="text-center">
<ul class="pagination">
<li>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%#gallery',
'current' => max(1, get_query_var('page')),
'total' => $the_query_gallery->max_num_pages,
'prev_text' => __('« '),
'next_text' => __(' »'),
));
?>
</li>
</ul>
</nav>
</div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Sorry, Geen albums gevonden!.'); ?></p>
<?php endif; ?>
</div>
</div>
</section>
更新 發現這個話題:https://wordpress.stackexchange.com/questions/47259/multiple-wp-query-loops-with-pagination
這也正是我的問題,BU我應用瞭解決方案,現在看來paginate_links()忽略了我的格式。我將解決方案更新爲'page'而不是'paged',因爲我使用的是靜態首頁。
這是否意味着我應該只改變這一行:'$ paged_gallery =(get_query_var('page'))? get_query_var('page'):1;'定製解決方案還是我需要重寫整個'paginate_links()'功能? – Christophvh
只需將其更改爲 $ paged_gallery = isset($ _ GET ['gallery_page'])? (int)$ _GET ['gallery_page']:1; 然後使用url更改值,例如。 something.com?gallery_page=2 – damlys