Impaginazione non funziona con il ciclo personalizzato
-
16-10-2019 - |
Domanda
Ho un ciclo personalizzato che sto usando per visualizzare alcune Annunci Immobiliari che saranno disponibili entro 60 giorni. Sto chiamando con la seguente funzione:
<?php
$sixtydays = date('Y/m/d', strtotime('+60 days'));
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new PostsOrderedByMetaQuery(array(
'post_type' => array('post', 'real-estate'),
'meta_key' => 'Time Available',
'meta_compare' => '<=',
'meta_value' => $sixtydays,
'paged' => $paged,
'orderby_meta_key' => 'Price',
'orderby_order' => 'ASC'
));
?>
<?php while ($query->have_posts()) : $query->the_post(); ?>
Mentre il ciclo funziona alla grande, non posso farlo compagine. Essa mostra i primi 10 (default) i miei messaggi, ma non mostra l'impaginazione. L'unico modo per visualizzare tutti i messaggi è quello di mostrare loro su una pagina aggiungendo 'posts_per_page' => -1,
Ho cicli simili su altre pagine che non hanno alcun problema impaginazione. L'unica differenza con questo è che ci sono due meta chiavi che vengono filtrando i messaggi.
sto usando WP Pagina Navi per questo e il resto delle mie pagine. Sto chiudendo il ciclo e aggiungendo l'impaginazione utilizzando il seguente codice:
<?php endwhile; // End the loop. Whew. ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>
Come posso fare per correggere questo?
Soluzione
Ho eseguito in questo problema con PageNavi prima. La mia soluzione è quella di dirottare la variabile $ WP_Query temporaneamente e poi riassegnare dopo la chiusura del ciclo. Un exmaple:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'post_type'=>'post',
'cat' => 6,
'posts_per_page' => 5,
'paged'=>$paged
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
/* PageNavi at Top */
if (function_exists('wp_pagenavi')){wp_pagenavi();}
if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
/* DO STUFF IN THE LOOP */
endwhile; endif;
/* PageNavi at Bottom */
if (function_exists('wp_pagenavi')){wp_pagenavi();}
$wp_query = null;
$wp_query = $temp;
wp_reset_query(); ?>
L'ultimo passo è quello di riassegnare la variabile $ WP_Query a quello che è stato originariamente e quindi reimpostare la schiena query per iniziare.
* Modifica: * tag php fisso. Buon cecchino occhio.
Altri suggerimenti
Ho avuto un problema simile prima di oggi ...
Hai un tipo personalizzato postale e una pagina o un post con la stessa lumaca? Il significato è l'URL di una pagina avete / immobiliare e il post personalizzato tipo URL Rewrite a / immobiliare?
Se questo è il caso non è possibile avere 2 con lo stesso URL oppure wordpress si confonde.
Si sia possibile modificare l'URL o provare questo http://wordpress.org/support/topic/pagination-with-custom-post-type-listing?replies=23#post-1637753 . Ho scelto di cambiare il mio url, ma qualcuno ci ha scritto una query personalizzata per aggirare il problema
Sto usando questo per la numerazione personalizzato e il suo bel lavoro
//paginations for newsletter
//define in function file
function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default queries
* and custom queries.
*/
if ($paged == '') {
global $paged;
if (empty($paged)) {
$paged = 1;
}
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => false,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => true,
'prev_text' => __('◄'),
'next_text' => __('►'),
'type' => 'plain',
'add_args' => true,
'add_fragment' => '',
'after_page_number' => '',
'before_page_number' =>'',
);
$paginate_links = paginate_links($pagination_args);
if ( $paginate_links ) {
echo "<nav class='custom-pagination'>";
//echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}
?>
// Definire questo per qualsiasi modello come template newsletter
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$newslatter_detail = array(
'post_type' => 'newsletter',
'post_status' => 'publish',
'posts_per_page' =>4,
'order' => 'ASC',
//'orderby' =>'date',
'paged' => $paged
);
$posts = new WP_Query( $newslatter_detail );
$posts_array = get_posts( $newslatter_detail );
if ( $posts -> have_posts() ) {
while ( $posts->have_posts() ) : $posts->the_post();
the_title();
endwhile;
wp_reset_postdata();
} else { ?>
No Forum List found.
<?php } ?>
<div class="pagination">
<?php
if (function_exists(custom_pagination)) {
custom_pagination($posts->max_num_pages,"",$paged);
}
?>
</div>
Questa è la soluzione che ha funzionato per me, utilizzando parte del codice originale dal nurain e la risposta da Jan Fabry:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$myquery = new WP_Query(
array(
'posts_per_page' => '2',
'paged'=>$paged
// add any other parameters to your wp_query array
)
);
?>
<?php
if ($myquery->have_posts()) : while ($myquery->have_posts()) : $myquery->the_post();
?>
<!-- Start your post. Below an example: -->
<div class="article-box">
<h2 class="article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="article-excerpt"><?php echo(get_the_excerpt()); ?></p>
</div>
<!-- End of your post -->
<?php endwhile; ?>
<?php wp_pagenavi( array( 'query' => $myquery ) ); ?><!-- IMPORTANT: make sure to include an array with your previously declared query values in here -->
<?php wp_reset_query(); ?>
<?php else : ?>
<p>No posts found</p>
<?php endif; ?>
È possibile visualizzare ti tipo messaggio personalizzato utilizzando questo metodo la vostra impaginazione può lavorare !!!
<?php
query_posts( array( 'post_type' => 'post', 'posts_per_page' => '2', 'paged' => get_query_var( 'paged' ) ) );
if (have_posts() ) : while (have_posts() ) : the_post();
?>
<!-- Start your post. Below an example: -->
<div class="article-box">
<h2 class="article-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="article-excerpt"><?php echo(get_the_excerpt()); ?></p>
</div>
<!-- End of your post -->
<?php endwhile; ?>
<?php wp_pagenavi(); ?><!-- IMPORTANT: make sure to include an array with your previously declared query values in here -->
<?php wp_reset_query(); ?>
<?php else : ?>
<p>No posts found</p>
<?php endif; ?>