【问题标题】:Create pagination of custom post type in WordPress在 WordPress 中创建自定义帖子类型的分页
【发布时间】:2016-08-17 10:13:40
【问题描述】:

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

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

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    您可以使用 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
        }
        ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-08
      • 2015-02-05
      • 2014-05-15
      • 2011-03-20
      • 2014-05-21
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      相关资源
      最近更新 更多