Create wp_query() Custom Shortcode with Parameters For Showing Post


/**
 *  wp_query shortcode Shortcode    [wp-post showpost="2" post_type="post" pagination="true" include_excerpt='true' ]
 */




function wp_query_shortcode($atts, $content = null)
        {
        extract(shortcode_atts(array(
                'showpost' => '-1',
                'post_type' => 'post',
                'pagination' => '',
                'include_excerpt' => ''
        ) , $atts));
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $args = array(
                'post_type' => $post_type,
                'paged' => $paged,
                'order' => 'ASC',
                'orderby' => 'date',
                'posts_per_page' => $showpost,
        );
        $query = new WP_Query($args);
        if ($query->have_posts())
        while ($query->have_posts()):
                $query->the_post();
                $content.= '<article class="' . $post_type . ' status-publish hentry">';
                $content.= '<h3><a href=' . get_permalink() . '>' . get_the_title() . '</a></h3>';
                if ($include_excerpt == 'true')
                        {
                        $content.= '<div class="entry-content">' . do_shortcode(get_the_excerpt()) . '</div>';
                        }
                  else
                        {
                        $content.= '<div class="entry-content">' . do_shortcode(get_the_content()) . '</div>';
                        }

                $content.= '</article>';
        endwhile; // end of the loop.
        /* Pagination  Code Start  */
        if ($pagination == 'true'):
                global $wp_query;
                $big = 999999999; // need an unlikely integer
                $totalpages = $query->max_num_pages;
                $current = max(1, get_query_var('paged'));
                $paginate_args = array(
                        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))) ,
                        'format' => '?paged=%#%',
                        'current' => $current,
                        'total' => $totalpages,
                        'show_all' => False,
                        'end_size' => 1,
                        'mid_size' => 3,
                        'prev_next' => True,
                        'prev_text' => __('« Previous') ,
                        'next_text' => __('Next »') ,
                        'type' => 'plain',
                        'add_args' => False,
                        'add_fragment' => '',
                        'before_page_number' => '',
                        'after_page_number' => ''
                );
                $pagination = paginate_links($paginate_args);
                $content.= "<nav class='pagination'>";
                $content.= "<span class='page-numbers page-no'>Page " . $paged . " of " . $totalpages . "</span> ";
                $content.= $pagination;
                $content.= "</nav>";
        endif;
        wp_reset_postdata();
        return $content;
        }

add_shortcode('wp-post', 'wp_query_shortcode');

source:http://help4cms.com/create-wp_query-custom-shortcode-with-parameters-for-showing-post/

Comments

Popular posts from this blog

Mengatasi Pesan Error PHP pada XAMPP versi terbaru

Membuat Fitur Pencarian Data Berbasis Ajax Menggunakan PHP, jQuery dan MySql

Bedanya INNER, CROSS, LEFT, RIGHT, dan FULL JOIN di SQL