0

I have a Wordpress site that accepts posts by email and feeds them into specific categories. I just adopted a new theme (Astrid) and while I've been able to customize it with css and child theme, I'm frustrated by the fact that the sidebar is ever present in the category/archive view. Is there a way (perhaps using category.php) to remove the sidebar from all category pages (which for my site would mean all pages except one static "about" page and the homepage itself which shows most recent blog posts).

Thanks much!

4 Answers 4

1

category page flow (bottom to top) is like this:

category-slug.php
category-ID.php
category.php
archive.php
index.php

so you can not create compulsory category.php. open your current theme archive.php. and edit this code.

<?php 
     if( !is_category() ){
        get_sidebar();
      }
?>
0

This solution depend on the theme. Sometimes this solution does not work for your theme.

  1. Go to category.php page.

  2. Find get_sidebar function.

  3. Remove the get_sidebar function including parameters if there.

You may need to change some css part also. And if you are not using child theme your customization will be lost when updating the theme.

2
  • My theme didn't come with category.php. I had to create it. Does have archive.php though.
    – Etherplain
    Nov 18, 2016 at 6:39
  • Then create it. There are many tutorials on the internet. Ex : wpbeginner.com/wp-themes/…. Even you can customize archive.php. It has get_sidebar function. Put it inside the if(!is_category()) {}. However it is better to create a category.php file.
    – Ranuka
    Nov 18, 2016 at 6:47
0

So, in the same way you might go to the store and you can't find that one item your shopping for, then break down and ask someone only to learn it's right behind you.... I figured this out. I cheated, but I found that if you just copy the full width page template into category.php (which I had to create) and just change one line of code, it does the job. Here's the full php if it helps anyone.

    <?php

/*

Template Name: Full width

*/
    get_header();
?>



    <div id="primary" class="content-area fullwidth">
        <main id="main" class="site-main" role="main">

            <?php while ( have_posts() ) : the_post(); ?>

                <?php get_template_part( 'template-parts/content', 'page' ); ?>

                <?php

                    if ( comments_open() || '0' != get_comments_number() ) :

                        comments_template();

                    endif;
                ?>

            <?php endwhile; // end of the loop. ?>


        </main><!-- #main -->
    </div><!-- #primary -->

<?php get_footer(); ?>

Only thing you need to change is here. Change page to category:

<?php get_template_part( 'template-parts/content', 'category' ); ?>

That said, I'm sure there's an easier way to address this problem. One way may be to display categories on a specific page (where the theme's page templates will come into play), instead of redirecting to the category like I do.

2
  • So while the above does work, I'm not entirely sure why it does and I'm trusting other comments here that suggest it's simply not right, so I went with Ranuka and Vasim's solution. Also went one step more and did the same for single posts. All changes made in child theme.
    – Etherplain
    Nov 18, 2016 at 7:22
  • This is a perfectly valid solution. See WP Template Hierarchy: wphierarchy.com. You basically are funneling visitors to the correct template based on content type.
    – disinfor
    Nov 16, 2017 at 21:52
0

Your provided example only works because the new template that you've created is hooked to content-category.php, which doesn't exist by default within the Astrid Theme.

I've only taken a brief scan of the code for Astrid, but it looks like there's only a single sidebar that's called -- located in index.php.

If you're trying to avoid the sidebar on specifically your Archives and Categories pages, open index.php and you should see the following:

<?php
/**
* The main template file.
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists.
*
* @link https://codex.wordpress.org/Template_Hierarchy
*
* @package Astrid
*/

get_header(); ?>

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

    <?php
    if ( have_posts() ) :

        if ( is_home() && ! is_front_page() ) : ?>
            <header>
                <h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
            </header>

        <?php
        endif;

        /* Start the Loop */
        while ( have_posts() ) : the_post();

            /*
             * Include the Post-Format-specific template for the content.
             * If you want to override this in a child theme, then include a file
             * called content-___.php (where ___ is the Post Format name) and that will be used instead.
             */
            get_template_part( 'template-parts/content', get_post_format() );

        endwhile;

        the_posts_navigation();

    else :

        get_template_part( 'template-parts/content', 'none' );

    endif; ?>

    </main><!-- #main -->
</div><!-- #primary -->

<?php
get_sidebar();
get_footer();

At the bottom of index.php, get_sidebar(); is called with no condition as to when.

If I understand your question correctly, the simplest resolution would be to wrap the get_sidebar() method in a conditional statement as follows:

    if( !is_category() && !is_archive() ) {
    get_sidebar();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.