[...] this link: WordPress Permalink – How to Add Your Own Permalink Structures Posted in WordPress Tags: permalink, plugins, structure, WordPress « Why Can’t We [...]
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.
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.
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.
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 ‘/’?
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.
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;}
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?
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%
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?
[...] 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 [...]
[...] 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 [...]
[...] this link: WordPress Permalink – How to Add Your Own Permalink Structures Posted in WordPress Tags: permalink, plugins, structure, WordPress « Why Can’t We [...]
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.
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.
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.
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=”);
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.
[...] Continued here: WordPress Permalink – How to Add Your Own Permalink Structures [...]
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 -
Rewrite tag would be something like this -
Anyway, this is the general idea – you may need to change some things to get it exactly right.
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?
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%
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;
……..
}