php


Wordpress Pagination with custom template


I'm developing a website with a custom template and I've been facing some problems when trying to paginate the website (149 pages with 10 posts each one). I'm using a page called template-tests.php in which the pagination links and numbers should be displayed and another page called theme-functions.php in which the pagination function should be written.
Here's what I've got:
1- theme-functions.php (function to paginate only):
function pagination_test() {
if( is_singular() )
return;
global $wp_query;
/** Stop execution if there's only 1 page */
if( $wp_query->max_num_pages <= 1 )
return;
$paged = get_query_var( 'page', 1 );
$max = intval( $wp_query->max_num_pages );
/** Add current page to the array */
if ( $paged >= 1 )
$links[] = $paged;
/** Add the pages around the current page to the array */
if ( $paged >= 3 ) {
$links[] = $paged - 1;
$links[] = $paged - 2;
}
if ( ( $paged + 2 ) <= $max ) {
$links[] = $paged + 2;
$links[] = $paged + 1;
}
echo '<div class="navigation"><ul>' . "\n";
/** Previous Post Link */
if ( get_previous_posts_link() )
printf( '<li>%s</li>' . "\n", get_previous_posts_link('<i class="fa fa-angle-left"></i> Previous') );
/** Link to first page, plus ellipses if necessary */
if ( ! in_array( 1, $links ) ) {
$class = 1 == $paged ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
if ( ! in_array( 2, $links ) )
echo '<li>…</li>';
}
/** Link to current page, plus 2 pages in either direction if necessary */
sort( $links );
foreach ( (array) $links as $link ) {
$class = $paged == $link ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
}
/** Link to last page, plus ellipses if necessary */
if ( ! in_array( $max, $links ) ) {
if ( ! in_array( $max - 1, $links ) )
echo '<li>…</li>' . "\n";
$class = $paged == $max ? ' class="active"' : '';
printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
}
/** Next Post Link */
if ( get_next_posts_link() )
printf( '<li>%s</li>' . "\n", get_next_posts_link('Next <i class="fa fa-angle-right"></i>') );
echo '</ul></div>' . "\n";
}
2- and here's the template-tests.php extract:
<main id="primary" class="site-main" role="main">
<?php if (have_posts()) : while (have_posts()) : the_post();
get_template_part('content', get_post_format());
endwhile;
?>
<?php if( '' !== $post->post_content ): ?>
<?php zilla_page_before(); ?>
<!--BEGIN .page-->
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php zilla_page_start(); ?>
<!--BEGIN .entry-content -->
<div class="entry-content">
<?php the_content(__('Read more...', 'zilla')); ?>
<?php wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'zilla').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<!--END .entry-content -->
</div>
<?php zilla_page_end(); ?>
<!--END .page-->
</article>
<?php zilla_page_after(); ?>
<?php endif; ?>
<?php endwhile; endif; ?>
<section class="featured content">
<?php zilla_print_home_featured_post(); ?>
</section>
<section class="recent content <?php echo zilla_get_mod_state('homepage_recent_layout') ? esc_attr( zilla_get_mod('homepage_recent_layout') ) : 'grid'; ?>">
<div class="container">
<div class="col-md-8">
<?php zilla_print_home_recent_posts(); ?>
</div>
<div id="affix-home" class="text-right col-md-4 hidden-xs hidden-sm affix">
<br />
<?php /* Widgetised Area */
if( is_active_sidebar( 'sidebar' ) )
dynamic_sidebar( 'sidebar' ); ?>
</div>
<div class="col-md-9 pagination">
<?php pagination_test(); ?>
</div>
</div>
</section>
<!--END #primary .site-main-->
</main>
I've read a million forums and topics on StackOverflow but nothing seems to solve my problem.
When I open the page on the browser the pagination links are shown correctly but after clicking on "next" for the first time, the loop seems to stop. So after the first loop everytime I click "next" it takes me to the SECOND page, which is the most strange error.
Edit #1: the links containing the pages are updating correctly but the page content is always the one from the first page. For example: if I click "next" the link is updated to "site/page/2"..."site/page/3"... but the content stays the same.

Related Links

mysqli_query not working in php despite correctly built query
How to get all coordinates of single country or state rather than only bounds?
My form in phpmailer() only allow send mails from a mail with own domain
How can I fix the HTTP Error 417 Expectation failed on Laravel's Homestead?
How to print a colorful echo with $_SESSION['username'] [closed]
Find first src in php string
Getting a id back from a datalist option form using php post.
PHP export csv file
loop html statements using php [closed]
Laravel Queue with Supervisor, running but not processing jobs
Get nth day of each month between two dates [duplicate]
Syntax error, unexpected '', expecting 'function' [duplicate]
Ajax is driving me nuts. Why is Jquery post method failing in this specific case?
Joomla, create custom php page that list some results
How can I send an ajax request to controller backend php file from tpl file in CS cart?
PHP Pthreads. “Class Threaded not found”

Categories

HOME
python-2.7
artificial-intelligence
magento2
matrix
windows-8.1
angular-ui-grid
eclipse-plugin
concurrency
devexpress
openflow
haxe
gprs
firebase-database
minecraft
pyresttest
tfs2010
polymer-2.x
reportviewer
slide
cname
freertos
corona
uima
web-deployment
playframework-2.0
pickle
spam
psql
spyder
mime-types
environment
pubxml
web-config-transform
password-generator
xdebug
react-bootstrap-table
bobo-browse.net
sparkle
sales
bitbake
javax.persistence
mdns
snap-framework
rust-cargo
fotorama
john-the-ripper
universal
winrm
polyml
spinner
xvfb
macports
unification
cheat-engine
windows-store
export-to-pdf
right-click
hypothesis-test
oracle-bmcs
doctrine-extensions
nuclio
jenkins-slave
sony-future-lab-n
ilrepack
datediff
maven-jaxb2-plugin
simple-schema
htmlspecialchars
mongodb-aggregation
predicates
adobe-reader
openerp-6
pbx
gapi
streamwriter
logical
ndk-build
mathjs
svnserve
between
dojo-build
node-glob
sharepoint-apps
django-redis
okta-api
arbre
gluon-desktop
featuretoggle
supersocket.net
algebraixlib
scala-swing
python-curses
nsmutabledata
htmltidy
sunos
famo.us
encryption-symmetric
tabris
storing-data
accessory
network-printers
application-blocks
mousemotionlistener
google-code-prettify
void-pointers
flv
ckeditor.net
spark-view-engine
audiotoolbox
yuidoc
mvcrecaptcha
filelock
winsxs
airprint
anonymous-types
turbine
android-2.1-eclair
resharper-5.1
android-input-method
self-tracking-entities
rational-unified-process
mysqli-multi-query
yahoo-maps
rendering-engine

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile