- This topic has 15 replies, 2 voices, and was last updated 6 years, 6 months ago by Tom.
-
AuthorPosts
-
July 5, 2018 at 8:57 am #616276Andy
Hi,
I basically want to display a grid style list of sub-categories on a parent category archive page. I know this is possible by creating a custom category archive template, but this would be quite complex.
I believe the easiest way would be to use a plugin like WP Show Posts which I’m quite familiar with. However, I foresee an issue where duplicate content would become an issue.For example, if I created a static page with the url /recipes/ and then created a grid on the page with links to the sub-categories breakfast, dinner, tea. I would also need to create the actual parent category for posts to be archived in. As WordPress doesn’t allow you to create two posts/pages with the same url this would need to be different to the page ‘recipes’
So I would end up with two similar pages, one called /recipes/ and the other /recipes-category/ for example.
The static page (/recipes/) which displays the sub-category links is the one that I actually want used by visitors and indexed by search engines. But WordPress will also create an archive of posts under the /recipes-category/ url and also under the sub-categories url /recipes-category/breakfast/.
So, is there anyway to stop WordPress from creating these archives or is my only option to noindex/nofollow them?. Or is there a better way to approach this?. I’d rather avoid using any other plugins if possible.
Any help appreciated.
Thanks.
July 5, 2018 at 9:14 pm #616832TomLead DeveloperLead DeveloperThe best option might be to 301 redirect the category pages to the static pages.
That way the user/search engine never reaches the category.
July 6, 2018 at 5:02 am #617117AndyThanks Tom thats a good idea. But thinking about it more there wont actually be any duplicate content. The static landing page /recipes/ will just have the grid of sub-categories and the /recipes-category/ archive will have lists of all the posts. The only slight concern I have is when someone clicks on a sub-category or post the URL will change slightly from the landing page, so they would expect the post URL to be /recipes/breakfast/post_name/ but it would actually show /recipes-category/breakfast/post_name/.
I’m not sure most visitors would actually notice but it’s something I’d always be aware of. I could get round this by making the sub-category pages static pages too and using WP Show Posts to display the relevant posts on each one. However, that WILL be creating duplicate content.
I just wish I knew what other people did in this situation.
July 6, 2018 at 3:47 pm #617644TomLead DeveloperLead DeveloperIf the category is named “Recipes”, then the URL should be
/recipes/breakfast/name
. It shouldn’t have the wordcategory
added it to it by default.However, I’m not sure if having a static page at
/recipes/
would conflict with the category. I suppose it might..July 6, 2018 at 3:49 pm #617646TomLead DeveloperLead DeveloperThis topic might help: https://generatepress.com/forums/topic/replace-default-post-category-template-with-a-blank-page-template/#post-608201
He was able to replace a category archive with a static page.
July 6, 2018 at 4:59 pm #617659AndyYeh sorry for the confusion. I renamed the category slug to /recipes-category/ because I assumed wordpress wouldn’t allow a page and category slug to be the same. However, according to that topic it does allow it but will show the category page when browsing to the same URL.
The code that the OP decides to use, option #3, looks very promising and avoids the duplicates issues I was concerned about.
I’ve pasted the code below for reference. Do you know how this could be altered to apply to more than one category or should I just create 2 filters, one for each category?.
As an experienced and advanced developer yourself, do you foresee any issues using this code in the future, for instance in the upcoming release with Guttenburg or does it generally look like a stable solution?.<?php add_filter('request', function(array $query_vars) { // do nothing in wp-admin if(is_admin()) { return $query_vars; } // if the query is for a category if(isset($query_vars['category_name'])) { // save the slug $pagename = $query_vars['category_name']; // completely replace the query with a page query $query_vars = array('pagename' => "$pagename"); } return $query_vars; }); ?>
Many thanks again Tom for your invaluable help and support.
July 6, 2018 at 5:36 pm #617672AndyI have just tried this code. It works fine for parent categories, but it breaks sub-categories giving the browser error page not found.
Also, won’t search engines still crawl and index both the static page and the category archive, which means duplicated content causing some SEO issues?.So it does seem either creating a custom category archive template or using some kind of plugin that allows me to customize category archive templates would be the best solution.
July 6, 2018 at 8:01 pm #617714TomLead DeveloperLead DeveloperJust to confirm, did you leave the
category_name
parts of the code as they were, or did you change the name?July 7, 2018 at 6:29 am #617878AndyAt first I tried changing the category names to mine but the code had no effect, so I changed them back to how they were and then it worked, but only for the parent categories.
I have now successfully setup a custom category archive and it’s working well. I do have a small issue, but it’s not really related to the custom category archive.
I’m using the GPP Page Header on all pages including the category archive. I’ve noticed if I DON’T use the template tag {{post_title}} anywhere in the page header content then it will display the archive title below the page header and also the category description.I’m already using a custom shortcode to display the category description (I was told this was required because I was using page headers).
add_shortcode( 'term_description', 'tu_term_description' ); function tu_term_description() { ob_start(); echo term_description( get_queried_object()->term_id, 'category' ); return ob_get_clean(); }
So if I make a page header title without {{post_title}} in the content I end up with 2 category archive titles (one in the page header and one under it) and 2 category descriptions on the page. There is just one category where I would prefer to change the page header title and not the category name itself.
Any advice appreciated.
July 7, 2018 at 10:01 am #618062TomLead DeveloperLead DeveloperYou should be able to remove it with this function:
add_action( 'wp', 'tu_remove_category_title' ); function tu_remove_category_title() { if ( is_category() ) { remove_action( 'generate_archive_title', 'generate_archive_title' ); } }
July 7, 2018 at 11:10 am #618106AndyHi Tom,
I just tried that code but it still shows a 2nd archive title and description below the page header. I’m wondering if it’s because I’m also using the following code to move the category name below the post title:
add_action( 'generate_after_entry_title', 'tu_add_meta_below_title' ); function tu_add_meta_below_title() { $categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'generatepress' ) ); $categories_list = sprintf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>', _x( 'Categories', 'Used before category names.', 'generatepress' ), $categories_list ); ?> <div class="entry-meta"> <?php echo $categories_list; ?> </div> <?php }
July 7, 2018 at 8:48 pm #618321TomLead DeveloperLead DeveloperThat shouldn’t matter. Can you link me to the one of the pages?
July 8, 2018 at 5:38 am #618490AndyHi,
I’ve edited the URL for this post with a test category so that you can see the issue without it affecting my other categories.July 8, 2018 at 10:21 am #618694TomLead DeveloperLead DeveloperI just edited the code above: https://generatepress.com/forums/topic/displaying-sub-categories-on-parent-category-page/#post-618062
Can you give it another shot?
July 8, 2018 at 1:18 pm #618769AndyThanks Tom working perfectly now!.
-
AuthorPosts
- You must be logged in to reply to this topic.