admin管理员组

文章数量:1122846

I have a few questions and I don't think the outcome I want is possible without adding a custom taxonomy and redoing a bunch of work.

I have registered a custom post type - gallery - with taxonomies set to post_tag and category.

I have the permalink structure set to /blog/%postname%/ so Posts can have the /blog/ base set using the default Posts section within WordPress.

When I use get_category_link() for a category on the gallery custom post type, it links to /blog/ceremonies/ instead of /gallery/ceremonies/ which is what I'd like for anything on the gallery archive or single gallery page. For the blog, it would need to be /blog/category_name/ as it currently is.

I know I can do the work from this question, but I have a lot of work done already that I do not want to back track on.

My questions:

Is there a way have /blog/ceremonies/ and /gallery/ceremonies/ load the same categories, and I can filter them with what the /blog/ or /gallery/ is set to in the URL with $_SERVER['REQUEST_URI'] (or something similar) without creating a custom taxonomy?

If not (I don't think there is), is there a way I can strip out the /blog/ out of the custom permalink so it just shows /ceremonies/ and still have /blog/ at the beginning of anything posted in the "Posts" section?

Any help is appreciated. Thank you!

I have a few questions and I don't think the outcome I want is possible without adding a custom taxonomy and redoing a bunch of work.

I have registered a custom post type - gallery - with taxonomies set to post_tag and category.

I have the permalink structure set to /blog/%postname%/ so Posts can have the /blog/ base set using the default Posts section within WordPress.

When I use get_category_link() for a category on the gallery custom post type, it links to /blog/ceremonies/ instead of /gallery/ceremonies/ which is what I'd like for anything on the gallery archive or single gallery page. For the blog, it would need to be /blog/category_name/ as it currently is.

I know I can do the work from this question, but I have a lot of work done already that I do not want to back track on.

My questions:

Is there a way have /blog/ceremonies/ and /gallery/ceremonies/ load the same categories, and I can filter them with what the /blog/ or /gallery/ is set to in the URL with $_SERVER['REQUEST_URI'] (or something similar) without creating a custom taxonomy?

If not (I don't think there is), is there a way I can strip out the /blog/ out of the custom permalink so it just shows /ceremonies/ and still have /blog/ at the beginning of anything posted in the "Posts" section?

Any help is appreciated. Thank you!

Share Improve this question edited Sep 7, 2017 at 13:33 Devyn asked Sep 6, 2017 at 20:57 DevynDevyn 1012 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

When you register your post type, in your options array, set

'rewrite' => array( 
    'with_front'    => false 
),

this will remove the "blog" from the front. Remember to flush your permalinks after you make this change.

Not sure about categories being programmed through wordpress (it COULD be possible) but you could do a rewrite rule in your htaccess that redirects all /blog/category to /gallery/category pretty easily. Something like:

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^/blog/category/([A-Za-z0-9-]+)/?$    /gallery/category/$1    [NC,L]    # Handle requests for "blog prefix"

I FOUND A SOLUTION

To begin, I had a custom post type (gallery) that I needed to use the same categories with as the WordPress default Posts but be completely separate. I finally came up with my own solution, and I'll share it in case anyone encounters the same issues I did.

First, I set the category base inside the permalink section to /blog/category.

Then, I created a rewrite rule: (thanks rudtek for the idea)

add_rewrite_rule('^gallery/category/(.+?)/?$', 'index.php?category_name=$matches[1]&is_gallery_cat=1', 'top');

and I needed to add is_gallery_cat to the query_vars to make it work.

add_filter( 'query_vars', 'query_vars' );
function query_vars( $vars )
{
  array_push($vars, 'is_gallery_cat');
  return $vars;
}

From there, on the gallery archive page, I used the following code to link to each of the categories it's listed in with it's own gallery link:

$cats = get_the_category();
$co = "";
if(!empty($cats)){
  foreach($cats as $cat){
    $link = get_category_link($cat->term_id);
    $link = str_replace("blog/category", "gallery/category", $link);
    $co .= "<a href='$link'>{$cat->name}</a>, ";
  }
}
echo rtrim($co, ", ");

That changes the links how I want them to, but the main category.php or archive.php page was still loading the post_type posts, so I needed to add a filter.

function set_category_archive_gallery_posts($query){
  //check if cat page
  if(is_category()){
    if(isset($query->query_vars['is_gallery_cat'])){
      //it's a gallery cat search, change the query type to gallery
      $query->set('post_type', 'gallery');
    }
  }
}
add_action('pre_get_posts', 'set_category_archive_gallery_posts');

Now, when I go to a category page via the blog (posts), or via the gallery, they load entirely separate posts using the same categories. They can share the category, for instance, parties but show different content.

Loading /blog/category/parties/ shows all the posts (not galleries) with parties set whereas /gallery/category/parties/ only shows the galleries with the category set, which is exactly what I was looking for.

I hope someone finds this useful!

本文标签: categoriesChange Category Base For Custom Post Type or Posts Page