简体   繁体   中英

Pagination not working on Post name permalink but working on plain permalink


I am trying to loop through a post type called blog. The pagination works fine when the Wordpress permalinks are set to plain however when I change it to post the name and click to go on pagination link, it loads a 404 error.

I found out that you can't have the same post type and page name since it will cause a 404 error. I wanted to know if there was a workaround because changing the name of the post type will affect the blog posts.

My page-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;

?>

My functions.php

add_action('init', 'custom_rewrite_basic');
function custom_rewrite_basic() {
    global $wp_post_types;
    foreach ($wp_post_types as $wp_post_type) {
        if ($wp_post_type->_builtin) continue;
        if (!$wp_post_type->has_archive && isset($wp_post_type->rewrite) && isset($wp_post_type->rewrite['with_front']) && !$wp_post_type->rewrite['with_front']) {
            $slug = (isset($wp_post_type->rewrite['slug']) ? $wp_post_type->rewrite['slug'] : $wp_post_type->name);
            $page = get_page_by_slug($slug);
            if ($page) add_rewrite_rule('^' .$slug .'/page/([0-9]+)/?', 'index.php?page_id=' .$page->ID .'&paged=$matches[1]', 'top');
        }
    }
}

function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page' ) {
    global $wpdb;

    $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );

    return ($page ? get_post($page, $output) : NULL);
}

what do you want to achieve to have a Post Type and Page to be of the same slug?

As per my understanding you want to display the archive of your custom post type "Blog". All you have to do is create a file name archive-blog.php and use the plain WordPress loop. That way you don't need to have a page-blog.php (Delete it) to display the Archives of your "Blog" post type. yourwebsite.com/blog will automatically display your "Blog" archive.

Use the code below to paste in your archive-blog.php

<?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; ?>

不得不将其添加到我的functions.php

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM