Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Wordpress自定义帖子类型查询中包含分页_Wordpress_Pagination - Fatal编程技术网

如何在Wordpress自定义帖子类型查询中包含分页

如何在Wordpress自定义帖子类型查询中包含分页,wordpress,pagination,Wordpress,Pagination,我的代码如下: <?php $the_query = new WP_Query( 'posts_per_page=30&post_type=phcl' ); ?> <?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?> <div class="col-xs-12 file"> <a href="<?php echo $file;

我的代码如下:

<?php $the_query = new WP_Query( 'posts_per_page=30&post_type=phcl' ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

<div class="col-xs-12 file">
    <a href="<?php echo $file; ?>" class="file-title" target="_blank">
        <i class="fa fa-angle-right" aria-hidden="true"></i> 
        <?php echo get_the_title(); ?>
    </a>
    <div class="file-description">
        <?php the_content(); ?>
    </div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
    $the_query = new WP_Query( array('posts_per_page'=>30,
                                 'post_type'=>'phcl',
                                 'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 
                            ); 
                            ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php the_permalink(); ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
</a>
<div class="file-description"><?php the_content(); ?></div>
</div>
<?php
endwhile;

$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' => $the_query->max_num_pages
) );

wp_reset_postdata();

我试图使用
paginate_links
Wordpress函数,但无论我把它放在哪里,我都无法让它工作。有人能帮我吗?

试试下面的代码:

<?php $the_query = new WP_Query( 'posts_per_page=30&post_type=phcl' ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

<div class="col-xs-12 file">
    <a href="<?php echo $file; ?>" class="file-title" target="_blank">
        <i class="fa fa-angle-right" aria-hidden="true"></i> 
        <?php echo get_the_title(); ?>
    </a>
    <div class="file-description">
        <?php the_content(); ?>
    </div>
</div>
<?php endwhile; wp_reset_postdata(); ?>
    $the_query = new WP_Query( array('posts_per_page'=>30,
                                 'post_type'=>'phcl',
                                 'paged' => get_query_var('paged') ? get_query_var('paged') : 1) 
                            ); 
                            ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="col-xs-12 file">
<a href="<?php the_permalink(); ?>" class="file-title" target="_blank">
<i class="fa fa-angle-right" aria-hidden="true"></i> <?php echo get_the_title(); ?>
</a>
<div class="file-description"><?php the_content(); ?></div>
</div>
<?php
endwhile;

$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' => $the_query->max_num_pages
) );

wp_reset_postdata();
$the_query=new WP_query(数组('posts_per_page')=>30,
“post_类型”=>“phcl”,
“paged'=>获取查询变量(“paged”)?获取查询变量(“paged”):1)
); 
?>

使用新的查询循环时,将“total”参数设置为
WP\u Query
对象的
max\u num\u pages
属性

自定义查询示例:

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

$args = array(
    'post_type'=>'post', // Your post type name
    'posts_per_page' => 6,
    'paged' => $paged,
);

$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
    while ( $loop->have_posts() ) : $loop->the_post();

             // YOUR CODE

    endwhile;

    $total_pages = $loop->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 »'),
        ));
    }    
}
wp_reset_postdata();
?>

适用于上述自定义查询的
paginate_链接
参数示例:


有关更多参考信息,请访问此

以代替此解决方案中的一个小错误。代码'paged'=>get_query_var('paged')?get_query_var('paged'):1)不应包含最后一个括号。为了使分页正确运行,请删除数字1后的右括号。当我单击第二页时,对分页的查询不起作用。t返回第一页posts@Nadia这个代码对你有用吗???@Jay不,它不感谢你分享这个。它确实显示了分页。但当我点击第2页的URL时,它仍然显示与第1页相同的内容。有什么想法吗?@KhomNazid你在遵循这个permalink结构,对吗?@PurvikDhorajiya,没有。我在遵循
%category%/%postname%
,这是我们在自定义帖子之外的博客部分所需要的。对于自定义帖子,我们依赖于“Permalink Manager Lite”插件。我们的具体案例在这里详述(现在我们的第2页给出了一个404):这太完美了@PurvikDhorajiya