[Resolved] Displaying Sub-Categories on Parent Category Page

Home Forums Support Displaying Sub-Categories on Parent Category Page

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #616276
    Andy

    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.

    #616832
    Tom
    Lead Developer
    Lead Developer

    The best option might be to 301 redirect the category pages to the static pages.

    That way the user/search engine never reaches the category.

    #617117
    Andy

    Thanks 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.

    #617644
    Tom
    Lead Developer
    Lead Developer

    If the category is named “Recipes”, then the URL should be /recipes/breakfast/name. It shouldn’t have the word category 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..

    #617646
    Tom
    Lead Developer
    Lead Developer
    #617659
    Andy

    Yeh 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.

    #617672
    Andy

    I 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.

    #617714
    Tom
    Lead Developer
    Lead Developer

    Just to confirm, did you leave the category_name parts of the code as they were, or did you change the name?

    #617878
    Andy

    At 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.

    #618062
    Tom
    Lead Developer
    Lead Developer

    You 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' );
        }
    }
    #618106
    Andy

    Hi 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
    }
    #618321
    Tom
    Lead Developer
    Lead Developer

    That shouldn’t matter. Can you link me to the one of the pages?

    #618490
    Andy

    Hi,
    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.

    #618694
    Tom
    Lead Developer
    Lead Developer
    #618769
    Andy

    Thanks Tom working perfectly now!.

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.