对自定义邮件类型页面进行分页

时间:2012-02-25 作者:AndrettiMilas

我为我的自定义帖子类型创建了一个页面。可在Snippi上查看代码:http://snippi.com/s/e8852rx

我正在尝试插入paginate\\u链接,但无论出于何种原因,它都没有显示出来。以下是paginate\\u links代码(此代码适用于普通archive.php文件):

<?php
global $wp_query;

$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $wp_query->max_num_pages
) );
?>

7 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

以下内容对我很有用(我已经删除了所有格式化/自定义帖子元)。

我想补充一下,不清楚为什么需要使用带有自定义模板的页面,而不是创建一个名为archive-portfolio.php 用于自定义帖子类型的存档页(请参阅template hierarchy)

<?php
/*
Template Name: Portfolio
*/
?>

<?php get_header(); ?>
<div id="container">
<div id="portfolio_content">
<div id="portfolio_wrap">

    <?php $loop = new WP_Query(array(\'post_type\' => \'portfolio\', \'posts_per_page\' => get_option(\'to_count_portfolio\'), \'paged\' => get_query_var(\'paged\') ? get_query_var(\'paged\') : 1 )
); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <span class="title"><?php the_title(); ?></span></br>
        <?php endwhile; ?>  

<?php

$big = 999999999; // need an unlikely integer
 echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $loop->max_num_pages
) );
?>
</div>
</div>
</div>
</div>

<?php get_footer(); ?>

SO网友:Pablo S G Pacheco

与Stephen Harris指出的“$循环->have\\u posts()”方法不同,您还可以尝试如下循环帖子:

<?php foreach ($loop->get_posts() as $post) { ?>
     <span class="title"><?php echo $post->post_title; ?></span></br>
<?php } ?>

SO网友:Tom J Nowell

您是否尝试过使用公文包帖子类型根归档页面?

如果您将大部分代码移动到归档组合中。php文件,然后应该正确设置循环,分页应该是开箱即用的,没有古怪的函数,没有黑客或乱七八糟的东西,就像在后期归档上一样。

这也意味着您不需要使用自己的自定义查询,只需使用主循环即可,从而加快页面加载速度

SO网友:David Gard

您正在引用主查询,因此它很可能只是试图显示“posts”,而不是您的自定义post类型。

不知道你的确切格式是什么,但我会重做查询,以便-

$args = Array( // Array of arguments for query_posts()
    \'numberposts\' => -1,
    \'posts_per_page\' => get_option(\'posts_per_page\'),
    \'paged\' => $paged,
    \'post_type\' => array(\'your-post-type-slug\', \'another-post-type-slug-if-you-want\')
);
query_posts($args);
这意味着您的代码将成为-

<?php
global $wp_query; // You don\'t need this if you are not in a function, so can probably be removed.

$args = Array( // Array of arguments for query_posts()
    \'numberposts\' => -1,
    \'posts_per_page\' => get_option(\'posts_per_page\'),
    \'paged\' => $paged,
    \'post_type\' => array(\'your-post-type-slug\', \'another-post-type-slug-if-you-want\')
);
query_posts($args);


$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    \'base\' => str_replace( $big, \'%#%\', get_pagenum_link( $big ) ),
    \'format\' => \'?paged=%#%\',
    \'current\' => max( 1, get_query_var(\'paged\') ),
    \'total\' => $wp_query->max_num_pages )
);
?>

SO网友:heathenJesus

这是我在很多场合都在努力解决的问题。

以下是在这种情况下对我有效的方法(WP 3.1.1):

<?php   
                $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
                $args = array( \'post_type\' => \'custom_post_type\', \'posts_per_page\' => 5, \'paged\' => $paged);
                $loop = new WP_Query( $args );
                if($loop->have_posts()):
                    while ( $loop->have_posts() ) : $loop->the_post();
                        //whatever output you require, such as, e.g:
                        the_excerpt();
                    endwhile;
                endif;
        ?>
然后,对于分页控件:

<?php 
    echo custom_paginate_links( 
                            array(
                                \'format\' => \'?paged=%#%\',
                                \'current\' => max( 1, get_query_var( \'paged\' ) ),
                                \'total\' => $loop->max_num_pages
                                ) );
?>

SO网友:deadlyhifi

最好在函数中创建一个函数。处理分页的php文件,可用于整个主题、标准和自定义帖子类型。

正在删除以下函数get_pagination() 进入你的主题将让你导航到任何你需要的地方。我在这里写了一篇博文:http://deadlyhifi.com/2011/06/non-bloated-pagination-for-your-wordpress-functions-php-file/ (代码基于http://robertbasic.com/blog/wordpress-paging-navigation/ 最初)

/**
 * A pagination function
 * @param integer $range: The range of the slider, works best with even numbers
 * Used WP functions:
 * get_pagenum_link($i) - creates the link, e.g. http://site.com/page/4
 * previous_posts_link(\' ‚ \'); - returns the Previous page link
 * next_posts_link(\' é \'); - returns the Next page link
 * http://robertbasic.com/blog/wordpress-paging-navigation/
 * tweaked by tdB ...
 */
function get_pagination($range = 4) {
global $paged, $wp_query;

// How much pages do we have?
if ( !$max_page ) {
    $max_page = $wp_query->max_num_pages;
}

// We need the pagination only if there is more than 1 page
if ( $max_page > 1 ) {
    if ( !$paged ) $paged = 1;

    echo \'<div class="postpagination">\';

    // To the previous page
    previous_posts_link(\'Prev\');

    if ( $paged >= $range ) echo \'<a href="\' . get_pagenum_link(1) . \'">1</a>\';
    if ( $paged >= ($range + 1) ) echo \'<span class="page-numbers">&hellip;</span>\';

    // We need the sliding effect only if there are more pages than is the sliding range
    if ( $max_page > $range ) {
        // When closer to the beginning
        if ( $paged < $range ) {
            for ( $i = 1; $i <= ($range + 1); $i++ ) {
                    echo ( $i != $paged ) ? \'<a href="\' . get_pagenum_link($i) .\'">\'.$i.\'</a>\' : \'<span class="this-page">\'.$i.\'</span>\';
            }
        // When closer to the end
        } elseif ( $paged >= ($max_page - ceil(($range/2))) ) {
            for ( $i = $max_page - $range; $i <= $max_page; $i++ ) {
                echo ( $i != $paged ) ? \'<a href="\' . get_pagenum_link($i) .\'">\'.$i.\'</a>\' : \'<span class="this-page">\'.$i.\'</span>\';
            }
        // Somewhere in the middle
        } elseif ( $paged >= $range && $paged < ($max_page - ceil(($range/2))) ) {
            for ( $i = ($paged - ceil($range/2)); $i <= ($paged + ceil(($range/2))); $i++ ) {
                echo ($i != $paged) ? \'<a href="\' . get_pagenum_link($i) .\'">\'.$i.\'</a>\' : \'<span class="this-page">\'.$i.\'</span>\';
            }
        }
    // Less pages than the range, no sliding effect needed
    } else {
        for ( $i = 1; $i <= $max_page; $i++ ) {
                echo ($i != $paged) ? \'<a href="\' . get_pagenum_link($i) .\'">\'.$i.\'</a>\' : \'<span class="this-page">\'.$i.\'</span>\';
            }
    }

    // On the last page, don\'t put the Last page link
    if ( $paged <= $max_page - ($range - 1) ) echo \'<span class="page-numbers">&hellip;</span><a href="\' . get_pagenum_link($max_page) . \'">\' . $max_page . \'</a>\';

    // Next page
    next_posts_link(\'Next\');

    echo \'</div><!-- postpagination -->\';
}
}

SO网友:Dominic

我在自定义页面上使用分页从未遇到过问题archive. 因此,与其制作一个页面,然后在上面写我自己的查询,不如复制/粘贴archive.php 并将其重命名为符合您的自定义帖子,例如archive-videos.php.

如果确实要使用自定义页面模板,请在查询之前添加此模板:

query_posts( array( \'post_type\' => \'videos\', \'paged\' => get_query_var(\'page\') ) )
更多信息:http://scribu.net/wordpress/wp-pagenavi/right-way-to-use-query_posts.html

结束

相关推荐

Pagination for query_posts();

我知道有很多像这样的问题,我也读过,但我还是不能让它工作。。。我有这个问题:query_posts(array(\'cat\'=>3,\'posts_per_page\'=>5)); 我只需在中显示以下内容while (have_posts()) : the_post(); 循环如何向此添加分页?我正在尝试添加posts_nav_link(); (我不知道调用我想要的功能是否正确)和显示的链接将我带到http://mysite.com/page/2. 由于它是我的索引页,所以可以不在