su_shortcode_display_posts_prevnext_pagination_link

The shortcodes-ultimate su shortcode display posts prevnext pagination link function.

Published Date - February 15, 2021

Description

su_shortcode_display_posts_prevnext_pagination_link( $direction );

Parameters

  1. $direction


Usage

The following example is for adding a hook callback.

if ( !function_exists( 'su_shortcode_display_posts_prevnext_pagination_link' ) ) {
    require_once ABSPATH . PLUGINDIR . 'shortcodes-ultimateshortcodes-ultimate/includes/shortcodes/display-posts.php';
}

// The input(s). 
$direction = 'next';

// NOTICE! Understand what this does before running.
$result = su_shortcode_display_posts_prevnext_pagination_link( $direction );
            

Defined

The function is defined in the following location(s).

shortcodes-ultimate/includes/shortcodes/display-posts.php

function su_shortcode_display_posts_prevnext_pagination_link( $direction = 'next' ) {

	$su_query  = su_shortcode_display_posts_store( 'get', 'su_query' );
	$atts      = su_shortcode_display_posts_store( 'get', 'atts' );
	$direction = sanitize_key( $direction );
	$label     = $atts[ 'pagination_' . $direction ];
	$class     = 'su-display-posts-pagination-' . $direction;
	$key       = su_shortcode_display_posts_get_pagination_key( $atts['id'] );
	$current   = su_shortcode_display_posts_get_current_page( $atts['id'] );
	$total     = $su_query->max_num_pages;
	$template  = apply_filters(
		'su/shortcode/display_posts/prevnext_pagination_link_template',
		'<a href="%1$s" class="%2$s">%3$s</a>',
		$atts,
		$direction
	);

	if ( $current > $total ) {
		return;
	}

	if ( 'next' === $direction && $current < $total ) {
		$goto = $current + 1;
	}

	if ( 'prev' === $direction && $current > 1 ) {
		$goto = $current - 1;
	}

	if ( ! isset( $goto ) ) {
		return;
	}

	$url = 1 === $goto
		? remove_query_arg( $key )
		: add_query_arg( $key, $goto );

	if ( 'yes' === $atts['pagination_anchor'] ) {
		$url .= '#' . $atts['id'];
	}

	return sprintf(
		$template,
		esc_attr( $url ),
		sanitize_html_class( $class ),
		esc_html( $label )
	);

}