custom dinamically generated (sub)pages

Hi All,

I have a a custom wp page with the following slug: /artist

In that page, I'd like to catch the rest of url to manage it as a normal parameter for the same page.

For example:
/artist/foo

where "foo" isn't a page, but a simple parameter of the "artist" page. I know that mod_rewrite could be a friend of mine, but I don't know how to do.

To sum up, I'd like to have just ONE page, that takes in input the rest of URL as a parameter.

Any hints, any suggetions?

thanks in advance!
e.

  • DavidM

    Hello there Emiliano,

    That one had me scratching my head for a while as to how to respond to it because if you're including parameters, something must be making use of the paremeters (ie. some PHP) but the following post made a bit more sense of why and how this would be used in Wordpress.

    http://wordpress.org/support/topic/adding-custom-rewrite-rule-to-my-blog?replies=7

    Hope that post helps!

    Cheers,
    David

  • Emiliano

    David, Ulrich,
    thanks for your replies!

    David, your link is interesting, but I'm not able to setup a working example of redirecting. Ulrich, I tried to add your line into my .htaccess without success. :slight_frown:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteRule ^artist/(.+) artist/ [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    # END WordPress

    If if go to http://mysite.com/artist/foo
    I received a 404 error, with the only WP page "artist".

    What am I missing?
    e.

  • Emiliano

    Just to shall you know how I resolved this issue, hoping to help anyone :slight_smile:
    I've just added the following lines into my function.php:

    add_action( 'init', 'wpa5413_init' );
    function wpa5413_init()
    {
    
        // Remember to flush the rules once manually after you added this code!
        add_rewrite_rule(
            // The regex to match the incoming URL
            'artist/([^/]+)/?',
            // The resulting internal URL: <code>index.php</code> because we still use WordPress
            // <code>pagename</code> because we use this WordPress page
            // <code>designer_slug</code> because we assign the first captured regex part to this variable
            'index.php?pagename=artist&artist_slug=$matches[1]',
            // This is a rather specific URL, so we add it to the top of the list
            // Otherwise, the "catch-all" rules at the bottom (for pages and attachments) will "win"
            'top' );
        //global $wp_rewrite;
        //$wp_rewrite->flush_rules(true);
    }
    
    add_filter( 'query_vars', 'wpa5413_query_vars' );
    function wpa5413_query_vars( $query_vars )
    {
        $query_vars[] = 'artist_slug';
        return $query_vars;
    }

    Then, create a new page called "artist" with a template that will have the following simple code (to catch the rest of rthe url as a normal parameter):

    <?php
    /*
    Template Name: ArtistPage
    */
    global $wp_query;
    
    if(isset($wp_query->query_vars['artist_slug'])) {
        $test_slug = urldecode($wp_query->query_vars['artist_slug']);
    }
    echo $artist_slug;
    ?>

    Home it helps!

    Ciao,
    e.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.