jeudi 19 mars 2015

URL rewriting with custom user meta "/%shop_name%/gallery/%gallery%"

I have created a custom user meta = shop_name in User Profile and custom post type gallery


I want my permalink to be in form of /%shop_name%/gallery/%gallery%.


Every user has a custom user_meta = shop_name.


Thus if my user has shop_name="shoppers stop" and a gallery whose title="Washing Machine" then my user url should be http://localhost/shoppers-stop/gallery/mashing-machine


When i go to http://localhost/shoppers-stop/gallery/I want all the galleries of shoppers-stop should be listed. Instead the index page is being shown. How can I make it possible?


Below is the code which i have written for creating custom url but want to extend it to above mentioned problem.



add_action('init', 'tdd_add_rewrite_rules');
function tdd_add_rewrite_rules(){
// Register custom rewrite rules
global $wp_rewrite;
//$wp_rewrite->add_rewrite_tag('%gallery%', '([^/]+)', 'gallery=');
$wp_rewrite->add_rewrite_tag('%shop_name%', '([^/]+)', 'shop_name=');
$wp_rewrite->add_permastruct('gallery', '/%shop_name%/gallery/%gallery%', false);
}

add_filter('post_type_link', 'tdd_permalinks', 10, 3);
function tdd_permalinks($permalink, $post, $leavename){
$no_data = get_the_author_meta('ID');;
$post_id = $post->ID;
if($post->post_type != 'gallery' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft'))) return $permalink;
// $var1 = get_post_meta($post_id, 'posts_solicitorspeciality', true);
$var1 = get_the_author_meta('shop_name');

$var1 = sanitize_title($var1);
if(!$var1) { $var1 = $no_data; }
$permalink = str_replace('%shop_name%', $var1, $permalink);
// $permalink = str_replace('%post_id%', $post_id, $permalink);
return $permalink;
}

Aucun commentaire:

Enregistrer un commentaire