I am having a rather bizarre issue that I cannot wrap my head around. I am working with code from the following to build my permalinks for my custom posts (they are location based):
[[LINK href="http://wpquestions.com/question/show/id/2660"]]http://wpquestions.com/question/show/id/2660
[[/LINK]]
<strong>Problem:
</strong>When I have the "/" in place, I get a 404 error... but the URL's are perfect.
When I remove the "/", the pages load but the URL's are no good.
<strong>What is Needed:
</strong>Help working with my permalink code so that http://www.website.com/region/country/city/state/page propagates properly.
====My Current Code====
<strong>functions.php
</strong>
// looks for %location% in the Custom Permalinks settings
function directory_location_permalink($permalink, $post_id, $leavename) {
if (strpos($permalink, '%portfolio_category%') === FALSE) return $permalink;
// Get post
$post = get_post($post_id);
if (!$post) return $permalink;
// Get taxonomy terms. Change location to your taxonomy slug
$args = array(
'orderby' => 'parent',
'order' => 'ASC',
);
$terms = wp_get_object_terms($post->ID, 'portfolio_category', $args);
if (!is_wp_error($terms) && !empty($terms) && count($terms) == 3) {
if(!$terms[0]->parent) {
$taxonomy_slug = $terms[0]->slug . "/" . $terms[1]->slug . "/" . $terms[2]->slug;
} else {
$taxonomy_slug = $terms[1]->slug . "/" . $terms[0]->slug;
}
} elseif (!is_wp_error($terms) && !empty($terms) && count($terms) == 4) {
if(!$terms[0]->parent) {
$taxonomy_slug = $terms[0]->slug . "/" . $terms[1]->slug . "/" . $terms[2]->slug . "/" . $terms[3]->slug;
} else {
$taxonomy_slug = $terms[1]->slug . "/" . $terms[0]->slug;
}
}
else $taxonomy_slug = 'other';
// replaces %location% with the actual name of the taxonomy term
return str_replace('%portfolio_category%', $taxonomy_slug, $permalink);
}
add_filter('post_type_link', 'directory_location_permalink', 10, 2);
add_filter('post_link', 'directory_location_permalink', 10, 3);
<strong>post-types.php
</strong>
add_action('init', 'portfolio_post_type_init');
function portfolio_post_type_init() {
$labels = array(
'name' => __('Directory', 'post type general name'),
'singular_name' => __('Directory', 'post type singular name'),
'add_new' => __('Add New', 'directory','aplex'),
'add_new_item' => __('Add New directory','aplex'),
'edit_item' => __('Edit Museum','aplex'),
'new_item' => __('New Museum','aplex'),
'view_item' => __('View Museum','aplex'),
'search_items' => __('Search directory','aplex'),
'not_found' => __('No museum found','aplex'),
'not_found_in_trash' => __('No museum found','aplex'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'menu_icon' => get_template_directory_uri() . '/images/admin/portfolio.png',
'publicly_queryable' => true,
'show_ui' => true,
'rewrite' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'show_in_nav_menus' => false,
'menu_position' => 40,
'rewrite' => array(
'slug' => 'private-museums-and-foundations/%portfolio_category%',
'with_front' => FALSE,
),
'taxonomies' => array('portfolio_category', 'portfolio_tags'),
'supports' => array(
'title','editor','author','thumbnail','excerpt','comments','trackbacks','custom-fields','revisions','page-attributes','tags'
)
);
register_post_type('portfolio',$args);
flush_rewrite_rules();
}
register_taxonomy("portfolio_category",
array("portfolio"),
array( "hierarchical" => true,
"label" => __("Directory Categories",'aplex'),
"singular_label" => __("Directory Categories",'aplex'),
"rewrite" => true,
"query_var" => true,
"rewrite" => array(
"slug" => "private-museums-and-foundations"
)
));
NOTE: If I replace the "/" with any other character (for example, "-"), the page loads fine. The only character giving me trouble is the "/".
Arnav Joy answers:
can you show me the url with problem in it.
BriceLucas comments:
Sure!
The url is http://www.artprivee.org/private-museums-and-foundations/europe/switzerland/reihen/fondation-beyeler/
Password: goodwork
Asad Iqbal answers:
Is it like the following:
Page opens: http://www.website.com/region/country/city/state/page
404 error: http://www.website.com/region/country/city/state/page/
Am I right?
BriceLucas comments:
This page does not open: http://www.website.com/region/country/city/state/page/
This page (http://www.website.com/region/country/city/state/page ) is automatically redirected to this page http://www.website.com/region/country/city/state/page/
So I am not sure if removing the trailing slash fixes the issue.
The url is http://www.artprivee.org/private-museums-and-foundations/europe/switzerland/reihen/fondation-beyeler/
Password: goodwork
Sabby Sam answers:
try to change the permalink
and use this link
http://codex.wordpress.org/Taxonomies
to understand.