Adding pagination to the Single Entry page
GravityView’s Single Entry page has no links to the previous or next entry. The snippet on this page adds them below each entry, with a counter such as “Entry 4 of 8”.
Staff who review entries one at a time, such as job applications or graded submissions, can then move through the list without going back to it.
Note: Entries opened in a lightbox already have arrows to the previous and next entry. See Opening and editing entry details in a lightbox.
What the snippet adds #
Below the entry, the snippet shows:
- a Previous link to the entry before it
- a Next link to the entry after it
- a counter with the entry’s place in the list, such as “Entry 4 of 8”
The links follow the View, so they go through the same entries in the same order as its list:
- the order set by Sort by field and Sort direction on the View’s Filter & Sort tab
- the conditions in Advanced Filter on the same tab
- Show only approved entries on the View Settings tab, which leaves out entries that are not approved
Entries in the trash are always left out. The first entry has no Previous link, and the last has no Next link. When the View is embedded on a page, the links open the entries on that page.
The snippet loads up to 5,000 of the View’s entries each time a Single Entry page opens. An entry further down the list than that shows no links.
Add the snippet #
Add this code to your site:
<?php
/**
* GravityView Single Entry Navigation
*
* Adds "Entry X of Y" with Previous/Next links on single entry views.
* Respects View sorting, approval status, active entry filters, and Advanced Filter conditions.
*
* Works with both CPT Views and shortcode-embedded Views.
*
* Usage: add this to your theme's functions.php or as a code snippet plugin.
* The navigation will automatically appear at the bottom of single entry views.
*/
/**
* Returns the Previous and Next links and the entry counter for one entry.
*
* @param \GV\Template_Context $context The template context provided by GravityView.
*
* @return string HTML output with entry count and navigation links.
*/
function gv_single_entry_navigation( $context ) {
$view = $context->view;
$entry = $context->entry;
if ( ! $view instanceof \GV\View || ! $view->form || ! $entry ) {
return '';
}
// Override paging to retrieve ALL entries (not just one page).
// Uses the View's full entry pipeline, which fires `gravityview/view/query`
// so Advanced Filter, approval status, sorting, etc. are all applied.
$remove_limit = function ( &$query ) {
$query->limit( 5000 )->offset( 0 );
};
add_action( 'gravityview/view/query', $remove_limit, 9999 );
$entries = $view->get_entries( gravityview()->request );
remove_action( 'gravityview/view/query', $remove_limit, 9999 );
$all_entries = $entries->all();
if ( empty( $all_entries ) ) {
return '';
}
// Find the current entry position in the sorted, filtered list.
$current_id = $entry->ID;
$total_count = count( $all_entries );
$position = false;
foreach ( $all_entries as $index => $gv_entry ) {
if ( (string) $gv_entry->ID === (string) $current_id ) {
$position = $index;
break;
}
}
if ( false === $position ) {
return '';
}
$prev_pos = $position > 0 ? $position - 1 : false;
$next_pos = $position < $total_count - 1 ? $position + 1 : false;
$output = '<div class="gv-entry-navigation" style="margin: 1em 0; display: flex; align-items: center; gap: 1em;">';
if ( false !== $prev_pos ) {
$prev_link = GravityView_API::entry_link( $all_entries[ $prev_pos ]->as_entry() );
$output .= '<a class="gv-entry-prev" href="' . esc_url( $prev_link ) . '">← Previous</a>';
}
$output .= '<span class="gv-entry-count">Entry <strong>' . ( $position + 1 ) . '</strong> of <strong>' . $total_count . '</strong></span>';
if ( false !== $next_pos ) {
$next_link = GravityView_API::entry_link( $all_entries[ $next_pos ]->as_entry() );
$output .= '<a class="gv-entry-next" href="' . esc_url( $next_link ) . '">Next →</a>';
}
$output .= '</div>';
return $output;
}
// Output navigation at the bottom of single entry views.
add_action( 'gravityview/template/after', function ( $context ) {
if ( ! $context instanceof \GV\Template_Context ) {
return;
}
if ( ! $context->entry ) {
return;
}
echo gv_single_entry_navigation( $context );
} );Note: Where to put code samples explains how to add this code to your site.
Then open any entry of the View. The links and the counter appear below it.
Change where or how it shows #
To show the navigation above the entry instead of below it, change gravityview/template/after to gravityview/template/before near the end of the snippet.
To change the labels or the style, edit the HTML that the gv_single_entry_navigation() function builds.
The links don’t appear #
- The links show only on the Single Entry page.
- Check that the snippet is active on your site.
- A View with one entry shows the counter alone, since there is no other entry to link to.