简体   繁体   中英

Create pagination of custom post type in WordPress

I have found some solution on SO and some other site but my issue does not solve those way.Please anyone tell me where is the problem? I get pagination but do not get custom post (gallery section) content when i click on second page.What i have tried so far:

custom post (gallery) creation:

function jellythemes_media_gallery()  {
  $labels = array(
    'name' => __('Gallery', 'framework'),
    'singular_name' => __('Gallery', 'framework'),
    'add_new' => __('Add media', 'framework'),
    'add_new_item' => __('Add media', 'framework'),
    'edit_item' => __('Edit media', 'framework'),
    'new_item' => __('New media', 'framework'),
    'view_item' => __('View media', 'framework'),
    'search_items' => __('Search media', 'framework'),
    'not_found' =>  __('No media found', 'framework'),
    'not_found_in_trash' => __('No media found in Trash', 'framework'),
    'parent_item_colon' => ''
  );

  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => true,
    'show_in_nav_menus' => false,
    'capability_type' => 'post',
    'hierarchical' => false,
    'exclude_from_search' => true,
    'menu_position' => 5,
    'supports' => array('title')
  );
  register_post_type('media',$args);
}

and query :

function jellythemes_gallery($atts, $content=null) {
    extract( shortcode_atts( array(
        'limit' => -1
        ), $atts ) );
    global $post;
    $back=$post; //Backup post data
    $return = '<div id="portfolio">
                <div class="section portfoliocontent">
                    <section id="i-portfolio" class="clear">';

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;       
    $media = new WP_Query(array('post_type'=>'media', 'posts_per_page' => esc_attr($limit),'paged' => $paged,));
    if($media->have_posts()) :
    while ($media->have_posts()) : $media->the_post();
        $image = get_post_meta( $post->ID, '_jellythemes_media_photo', true );
        $video = get_post_meta( $post->ID, '_jellythemes_media_video', true );
        $img = wp_get_attachment_image_src($image, 'media_thumb');
        $return .= '<div class="ch-grid element">
                        <img class="ch-item" src="' . $img[0] .'" alt="' . get_the_title() . '" />';
        if (empty($video)) {
            $return .= '<a class="fancybox img-lightbox" rel="" href="' . $img[0] .'">';
        } else {
            $return .= '<a class="fancybox-media" rel="" href="' . $video.'">';
        }
        $return .= '    <div>
                                <span class="p-category ' . (empty($video) ? 'photo' : 'video') . '"></span>
                            </div>
                        </a>
                    </div>';

    endwhile;
    $total_pages = $media->max_num_pages;
    if ($total_pages > 1){

        $current_page = max(1, get_query_var('paged'));

        echo paginate_links(array(
            'base' => get_pagenum_link(1) . '%_%',
            'format' => '/page/%#%',
            'current' => $current_page,
            'total' => $total_pages,
            'prev_text'    => __('« prev'),
            'next_text'    => __('next »'),
        ));
    }
    else {echo "<h3>404 Error</h3>";}
    endif;
    wp_reset_postdata();
    $post=$back; //restore post object
    $return .= '</section>
            </div>
        </div>';
    return $return;
}
add_shortcode( 'jellythemes_gallery', 'jellythemes_gallery' );

Please could anyone one tell me why in second pagination i do not get content of custom post (gallery) it is same as page one?

You can use wp-paginate plugin. https://wordpress.org/plugins/wp-paginate/

Or you can also use below mention code for pagination of custom post type.

<?php
    $postsperpage = get_option('posts_per_page');
    if($media->max_num_pages > $postsperpage){
    ?>
    <nav role="navigation">
        <ul class="cd-pagination no-space">
            <li class="button"><?php echo get_previous_posts_link( '' ); ?></li>
            <?php
            for($count=1; $count<=$media->max_num_pages; $count++){
                if($count == $paged){
                    $pagiClass="current";
                }else{
                    $pagiClass="";
                }
                echo '<li><a class="'.$pagiClass.'" href="'.get_permalink($current_post_id).'page/'.$count.'">'.$count.'</a></li>';
            }
            ?>
            <li class="button"><?php echo get_next_posts_link( '', $media->max_num_pages ); ?></li>
        </ul>
    </nav>
    <?php
    }
    ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM