Wordpress自定义帖子类型索引页面 [英] Wordpress Custom Post Type Index Pageination

查看:197
本文介绍了Wordpress自定义帖子类型索引页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的主题创建了一个名为'Projects'的自定义帖子类型。在我的项目页面上,我目前正在使用以下代码显示一页上的所有项目。

I have created a custom post type for my theme called 'Projects'. On my projects page i am currently displaying all the projects on the one page, using the following code.

<?php
$args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 18 );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post();

                    echo '<div class="project p-project" data-filter="">';
                    echo '<a href="'.get_permalink( $post->ID).'">';  
                    the_post_thumbnail();
                    echo '</a>'; 
                    echo '</div>';

                    endwhile;

                ?>

我使用的是archive-projects.php,它使用的分页与帖子大致相同。代码如下..

I am using archive-projects.php which is using pagination much the same as posts. Code as follows..

    <?php
    if ( have_posts() ) :

        /* Start the Loop */
        while ( have_posts() ) : the_post();

            get_template_part( 'template-parts/post/contentp', get_post_format() );

        endwhile;

    else :

        get_template_part( 'template-parts/post/contentp', 'none' );

    endif;
    ?>
    <nav>
        <ul class="pager">
            <li><?php next_posts_link( 'Previous' ); ?></li>
            <li><?php previous_posts_link( 'Next' ); ?></li>
        </ul>
    </nav>

如何创建分页项目主页而不是仅显示所有可用项目。我只是添加到我现有的数组(例1)还是有一个我应该使用的特定模板(home-projects.php)?

How do i go about creating a paginated projects home page instead of just displaying all available projects. Would i just add to my existing array (example 1) or is there a specific template i should be using for example (home-projects.php) ?

推荐答案

根据 codex 试试这个:

将paged参数添加到查询中

如果WP_Query正在改变主循环和分页参数未设置,您需要使用 get_query_var()添加它。这就是WordPress确切地知道它在哪个页面。

If WP_Query is altering the main loop and the "paged" parameter is not set you'll need to add it with get_query_var(). This is so WordPress knows exactly what page it's on.

例如,如果您的查询看起来像这样(没有paged参数):

For example, if your query looks like this (without the "paged" parameter):

<?php $the_query = new WP_Query( 'posts_per_page=3' ); ?>

你添加如下参数:

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$the_query = new WP_Query( 'posts_per_page=3&paged=' . $paged ); 
?>

下一个示例与上面的示例完全相同,但是数组中的参数:

The next example is exactly the same as above but with the parameters in an array:

<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
  'posts_per_page' => 3,
  'paged'          => $paged
);

$the_query = new WP_Query( $args ); 
?>

所以在你的具体情况下它会是这样的:

So in your specific case it would be something like:

<?php
                $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
                $args = array( 'post_type' => 'bw_projects', 'posts_per_page' => 18, 'paged' => $paged );
                $loop = new WP_Query( $args );
                while ( $loop->have_posts() ) : $loop->the_post();

                echo '<div class="project p-project" data-filter="">';
                echo '<a href="'.get_permalink( $post->ID).'">';  
                the_post_thumbnail();
                echo '</a>'; 
                echo '</div>';

                endwhile;

            ?>

添加分页后,重置循环也是一个好主意,以便返回WP到物理页面(而不是您的档案列表)。你可以这样做:

After adding in your pagination, it's also likely a good idea to reset the loop, so as to return WP to the physical page(rather than your list of archives). You can do this with:

<?php wp_reset_postdata(); ?>

这篇关于Wordpress自定义帖子类型索引页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆