WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
Clever URL's (4 posts)

  1. sdls
    Member
    Posted 1 year ago #

    hey there Scribu!

    Hoping you can help... I'm wondering if you've exlored custom URL sequence for related posts... for example.

    Post Type 1 - Fruit
    - Gala Apples
    - Oranges
    - Bananas

    Post Type 2 - Farmers Markets
    - Lower fairview
    - Waterfront
    - ect...

    Desire url sequence: /<market-name>/<fruit-name>

    So for "gala apples" associated with "lower fairview" url for the gala apples single page would look like "lower-fairview/gala-apples/"

    Any thoughts would be greatly appreciated.

    http://wordpress.org/extend/plugins/posts-to-posts/

  2. scribu
    Member
    Plugin Author

    Posted 1 year ago #

  3. sdls
    Member
    Posted 1 year ago #

    thanks for the ultra quick response Scribu! Can't seem to find the answer on the page you've suggested

    Are you suggesting I look at this tutorial?
    http://matty.co.za/2009/11/custom-url-rewrites-in-wordpress/

    Doesn't seem to be the same requirements as what Rayken is looking for. I'm a bit of a newb to this topic, so please excuse my slowness in understanding

    Any insight would be greatly appreciated.

  4. sdls
    Member
    Posted 1 year ago #

    Rayken's example is the closest I can find to a plug and play code. The key difference is in his example the first url chunk is fixed. i.e. 'spare-parts/' whereas I'm hoping to make this flexible.

    // the rules
    function custom_p2p_rewrites() {
        global $wp_rewrite;
    
        // Define custom rewrite tokens
        $rewrite_tag = '%productName%';
    
        // Add the rewrite tokens
        $wp_rewrite->add_rewrite_tag( $rewrite_tag, '(.+?)', 'products=' );
    
        // Define the custom permalink structure
        $rewrite_keywords_structure = $wp_rewrite->root . "/spare-parts/$rewrite_tag/";
    
        // Generate the rewrite rules
        $new_rule = $wp_rewrite->generate_rewrite_rules( $rewrite_keywords_structure );
    
        // Add the new rewrite rule into the global rules array
        $wp_rewrite->rules = $new_rule + $wp_rewrite->rules;
    
        return $wp_rewrite->rules;
    }
    
    // add to query vars
    function add_custom_p2p_rewrites( $public_query_vars ) {
        $public_query_vars[] = 'products';
        return $public_query_vars;
    }
    
    // flush
    function flush_custom_rules() {
        global $wp_rewrite;
        $wp_rewrite->flush_rules();
    }
    
    add_action( 'init', 'flush_custom_rules' );
    add_action( 'generate_rewrite_rules', 'custom_p2p_rewrites' );
    add_filter( 'query_vars', 'add_custom_p2p_rewrites' );

    Thanks thanks thanks for your help

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic