分页不适用于邮政名称永久链接,但适用于普通永久链接

时间:2018-12-19 10:13:40

标签: php wordpress


我试图遍历称为Blog的帖子类型。当Wordpress永久链接设置为普通时,分页效果很好,但是当我更改它以发布名称并单击以打开分页链接时,它会加载404错误。

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

我的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;

?>

我的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);
}

2 个答案:

答案 0 :(得分:0)

您希望实现的帖子类型和页面具有相同的特征吗?

根据我的理解,您想显示自定义帖子类型“博客”的存档。您要做的就是创建一个文件名为archive-blog.php并使用普通的WordPress循环。这样,您无需具有page-blog.php(删除它)即可显示“博客”帖子类型的存档。 yourwebsite.com/blog将自动显示您的“博客”存档。

使用下面的代码粘贴到您的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; ?>

答案 1 :(得分:0)

必须将此添加到我的functions.php

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