Skip to content

Instantly share code, notes, and snippets.

@zackkatz
Forked from Jeradin/pagination for GravityView
Last active January 27, 2017 19:05
Show Gist options
  • Save zackkatz/fadd382519af8b81d72a2bc0e4927868 to your computer and use it in GitHub Desktop.
Save zackkatz/fadd382519af8b81d72a2bc0e4927868 to your computer and use it in GitHub Desktop.
Entry Next / Prev pagination for GravityView (NOT PRODUCTION READY)
<?php
/**
* Get single entry pagination links
* to use: echo gravityview_entry_pagination();
*/
/**
* NOT FINAL CODE. Use at your own risk! This will be incorporated soon into core.
*
* This will use GravityView filters to generate the entry links.
*
* If the single entry is linked to with search parameters added, the entry search will be properly updated:
* ?gv_search=Example&mode=any will only link to entries with "Example" in them
*
* @return string If not single entry, empty string. Otherwise, HTML <ul> of next/prev links
*/
function gravityview_entry_pagination() {
/** @global GravityView_View $gravityview_view */
global $gravityview_view;
$single_entry_id = gravityview_is_single_entry();
if( ! $single_entry_id ) {
return '';
}
$args = $gravityview_view->getAtts();
$args['id'] = $gravityview_view->getViewId();
$form_id = $gravityview_view->getFormId();
$result = GravityView_frontend::get_view_entries( $args, $form_id );
//Using this below to get all entry IDs, I know there is a direct function for that, but wanted to preserve the Views sort order.
$entry_ids = wp_list_pluck( $result['entries'], 'id' );
$total_count = intval( $result['count'] );
$position = array_search( $single_entry_id, $entry_ids );
$prev_pos = ! rgblank( $position ) && $position > 0 ? $position - 1 : false;
$next_pos = ! rgblank( $position ) && $position < $total_count - 1 ? $position + 1 : false;
$post_id = $gravityview_view->getPostId();
$output = '<ul class="list-inline">';
$output .= '<li class="gf_entry_count">';
$output .= '<span>Entry <strong>'. ($position + 1) .'</strong> of <strong>'. $total_count .'</strong></span>';
$output .= '</li>';
if ( ! rgblank( $prev_pos ) ) {
$output .= '<li class="gf_entry_prev gv_pagination_links">';
$output .= '<a href="' . add_query_arg( $_GET, GravityView_API::entry_link( $entry_ids[$prev_pos], $post_id ) ) . '" class="' . ($prev_pos !== false ? ' ' : ' gf_entry_disabled') . '" title="Previous Entry"><i class="dashicons dashicons-arrow-left"></i> Previous</a></li>';
$output .= '</li>';
}
if ( ! rgblank( $next_pos ) ) {
$output .= '<li class="gf_entry_next gv_pagination_links">';
$output .= '<a href="' . add_query_arg( $_GET, GravityView_API::entry_link( $entry_ids[$next_pos], $post_id ) ) . '" class="' . ($next_pos !== false ? ' ' : ' gf_entry_disabled') . '" title="Next Entry">Next <i class="dashicons dashicons-arrow-right"></i></a></li>';
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment