WordPress ways to add parameters to a friendly URL structure
P粉019353247
P粉019353247 2024-01-16 16:14:09
0
1
471

I built a Wordpress website with a page called "Nieuwsberichten". The URL looks like

https://www.example.com/newsitems

This page is dynamic, I need an extra parameter called "news_page"

https://www.example.com/news/?news_page=recent

The above URL is not friendly, so I created a rewrite rule for this and added the variable "news_page".

functions.php

function add_query_vars($vars){   
    $vars[] = 'news_page';     
    return $vars;
}
add_filter( 'query_vars', 'add_query_vars', 10, 1 );

function rewrite_paged(){
    add_rewrite_rule('^nieuwsberichten/([0-9]+)/?', 'nieuwsberichten?news_page=$matches[1]', 'top');
}
add_action('init', 'rewrite_paged');

I refreshed the permanent link after this Menu -> Settings -> Permalinks -> Save

If I browse to

https://www.example.com/news/recent/

It will redirect to

https://www.example.com/newsmessages/

What am I missing in my code?

Hope someone can help me solve this problem

P粉019353247
P粉019353247

reply all(1)
P粉541551230

In the regex part you specify only the numeric entries but pass text like "nearest" in the URL. Maybe the error lies here. Please check if it works for numeric input and edit your expression if that is the problem. Also, the url structure you specified doesn't seem quite right.

Anyway, I'm not sure about my answer, so I'd appreciate it if you could confirm it after checking.

https://developer.wordpress.org/reference/functions/add_rewrite_tag/一个> This is probably the function you should use to add parameters. This function also does not require the query_var function. So I don't think you need to use other functions.

I can't check or test right now. If your issue isn't resolved, I'll look into it in more detail.

You can use it like this:

function rewrite_paged(){
        add_rewrite_tag('%news_page%', '([^/]+)');
        add_rewrite_rule('^nieuwsberichten/?([^/]*)$', 'nieuwsberichten?news_page=$matches[1]', 'top');
}
add_action('init', 'rewrite_paged');

Also, if you are pointing to a page, you need to use the page ID or page name. Blog or product etc. If so, there will still be parameters you need to use and you should redirect this URL to index.php. I'm leaving an example for you to understand what I'm talking about, and to make it easier for you to see, you can view the rewrite on your site like this:

View example:

global $wp_rewrite;
print_r($wp_rewrite);

Example:

Matching page name:

add_rewrite_rule('^nieuwsberichten/?([^/]*)$', 'index.php?pagename=nieuwsberichten&news_page=$matches[1]', 'top');

Matching page ID:

add_rewrite_rule('^nieuwsberichten/?([^/]*)$', 'index.php?page_id=10000&news_page=$matches[1]', 'top');

Your example is correct if you are installing outside of WordPress, but if it is inside WordPress, all requests should be directed to index.php. So you may need to edit the code I've given like these examples.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template