• Dave J

    (@dave-j)


    On my installation, I have a page set up as /blog/ which is where all my blog posts are, the homepage is a static template and therefore not the actual blog itself.

    I’m aware that it’s possible to add /blog/ to the permalink structure, however this causes problems as it applies to all post types, and I have custom post types in the site that I do not want to have /blog/ at the start of their urls.

    So, after some research, I came across the below, which puts /blog/ at the start of all regular post urls. It works very well, except for when pagination is used. If I click on page 2,3,4 etc. I’m getting a 404.

    So, this is what goes in my functions file-

    function filter_post_link($permalink, $post) {
        if ($post->post_type != 'post')
            return $permalink;
        return 'blog'.$permalink;
    	}
    	add_filter('pre_post_link', 'filter_post_link', 10, 2);
    
    	add_action( 'generate_rewrite_rules', 'add_blog_rewrites' );
    	function add_blog_rewrites( $wp_rewrite ) {
    	    $wp_rewrite->rules = array(
    	        'blog/([^/]+)/?$' => 'index.php?name=$matches[1]',
    	        'blog/[^/]+/attachment/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
    	        'blog/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
    	        'blog/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
    	        'blog/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
    	        'blog/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
    	        'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
    	        'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
    	        'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
    	        'blog/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?name=$matches[1]&paged=$matches[2]',
    	        'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
    	        'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]',
    	        'blog/[^/]+/([^/]+)/?$' => 'index.php?attachment=$matches[1]',
    	        'blog/[^/]+/([^/]+)/trackback/?$' => 'index.php?attachment=$matches[1]&tb=1',
    	        'blog/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
    	        'blog/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?attachment=$matches[1]&feed=$matches[2]',
    	        'blog/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?attachment=$matches[1]&cpage=$matches[2]',
    	    ) + $wp_rewrite->rules;
    	}

    …and this is the query I’m using on the blog page template:

    <?php
    					query_posts(array(
    					   'showposts' => get_option('posts_per_page'),
    					   'paged' => get_query_var('paged'),
    					   'orderby' => 'date',
    					   'order' => 'DESC'
    					));
    
    					while (have_posts()) : the_post();
    				?>
    						<h6>Posted on <?php the_time('F jS, Y'); ?> in
    							<?php
    								$category = get_the_category();
    								if($category[0]){
    								echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';
    								}
    							?>
    						</h6>
    						<a class="article" href="<?php the_permalink(); ?>">
    							<h2><?php the_title(); ?></h2>
    							<p><?php echo get_the_news_excerpt(); ?>...</p>
    							<p class="book-icon">Continue Reading</p>
    						</a>
    					<?php endwhile; ?>
    
    					<?php wp_pagenavi(); ?>
    
    				<?php wp_reset_query(); ?>

    Any help at all would be greatly appreciated.

  • The topic ‘Rewrite of posts base url causes 404 when pagination is used.’ is closed to new replies.