Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.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
Php 分页不适用于Post name permalink,但适用于普通permalink_Php_Wordpress - Fatal编程技术网

Php 分页不适用于Post name permalink,但适用于普通permalink

Php 分页不适用于Post name permalink,但适用于普通permalink,php,wordpress,Php,Wordpress,我正在尝试循环浏览一个名为blog的帖子类型。当Wordpress permalinks设置为plain时,分页工作正常,但是当我将其更改为post名称并单击继续分页链接时,它会加载404错误 我发现你不能有相同的文章类型和页面名称,因为它会导致404错误。我想知道是否有解决办法,因为更改帖子类型的名称会影响博客帖子 mypage-blog.php <?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' )


我正在尝试循环浏览一个名为blog的帖子类型。当Wordpress permalinks设置为plain时,分页工作正常,但是当我将其更改为post名称并单击继续分页链接时,它会加载404错误

我发现你不能有相同的文章类型和页面名称,因为它会导致404错误。我想知道是否有解决办法,因为更改帖子类型的名称会影响博客帖子

mypage-blog.php

 <?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query( array( 'post_type' => 'blog',
                             'posts_per_page' => 2,
                             'paged'          => $paged,
                            'has_archive' => false,
                            'rewrite'     => array(
                                             'slug'       => '/blog', // if you need slug
                                             'with_front' => false,
                                             ),)
);
if ( $loop->have_posts() ):
    while ( $loop->have_posts() ) : $loop->the_post(); 
        // Set variables
              $title = get_the_title();
              $post_date = get_the_date('M j');
              $amount_of_time_to_read = get_field('amount_of_time_to_read');     

    ?>
        <a href="<?php the_permalink(); ?>" class="post-blog-link">
        <div class="post">
           <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
            <div class="post-image-v2" style="background-image:url('<?php echo $url ?>');">
            </div>
            <div class="post-content-v2">
                <h2 class="post-title"><?php echo $title; ?></h2>
                <div class="post-excerpt">
                    <p><?php echo get_excerpt(); ?></p>
                </div>
                <p class="post-date"> <span class="caps"><?php echo $post_date; ?></span> | <?php echo $amount_of_time_to_read; ?>min read</p>
            </div>
        </div>
        </a>

    <!--

                     -->
    <?php endwhile; ?>
    <center>
    <div class="pagination mt-25">
        <?php pagination_bar( $loop ); ?>
    </div>
        </center>
<?php wp_reset_postdata();
endif;

?>
<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); 

// set vars
$amount_of_time_to_read = get_field('amount_of_time_to_read');

?>

<a href="<?php the_permalink(); ?>" class="post-blog-link">
    <div class="post">
       <?php $url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ), 'thumbnail' ); ?>
        <div class="post-image-v2" style="background-image:url( '<?php echo $url ?>' );">
        </div>
        <div class="post-content-v2">
            <h2 class="post-title"><?php the_title(); ?></h2>
            <div class="post-excerpt">
                <p><?php the_excerpt(); ?></p>
            </div>
            <p class="post-date"> <span class="caps"><?php the_date( 'M j' ); ?></span> | <?php echo $amount_of_time_to_read; ?>min read</p>
        </div>
    </div>
    </a>

<?php endwhile; ?>

    <?php 
// You need to tweak this function, it shouldn't be needing a $loop var to work
// paste the function here and may be we will take a look at that
// pagination_bar( $loop ); 

the_posts_pagination();

?>

<?php else : ?>

    <?php // No Posts Found ?>

<?php endif; ?>

要使文章类型和页面具有相同的slug,您希望实现什么

据我所知,你想显示你的自定义文章类型“博客”的存档。您所要做的就是创建一个文件名archive-blog.php并使用普通的WordPress循环。这样,您就不需要使用page-blog.php(删除它)来显示“blog”文章类型的存档。yourwebsite.com/blog将自动显示您的“blog”存档

使用下面的代码粘贴到archive-blog.php中

 <?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$loop = new WP_Query( array( 'post_type' => 'blog',
                             'posts_per_page' => 2,
                             'paged'          => $paged,
                            'has_archive' => false,
                            'rewrite'     => array(
                                             'slug'       => '/blog', // if you need slug
                                             'with_front' => false,
                                             ),)
);
if ( $loop->have_posts() ):
    while ( $loop->have_posts() ) : $loop->the_post(); 
        // Set variables
              $title = get_the_title();
              $post_date = get_the_date('M j');
              $amount_of_time_to_read = get_field('amount_of_time_to_read');     

    ?>
        <a href="<?php the_permalink(); ?>" class="post-blog-link">
        <div class="post">
           <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
            <div class="post-image-v2" style="background-image:url('<?php echo $url ?>');">
            </div>
            <div class="post-content-v2">
                <h2 class="post-title"><?php echo $title; ?></h2>
                <div class="post-excerpt">
                    <p><?php echo get_excerpt(); ?></p>
                </div>
                <p class="post-date"> <span class="caps"><?php echo $post_date; ?></span> | <?php echo $amount_of_time_to_read; ?>min read</p>
            </div>
        </div>
        </a>

    <!--

                     -->
    <?php endwhile; ?>
    <center>
    <div class="pagination mt-25">
        <?php pagination_bar( $loop ); ?>
    </div>
        </center>
<?php wp_reset_postdata();
endif;

?>
<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); 

// set vars
$amount_of_time_to_read = get_field('amount_of_time_to_read');

?>

<a href="<?php the_permalink(); ?>" class="post-blog-link">
    <div class="post">
       <?php $url = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ), 'thumbnail' ); ?>
        <div class="post-image-v2" style="background-image:url( '<?php echo $url ?>' );">
        </div>
        <div class="post-content-v2">
            <h2 class="post-title"><?php the_title(); ?></h2>
            <div class="post-excerpt">
                <p><?php the_excerpt(); ?></p>
            </div>
            <p class="post-date"> <span class="caps"><?php the_date( 'M j' ); ?></span> | <?php echo $amount_of_time_to_read; ?>min read</p>
        </div>
    </div>
    </a>

<?php endwhile; ?>

    <?php 
// You need to tweak this function, it shouldn't be needing a $loop var to work
// paste the function here and may be we will take a look at that
// pagination_bar( $loop ); 

the_posts_pagination();

?>

<?php else : ?>

    <?php // No Posts Found ?>

<?php endif; ?>


必须将此添加到my functions.php

add_rewrite_rule('^blog/page/([0-9]+)','index.php?pagename=blog&paged=$matches[1]', 'top');

您的页面名称不能与帖子类型名称相同。如果您不能更改文章类型名称,请更改页面名称。@Darshkhkhkhar仍然不起作用,因为页面的URL将是/blogs,而自定义文章类型slug将是/blog。因此,您希望保持页面名称和文章类型名称相同,并希望分页工作吗?您可以使用重写规则使其工作。我没用过,但看看这个“可能会对你有帮助”。@Darshkhakhar谢谢你答案就在那里。谢谢你,兄弟!很乐意帮忙……)