Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Patrick Jackson

    (@pjackson1972)

    Hi sweettea2,

    Unfortunately, I don’t know of an elegant solutions to this. I’d be interested to hear other ideas.

    The only thing I can think of offhand would be to create your own custom plugin from the original so that when updates are published they won’t overwrite your customizations.

    To make the plugin custom, you’ll need to edit the header information located in the posts_in_page.php file. That header looks like the following.

    /**
     *  Plugin Name: Posts in Page
     *  Plugin URI: https://www.ivycat.com/wordpress/wordpress-plugins/posts-in-page/
     *  Description: Easily add one or more posts to any page using simple shortcodes. Supports categories, tags, custom post types, custom taxonomies, and more.
     *  Version: 1.3.0
     *  Author: IvyCat, Inc.
     *  Author URI: https://www.ivycat.com
     *  License: GNU General Public License v2.0
     *  License URI: https://www.gnu.org/licenses/gpl-2.0.html
     *  Text Domain: posts-in-page
     *  Domain Path: /languages
     *
     * @package PostsInPage
     * @author Eric Amundson <eric@ivycat.com>
     * @copyright Copyright (c) 2014, IvyCat, Inc.
     * @license https://www.gnu.org/licenses/gpl-2.0.html
     */

    Basically, you just need to change the name of the plugin. You’ll need to change the first line that says Plugin Name to something like “My Posts in Page“. Optionally, you can edit the other fields, such as author, etc.

    After that, change the name of that file to my-posts-in-page.php, and change the name of the directory to my-posts-in-page as well.

    When you return to your list of plugins on the back end of your site, you should see your custom plugin listed and deactivated. Activate your plugin, and it should work like the old one did.

    Your custom plugin should no longer be tied to the original on the www.remarpro.com site and you should be able to make any changes you like without them being wiped out by updates.

    If you’re familiar with Git, we have a github repository you can fork: https://github.com/ivycat/Posts-in-Page.

    I hope this helps!

    Thread Starter sweettea2

    (@sweettea2)

    Thanks much for the help! I don’t really understand how to extend plugins with custom functions and am still very interested in learning how to do that with plugins such as yours! I ended up doing something different, though, for the job I was working on. Here’s what I did.

    Installed the “Insert PHP Code Snippet” plugin. (I also had the WP PageNavi plugin installed.) Created new snippets for each category that I wanted a corresponding page for. Put something like the following in each snippet…

    $paged = get_query_var('paged') ? get_query_var('paged') : 1; $query = new WP_Query( array('cat' => '2,5,9,12', 'paged' => $paged, 'post_per_page' => 10) );
    if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
     get_template_part( 'content', get_post_format() );
    endwhile; else:
     _e('Sorry, no posts matched your criteria.');
    endif;
    wp_pagenavi(array( 'query' => $query ));

    I also created a custom content template and used that to display only the excerpt of the posts.

    Here’s how you do this, without patching the plugin’s files.

    Just add this to your theme’s functions.php:

    class ICPagePosts_Paginate_With_Numbers {
    
    	public function __construct( ) {
    		add_filter('posts_in_page_results', array( &$this, 'get_wp_query' ));
    		add_filter('posts_in_page_paginate', array( &$this, 'paginate_links' ));
    	}
    
    	public function get_wp_query($posts){
    		$this->posts = $posts;
    		return $posts;
    	}
    
    	public function paginate_links($html){
    		global $wp_query;
    		$posts = $this->posts;
    		$page_url = home_url( '/' . $wp_query->post->post_name . '/' );
    		$page = isset( $_GET['page'] ) ? $_GET['page'] : 1;
    		$total_pages = $posts->max_num_pages;
    		$per_page = $posts->query_vars['posts_per_page'];
    		$curr_page = ( isset( $posts->query_vars['paged'] ) && $posts->query_vars['paged'] > 0	) ? $posts->query_vars['paged'] : 1;
    		$prev = ( $curr_page && $curr_page > 1 ) ? '<li><a href="'.$page_url.'?page='. ( $curr_page-1 ).'">Previous</a></li>' : '';
    		$next = ( $curr_page && $curr_page < $total_pages ) ? '<li><a href="'.$page_url.'?page='. ( $curr_page+1 ).'">Next</a></li>' : '';
    		$numbers = '';
    		for ($i = 1; $i < $total_pages+1; $i++){
    			if ($i == $curr_page) {
    				$numbers .= '<li class="current">' . $i . '</li>';
    			} else {
    			 	$numbers .= '<li><a href="'.$page_url.'?page='. $i .'">' . $i . '</a></li>';
    			}
    		}
    		return '<ul>' . $prev . $numbers . $next . '</ul>';
    	}
    }
    new ICPagePosts_Paginate_With_Numbers();

    Note that there’s a class “current” so you can add some CSS to the current page’s number.

    Shannon86

    (@shannon86)

    That works fantastically Manuel, thanks! The only thing is that lists EVERY page number, so it isn’t exactly practical for a large blog. Any way to only list 1,2,3 and the last three numbers of pages?

    For example:

    Prev < 1 < 2 < 3 | Current Page # | 300 > 301 > 302 > Next

    Updated snippet to get 1,2,3 pagination when using this shortcode in a category or tag archive template.

    /*
     * Add pagination with 1,2,3 numbers to the "Posts In Page" plugin.
     */
    class ICPagePosts_Paginate_With_Numbers {
    
    	public function __construct( ) {
    		add_filter('posts_in_page_results', array( &$this, 'get_wp_query' ));
    		add_filter('posts_in_page_paginate', array( &$this, 'paginate_links' ));
    	}
    
    	public function get_wp_query($posts){
    		$this->posts = $posts;
    		return $posts;
    	}
    
    	public function paginate_links($html){
    		$obj = get_queried_object();
    		$posts = $this->posts;
    
    		if (is_archive() || is_tax()) {
    			if ($obj->taxonomy == 'tag'){ $obj->taxonomy = 'post_tag'; }
    			$page_url = get_term_link($obj);
    		} elseif(is_post_type_archive() ) {
    			$page_url = get_post_type_archive_link( get_query_var('post_type') );
    		} else {
    			$page_url = get_permalink( $obj->ID );
    		}
    
    		$page = isset( $_GET['page'] ) ? $_GET['page'] : 1;
    		$total_pages = $posts->max_num_pages;
    		$per_page = $posts->query_vars['posts_per_page'];
    		$curr_page = ( isset( $posts->query_vars['paged'] ) && $posts->query_vars['paged'] > 0	) ? $posts->query_vars['paged'] : 1;
    		$prev = ( $curr_page && $curr_page > 1 ) ? '<li><a href="'.$page_url.'?page='. ( $curr_page-1 ).'">Previous</a></li>' : '';
    		$next = ( $curr_page && $curr_page < $total_pages ) ? '<li><a href="'.$page_url.'?page='. ( $curr_page+1 ).'">Next</a></li>' : '';
    		$numbers = '';
    		for ($i = 1; $i < $total_pages+1; $i++){
    			if ($i == $curr_page) {
    				$numbers .= '<li class="current">' . $i . '</li>';
    			} else {
    			 	$numbers .= '<li><a href="'.$page_url.'?page='. $i .'">' . $i . '</a></li>';
    			}
    		}
    		return '<ul>' . $prev . $numbers . $next . '</ul>';
    	}
    }
    new ICPagePosts_Paginate_With_Numbers();

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘1,2,3 Numbered Pagination’ is closed to new replies.