WordPress.org

Ready to get started?Download WordPress

Forums

hook and rewrite (2 posts)

  1. testinaweb
    Member
    Posted 3 years ago #

    hi,
    everybody knows that if you write http://www.domain.com/category/categoryname you arrive on /wp-content/theme/template/archive.php with a variable with the category name.

    i'd like to do the same. i want to write http://www.domain.com/list/variable1/variable2/variable3/etc and arrive to /wp-content/theme/template/list.php file.

    i wrote the code below but i can not go on...
    the code add a rewrite rule, but i arrive to /wp-content/theme/template/index.php...

    can someone help me?

    thanks in advance
    manuel

    add_action('generate_rewrite_rules', 'add_rewrite_rules');
    add_filter('query_vars', 'query_vars');
    add_filter('init','flushRules');
    
    function add_rewrite_rules( $wp_rewrite ) {
       $new_rules = array('list/(.+?)/?$' => 'index.php?list=$matches[1]');
       $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    
    function query_vars($public_query_vars) {
       $public_query_vars[] = "list";
       return $public_query_vars;
    }
    
    function flushRules(){
       global $wp_rewrite;
       $wp_rewrite->flush_rules();
    }
  2. 19dozen
    Member
    Posted 3 years ago #

    I am having a similar problem in WordPress 3 with a multisite install. You seem to have got further than I have though.

    I think your problem is in the line:

    $new_rules = array('list/(.+?)/?$' => 'index.php?list=$matches[1]');

    have you tried changing it to the following?

    $new_rules = array('list/(.+?)/?$' => 'index.php?pagename=list&list=$matches[1]');

    I have implimented your code on my site and it is working. It takes you through to a profile page and allows me to display users profiles (we've also build an advanced search in to it). Code is below if it helps :-)

    All the best,

    Steve

    function nd_add_rewrite_rules( $wp_rewrite ) {
    	   $new_rules = array('profile-page/(.+?)/?$' => 'index.php?pagename=profile-page&id=$matches[1]');
    	   $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    	}
    
    	function nd_query_vars($public_query_vars) {
    	   $public_query_vars[] = "id";
    	   return $public_query_vars;
    	}
    
    	function nd_flushRules(){
    	   global $wp_rewrite;
    	   $wp_rewrite->flush_rules();
    	}
    
    	add_action('generate_rewrite_rules', 'nd_add_rewrite_rules');
    	add_filter('query_vars', 'nd_query_vars');
    	add_filter('init','nd_flushRules');

Topic Closed

This topic has been closed to new replies.

About this Topic