Transfer Plugin Shortcode Function to Default Page Template with Pagination

Im using a plugin wich adds a like button to my site. The plugin includes the following shortcode function to list the 10 most liked posts.

My goals are:

  1. Show all posts – not just 10
  2. Display the posts in a default page template with pagination. (i already created the template by copy – paste and renamed the default one). Name of the new page template is _most_shared
  3. Display the posts in the same way as the default ones. Now it only shows a list with the like count. I also added the default page template.
    Unfortunately i dont know how to achieve this. I was not able to find a similar question and answer. Any help is appreciated.

Function in Plugin:

     /*--------------------------------------------*
     * Shortcode //dot_recommended_top_posts
     *--------------------------------------------*/

    function dot_recommended_top_posts( $atts, $content = null )
    {

        // define attributes and their defaults
        // get our variable from $atts
        $atts = shortcode_atts( array(
            'container' => 'li',
            'number' => '10',
            'post_type' => 'post',
            'year' => '',
            'monthnum' => '',
            'show_count' => '1',
        ), $atts );

        global $wpdb;

            // empty params array to hold params for prepared statement
            $params = array();

            // build query string
            $sql = "SELECT * FROM $wpdb->posts, $wpdb->postmeta WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id";

            // add year
            if( '' !== $atts['year'] ) {
                $sql .= ' AND YEAR(post_date) = %d';
                $params[] = $atts['year'];
            }

            // add monthnum
            if( '' !== $atts['monthnum'] ) {
                $sql .= ' AND MONTH(post_date) = %d';
                $params[] = $atts['monthnum'];
            }

            // add post WHERE
            $sql .= " AND post_status = 'publish' AND post_type = %s AND meta_key = '_recommended'";
            $params[] = $atts['post_type'];

            // add order by and limit
            $sql .= " ORDER BY {$wpdb->postmeta}.meta_value+0 DESC LIMIT %d";
            $params[] = $atts['number'];

            // prepare sql statement
            $query = $wpdb->prepare( $sql, $params );

            // execute query
            $posts = $wpdb->get_results( $query );

            $return = '';

        foreach ($posts as $item) {
            $post_title = stripslashes( $item->post_title );
            $permalink = get_permalink( $item->ID );
            $post_count = $item->meta_value;

            $return .= '<' . esc_html( $atts['container'] ) . '>';
            $return .= '<a href="' . esc_url( $permalink ) . '" title="' . esc_attr( $post_title ) .'" rel="nofollow">' . esc_html( $post_title ) . '</a> ';

            if ( $atts['show_count'] == '1') {
                $return .= '<span class="votes">' . esc_html( $post_count ) . '</span> ';
            }

            $return .= '</' . esc_html( $atts['container'] ) . '>';

        }
        return $return;

    }   //dot_recommended_top_posts

Shortcode for Text Editor:

    [dot_recommended_posts container='div' post_type='showcase' number='10' year='2013' monthnum='7']

Default page template with working pagination:

    <?php
        /*
        Template Name: _default
        */
    ?>

    <?php get_header(); ?>

    <div class="container">
        <div class="row subpage-title text-center">
            <h1><?php _e('Most-Shared Articles', 'test'); ?></h1>
        </div>
    </div>

    <div class="container-fluid">
        <?php
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

     // Filter/Function

        get_template_part('index', 'masonry');
        get_footer();
    ?>

Read more here: Transfer Plugin Shortcode Function to Default Page Template with Pagination

Leave a Reply

Your email address will not be published. Required fields are marked *