• WordPress Permalink – How to Add Your Own Permalink Structures
    by ShibaShake on
    [   See Full Article   ]
    13 Comments
    1. [...] have tried this method: http://shibashake.com/wordpress-theme/wordpress-permalink-add but I didn’t get that one to work either. The problem is that I just don’t seem to [...]

      9:01 pm on December 21st, 2011 Reply
    2. [...] have tried this method: http://shibashake.com/wordpress-theme/wordpress-permalink-add but I didn’t get that one to work either. The problem is that I just don’t seem to [...]

      3:18 am on December 6th, 2011 Reply
    3. [...] this link: WordPress Permalink – How to Add Your Own Permalink Structures Posted in WordPress Tags: permalink, plugins, structure, WordPress « Why Can’t We [...]

      12:22 pm on February 3rd, 2011 Reply
    4. Alessio

      Is possible create an alias for permalinks of post? if I add a post with poermalink /blog/category/post.html I can add an alias permalink like /blog/category/post/more.html ?
      this 2 permalinks must get same post id.

      10:13 am on November 3rd, 2010 Reply
      • Sounds like using server redirects would be the better way to go.

        But if you want to do this within WordPress, you can just hook into the request filter hook, check for your more.html name within your function, and then redirect it to the proper post id in the returned query arguments.

        2:01 pm on November 8th, 2010 Reply
    5. Great articles on custom permalinks. for the site im working on i want links like this

      /products/post-type/tax-term/tax-term-child/post-name/

      i have been able to get the link structure correct using the post_type_link filter and a foreach() loop on get_the_terms() for that post.

      my cpt is registered with slug of ‘products/residential/%res_brands%’
      here is that code

      `function product_permalink($permalink, $post_id, $leavename) {
      if (strpos($permalink, ‘%res_brands%’) === FALSE) return $permalink;

      // Get post
      $post = get_post($post_id);
      if (!$post) return $permalink;

      // Get taxonomy terms
      $terms = wp_get_object_terms($post->ID, ‘resbrands’);
      if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) {
      $term_count = count($terms);
      $i=1;
      if($term_count > 1){
      foreach( $terms as $term ) {
      if (!empty($term) && is_object($term)) $taxonomy_slug .= $term->slug;
      if($i slug;
      }
      }
      //else $taxonomy_slug = ‘not-rated’;

      return str_replace(‘%res_brands%’, $taxonomy_slug, $permalink);
      }`

      I am running into trouble coming up with the proper wr_rewrite rules. I have tried several things. Im assuming that because my %res_brands% could be several tags with / is the problem.

      Also i am trying to rewrite my function similar to the one used in this article but any help would be great.

      8:50 am on October 29th, 2010 Reply
      • Not sure but assuming that you only need the last child in the category, the lowest sub can i break it apart to use only the last slug for the wp_rewrite?

        because %res_brands% could be parent/child/grandchild and posts assigned to the grandchild category would show up under parent and child so for the rewrite rule im assuming i need to change ‘([^/]+)’ to something that selects only the text after the last ‘/’?

        $wp_rewrite->add_rewrite_tag(“%res_brands%”, ‘([^/]+)’, “resbrand=”);

        9:01 am on October 29th, 2010 Reply
        • Hello Daniel,
          I’m afraid I won’t be of much help here. Regular expressions are really not my forte. I usually just bang at it, try out a whole bunch of different patterns, bang at it some more, until something works. :D

          2:06 pm on November 2nd, 2010 Reply
    6. [...] Continued here: WordPress Permalink – How to Add Your Own Permalink Structures [...]

      10:23 am on June 14th, 2010 Reply
    7. Sorry I misunderstood you.

      If you want to add %cat_id% to your general permalink structure, i.e. to your post links, then you want to use the post_link hook.

      You just need to replace the %cat_id% tag with the proper id. The function would look a lot simpler because you wouldn’t need to construct the whole permalink structure from scratch as I did with the gallery objects above.

      Instead your permalink function would just be something like -

      function new_permalink($permalink, $post, $leavename) {
        if (strpos($permalink, '%cat_id%') !== FALSE) {
          $cat_id = ...[get cat id from $post]
          $permalink = str_replace('%cat_id%', $cat_id, $permalink);
        }
        return $permalink;
      }

      Rewrite tag would be something like this -

      $wp_rewrite->add_rewrite_tag("%cat_id%", '([0-9]+)', "cat_id=");

      Anyway, this is the general idea – you may need to change some things to get it exactly right.

      11:51 pm on April 30th, 2010 Reply
    8. Ryan

      This will be very helpful for what I need to do. I need to have the ability to add the category id to the permalink structure. So, http://www.mydomain.com/%cat_id%. To get started, would I use the category_link filter?

      3:13 pm on April 30th, 2010 Reply
      • Yeah the category_link filter will allow you to alter the category permalinks to suit your needs but you must also add in proper rewrite rules afterwards.

        The most challenging part is making sure that your new rewrite rules do not mess up the existing rewrite rules – i.e., you don’t misfire a category rule when you are actually looking for a post. For example htt://mydomain.com/%cat_id% may conflict with htt://mydomain.com/%post_id%

        9:21 pm on April 30th, 2010 Reply
        • Ryan

          The full permalink structure that I want to end up having is as follows:

          mydomain.com/%cat_id%/%post_id%/%category%/%postname%

          This way I can use only: mydomain.com/%cat_id%/%post_id% for posting to twitter and the like. While also getting the seo benefits of the longer permalink structure that includes the category and postname.

          Now, how would you suggest that I do my conditional in my function? Would something like this be a step in the right direction?

          if ($term->taxonomy == ‘category’){
          ……

          //then I would set the $perma_array as follows:

          $cat = get_the_category();
          $perma_array['%cat_id%'] = $cat->cat_ID;

          ……..
          }

          10:41 pm on April 30th, 2010 Reply

    Leave a Reply

    Your email address will not be published.

    You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

    search button search button
    rss