-
-
Save croemmich/5830094 to your computer and use it in GitHub Desktop.
Foundation 4 Pagination for Wordpress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foundation_pagination($arrows = true, $ends = true, $pages = 2) | |
{ | |
if (is_singular()) return; | |
global $wp_query, $paged; | |
$pagination = ''; | |
$max_page = $wp_query->max_num_pages; | |
if ($max_page == 1) return; | |
if (empty($paged)) $paged = 1; | |
if ($arrows) $pagination .= foundation_pagination_link($paged - 1, 'arrow' . (($paged <= 1) ? ' unavailable' : ''), '«', 'Previous Page'); | |
if ($ends && $paged > $pages + 1) $pagination .= foundation_pagination_link(1); | |
if ($ends && $paged > $pages + 2) $pagination .= foundation_pagination_link(1, 'unavailable', '…'); | |
for ($i = $paged - $pages; $i <= $paged + $pages; $i++) { | |
if ($i > 0 && $i <= $max_page) | |
$pagination .= foundation_pagination_link($i, ($i == $paged) ? 'current' : ''); | |
} | |
if ($ends && $paged < $max_page - $pages - 1) $pagination .= foundation_pagination_link($max_page, 'unavailable', '…'); | |
if ($ends && $paged < $max_page - $pages) $pagination .= foundation_pagination_link($max_page); | |
if ($arrows) $pagination .= foundation_pagination_link($paged + 1, 'arrow' . (($paged >= $max_page) ? ' unavailable' : ''), '»', 'Next Page'); | |
$pagination = '<ul class="pagination">' . $pagination . '</ul>'; | |
$pagination = '<div class="pagination-centered">' . $pagination . '</div>'; | |
echo $pagination; | |
} | |
function foundation_pagination_link($page, $class = '', $content = '', $title = '') | |
{ | |
$id = sanitize_title_with_dashes('pagination-page-' . $page . ' ' . $class); | |
$href = (strrpos($class, 'unavailable') === false && strrpos($class, 'current') === false) ? get_pagenum_link($page) : "#$id"; | |
$class = empty($class) ? $class : " class=\"$class\""; | |
$content = !empty($content) ? $content : $page; | |
$title = !empty($title) ? $title : 'Page ' . $page; | |
return "<li$class><a id=\"$id\" href=\"$href\" title=\"$title\">$content</a></li>\n"; | |
} |
Thanks for the code!
But I have a bit of a problem here - when I click on page 2, the pagination disappears completely! I am using the pagination on a custom post type archive page.
This is my query in the template file:
<?php
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args = array(
'post_type' => 'job_listing',
'paged' => $paged,
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC'
);
$job_posts = new WP_Query($args);
?>
<?php if($job_posts->have_posts()) : ?>
<?php while($job_posts->have_posts()) : $job_posts->the_post() ?>
<?php endwhile ?>
<?php endif ?>
<?php foundation_pagination(); ?>
<?php wp_reset_postdata(); ?>```
Thanks!
I added some accessibility markup from Foundation 6 and changed the containing div
to a nav
element.
...Not sure how to remove title attribute from .unavailable a
tags?
I changed it to "ellipsis" for now, but i would rather have no title at all.
function foundation_pagination($arrows = true, $ends = true, $pages = 2)
{
if (is_singular()) return;
global $wp_query, $paged;
$pagination = '';
$max_page = $wp_query->max_num_pages;
if ($max_page == 1) return;
if (empty($paged)) $paged = 1;
if ($arrows) $pagination .= foundation_pagination_link($paged - 1, 'arrow' . (($paged <= 1) ? ' unavailable' : ''), '« Previous', 'Previous Page', (($paged <= 1) ? 'true' : ''));
if ($ends && $paged > $pages + 1) $pagination .= foundation_pagination_link(1);
if ($ends && $paged > $pages + 2) $pagination .= foundation_pagination_link(1, 'unavailable', '…', 'ellipsis', 'true');
for ($i = $paged - $pages; $i <= $paged + $pages; $i++) {
if ($i > 0 && $i <= $max_page)
$pagination .= foundation_pagination_link($i, ($i == $paged) ? 'current' : '');
}
if ($ends && $paged < $max_page - $pages - 1) $pagination .= foundation_pagination_link($max_page, 'unavailable', '…', 'ellipsis','true');
if ($ends && $paged < $max_page - $pages) $pagination .= foundation_pagination_link($max_page);
if ($arrows) $pagination .= foundation_pagination_link($paged + 1, 'arrow' . (($paged >= $max_page) ? ' unavailable' : ''), 'Next »', 'Next Page', (($paged >= $max_page) ? 'true' : ''));
$pagination = '<ul class="pagination" role="menubar" aria-label="Pagination">' . $pagination . '</ul>';
$pagination = '<nav class="pagination-centered">' . $pagination . '</nav>';
echo $pagination;
}
function foundation_pagination_link($page, $class = '', $content = '', $title = '', $aria = '')
{
$href = (strrpos($class, 'unavailable') === false && strrpos($class, 'current') === false) ? get_pagenum_link($page) : "#$id";
$aria = empty($aria) ? $aria : " aria-disabled=\"$aria\"";
$class = empty($class) ? $class : " class=\"$class\"";
$content = !empty($content) ? $content : $page;
$title = !empty($title) ? $title : 'Page ' . $page;
return "<li $class $aria><a href=\"$href\" title=\"$title\">$content</a></li>\n";
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! works like charm!