分页在自定义帖子页面上不起作用

时间:2014-12-26 作者:Relax

我已经创建了名为

天然食品

在我的页面中,天然食物。php

<div class="main container">
    <div class="content left">
        <?php               
            $type = \'naturalfoods\';
            $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;

            $args=array(
              \'post_type\' => $type,
              \'post_status\' => \'publish\',
              \'posts_per_page\' => 2,
              \'paged\'=>$paged            
            );

            $my_query = null;
            $my_query = new WP_Query($args);

            if ( $my_query->have_posts()) :

            while ( $my_query->have_posts()) :  $my_query->the_post(); ?>

            <article class="post">
                <h1>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h1>                   

                <?php the_excerpt(); ?>     

                <div class="readMore right">        
                    <a href="<?php the_permalink(); ?>" class="btnReadMore">Read More&raquo;</a>
                </div><!-- End readMore --> 

                <div class="clear"></div>
            </article>      
            <?php endwhile;

            my_simone_paging_nav(); 

            else :
                echo "<p>No content found</p>";
            endif;

            wp_reset_query();  // Restore global post data stomped by the_post().                   

            ?>                      
    </div><!-- End content -->    
</div><!-- End main -->    
<?php get_footer(); ?>
我正在索引中使用以下函数。php

在我的功能中。php

if ( !function_exists( \'my_simone_paging_nav\' ) ) {

    function my_simone_paging_nav() {   

    // Don\'t print empty markup if there\'s only one page.
    if ( $GLOBALS[\'wp_query\']->max_num_pages < 2 ) {
        return;     
    }

    $paged        = get_query_var( \'paged\' ) ? intval( get_query_var( \'paged\' ) ) : 1;
    $pagenum_link = html_entity_decode( get_pagenum_link() );
    $query_args   = array();
    $url_parts    = explode( \'?\', $pagenum_link );

    if ( isset( $url_parts[1] ) ) {
        wp_parse_str( $url_parts[1], $query_args );
    }

    $pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
    $pagenum_link = trailingslashit( $pagenum_link ) . \'%_%\';

    $format  = $GLOBALS[\'wp_rewrite\']->using_index_permalinks() && ! strpos( $pagenum_link, \'index.php\' ) ? \'index.php/\' : \'\';
    $format .= $GLOBALS[\'wp_rewrite\']->using_permalinks() ? user_trailingslashit( \'page/%#%\', \'paged\' ) : \'?paged=%#%\';

    // Set up paginated links.
    $links = paginate_links( array(
        \'base\'     => $pagenum_link,
        \'format\'   => $format,
        \'total\'    => $GLOBALS[\'wp_query\']->max_num_pages,
        \'current\'  => $paged,
        \'mid_size\' => 2,
        \'add_args\' => array_map( \'urlencode\', $query_args ),
        \'prev_text\' => __( \'&laquo;\', \'my-simone\' ),
        \'next_text\' => __( \'&raquo;\', \'my-simone\' ),
        \'type\'      => \'list\',
    ) );

    if ( $links ) :

    ?>
    <nav class="navigation paging-navigation right" role="navigation">      
            <?php echo $links; ?>
    </nav><!-- .navigation -->
    <?php
    endif;
    }   
}
但分页不起作用

为创建自定义帖子类型定义的代码

function my_custom_posttypes() {

    // Natural Foods Post types
    $labels = array(
        \'name\'               => \'NaturalFoods\',
        \'singular_name\'      => \'NaturalFoods\',
        \'menu_name\'          => \'NaturalFoods\',
        \'name_admin_bar\'     => \'NaturalFoods\',
        \'add_new\'            => \'Add New\',
        \'add_new_item\'       => \'Add New NaturalFoods\',
        \'new_item\'           => \'New NaturalFoods\',
        \'edit_item\'          => \'Edit NaturalFoods\',
        \'view_item\'          => \'View NaturalFoods\',
        \'all_items\'          => \'All NaturalFoods\',
        \'search_items\'       => \'Search NaturalFoods\',
        \'parent_item_colon\'  => \'Parent NaturalFoods:\',
        \'not_found\'          => \'No NaturalFoods found.\',
        \'not_found_in_trash\' => \'No NaturalFoods found in Trash.\',
    );

    $args = array(
        \'labels\'             => $labels,
        \'public\'             => true,
        \'publicly_queryable\' => true,
        \'show_ui\'            => true,
        \'show_in_menu\'       => true,
        \'menu_icon\'          => \'dashicons-id-alt\',
        \'query_var\'          => true,
        \'rewrite\'            => array( \'slug\' => \'naturalfoods\' ),
        \'capability_type\'    => \'post\',
        \'has_archive\'        => true,
        \'hierarchical\'       => false,
        \'menu_position\'      => 5,
        \'supports\'           => array( \'title\', \'editor\', \'thumbnail\', \'comments\' ),
        \'taxonomies\'         => array(\'category\', \'post_tag\' )
    );


    register_post_type(\'naturalfoods\', $args);
}

add_action(\'init\',\'my_custom_posttypes\');

2 个回复
最合适的回答,由SO网友:shanebp 整理而成

改变$my_query$wp_query.
英寸my_simone_paging_nav(), 添加全局$wp_query 因此get_query_var 可以找到paged.

SO网友:cybmeta

问题是分页函数在全局查询中查找page参数,但您使用的是自定义查询来显示帖子。您有两种选择:

First option: Pass the custom query object to pagination function:

在分页选项中,使用作为参数传递的自定义查询,而不是使用全局查询对象:

function my_simone_paging_nav( $query ) {   

    // Don\'t use the global wp_query
    //if ( $GLOBALS[\'wp_query\']->max_num_pages < 2 ) {
    if( $query->max_num_pages < 2 ) {
        return;     
    }

Second option: use the archive template and leave WordPress handle the query and pages:

您已经创建了一个页面模板(page natural food.php),然后将该页面模板用作自定义帖子类型归档。这意味着WordPress对该页面进行查询,然后在该页面中,您必须构建一个自定义查询来显示您想要的内容。相反,您可以使用自定义的帖子类型存档模板,WordPress将对此进行查询without the need of a custom extra query. 您可以使用通用归档。php模板,或者,如果您只需要该自定义帖子类型的特定模板,请使用archive natural food。php(假设定制的post-type slug是天然食物)。天然食品档案的内容。php可以如下所示:

<div class="main container">
    <div class="content left">

        if ( have_posts()) :

        while (have_posts() ) {
            the_post();
            ?>

            <article class="post">
                <h1>
                    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                </h1>                   

                <?php the_excerpt(); ?>     

                <div class="readMore right">        
                    <a href="<?php the_permalink(); ?>" class="btnReadMore">Read More&raquo;</a>
                </div><!-- End readMore --> 

                <div class="clear"></div>
            </article>      
            <?php
            }

        my_simone_paging_nav(); 

        } else {
            echo "<p>No content found</p>";
        }          

        ?>                      
    </div><!-- End content -->    
</div><!-- End main -->    
<?php get_footer(); ?>

结束

相关推荐

Pagination and Related Posts

是否可以对嵌入在single中的以下相关POST代码使用分页。我的主题的php。 <?php // for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { $first_tag = $