在WordPress中创建自定义帖子类型的分页

时间:2016-08-17 10:13:41

标签: php wordpress pagination

我在SO和其他一些网站上找到了一些解决方案,但我的问题并没有解决这些问题。请问有谁告诉我问题出在哪里?当我点击第二页时,我得到了分页但没有获得自定义帖子(图库部分)的内容。到目前为止我尝试了什么:

自定义帖子(图库)创建:

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);
}

和查询:

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' );

请有人告诉我为什么在第二次分页中我没有获得自定义帖子(图库)的内容它与第一页相同?

1 个答案:

答案 0 :(得分:0)

您可以使用wp-paginate插件。 https://wordpress.org/plugins/wp-paginate/

或者您也可以使用下面提到的代码来自定义帖子类型的分页。

<?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
    }
    ?>