• Hello!
    I’m trying to add a sidebar to my archive pages, but only the archive pages for my custom post type ‘teachings’, and for the custom taxonomy pages that are correlated with that post type. I want to leave all the other pages and archives where a sidebar could go untouched, but I’m having a lot of difficulty.

    The best solution I could come up with was downloading a plugin that allows you to add sidebars to pages by shortcode (the one I ended up using is Stag Custom Sidebars), then adding that to the archive pages using my child theme’s functions.php file. So, I entered this:

    add_action( 'pre_get_posts', 'filter_sidebar' );
    function filter_sidebar(){
    	if( is_post_type_archive('teachings') ) {
    		echo do_shortcode( '[stag_sidebar id='filter']' );
    	}
    }

    but every time i apply this it breaks my site. I can’t figure out what I’m doing wrong…

    The reason I want to use my child theme’s functions.php is to avoid problems with updates to my theme in the future.

    Maybe there’s a better way to go about this. Any suggestions would be greatly appreciated.
    Thank you so much for taking the time to read my question, and thank you in advance for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    I think you would be a lot better off with some custom templates instead of using functions.php and a plugin. The custom templates may mainly be copies of existing templates, except with a particular name. If your custom templates are in your child theme’s folder, they will be safe from theme updates and will be used in preference to any parent theme templates of the same name. (not that yours will have the same names).

    For archives of a custom post type, copy the parent theme’s archive.php template to your child’s folder and name it archive-teachings.php. If the template does not have a get_sidebar() call, you probably only need to add it, assuming your theme has a sidebar.php template.

    The same goes for your custom taxonomy. You need to decide if you should use the parent’s archive.php, category.php, or tag.php. Copy the best fit to your child folder and name it taxonomy-{your-tax-slug}.php. If there is not a call to get_sidebar(), you should add it.

    If your parent theme did not have calls to get_sidebar(), you’ll probably need to adjust the CSS for the pages using these templates to narrow the main content to make room for the sidebar. You can add another class to whatever contains the main content so you can narrow that container for sidebars without affecting the same container without sidebars on other templates.

    More on naming template files is here: https://developer.www.ads-software.com/themes/basics/template-hierarchy/

    Thread Starter Calvary Tucson

    (@cfurrow)

    bcworkz, thank you so much!

    I didn’t know that I could add additional files to my child theme, that makes it much easier. And thank you for the detail on how to make the additional files. I think that will work perfectly for what i want to do.

    Thank you again!

    Thread Starter Calvary Tucson

    (@cfurrow)

    Hi bcworkz, sorry to open this up again, but i’m having a little bit of trouble.

    So, whenever I add anything to my sidebar it automatically resizes the archive pages and post pages to fit the sidebar in, even when i’ve removed the get_sidebar() from the archive and single php pages.

    I’ve tried everything i can think of, and everything I’ve found on google, to resize the archive pages that I don’t want to display a sidebar back to full width, but no luck.

    After I figure out how to resize posts and archive pages, i’m going to make custom php files to keep the sidebar for the archives and posts i do want to have the sidebar in.

    here’s an archive page that i can’t get to go full width:
    https://calvarytucson.wpengine.com/teachings/

    For the posts, I wrapped all of the content in a div and set that to 151.89% width, which is sort of a work around. Applying that to #content didn’t resize it fully, instead it pushed it to the right after it hit a certain width.
    here’s a page with that:
    https://calvarytucson.wpengine.com/teachings/life-as-god-intended/

    here’s the archive page code before I took out the sidebar:

    <?php
    /**
     * The template for displaying Archive pages
     *
     * Used to display archive-type pages if nothing more specific matches a query.
     *
     *
     * @since Yaga 1.0.0
     */
    $yaga_page = new Yaga_View(); 
    
    get_header(); ?>
    
    <div id="primary" class="content-area primary-content-area">
    
    	<?php $yaga_page->header(); 
    
    	$yaga_page->start_container();
    
    	if ( have_posts() ) :
    
    		$yaga_page->grid_container_starts();
    
    		do_action( 'yaga_before_archive_loop' );
    
    		while ( have_posts() ) : the_post(); 
    
    			get_template_part( 'content', 'grid' );
    
    		endwhile; 
    
    		do_action( 'yaga_after_archive_loop' );
    
    		$yaga_page->grid_container_ends();
    
    	else :
    
    		get_template_part( 'content', 'none' );
    
    	endif; 
    
    	$yaga_page->get_sidebar();
    
    	$yaga_page->end_container(); ?>
    
    </div><!-- #primary -->
    
    <?php get_footer();

    and after I removed the get_sidebar:

    <?php
    /**
     * The template for displaying Archive pages
     *
     * Used to display archive-type pages if nothing more specific matches a query.
     *
     *
     * @since Yaga 1.0.0
     */
    $yaga_page = new Yaga_View(); 
    
    get_header(); ?>
    
    <div id="primary" class="content-area primary-content-area">
    
    	<?php $yaga_page->header(); 
    
    	$yaga_page->start_container();
    
    	if ( have_posts() ) :
    
    		$yaga_page->grid_container_starts();
    
    		do_action( 'yaga_before_archive_loop' );
    
    		while ( have_posts() ) : the_post(); 
    
    			get_template_part( 'content', 'grid' );
    
    		endwhile; 
    
    		do_action( 'yaga_after_archive_loop' );
    
    		$yaga_page->grid_container_ends();
    
    	else :
    
    		get_template_part( 'content', 'none' );
    
    	endif; 
    
    	$yaga_page->end_container(); ?>
    
    </div><!-- #primary -->
    
    <?php get_footer();

    Here’s the single page before I removed the sidebar:

    <?php
    /**
     * The Template for displaying single posts, also the default template for displaying custom post types
     *
     * @since Yaga 1.0
     */
    
    $yaga_post = new Yaga_Single_View();
    
    get_header(); ?>
    
    <div id="primary" class="content-area primary-content-area">
    
    	<div id="content" class="site-content cf" role="main">
    
    		<?php 
    
    		$yaga_post->header(); 
    
    		$yaga_post->start_container();
    
    			while ( have_posts() ) : the_post(); 
    
    				$yaga_post->content();
    
    			endwhile; 			
    
    		$yaga_post->get_sidebar(); 
    
    		$yaga_post->end_container();?>
    
    		</div><!-- #content -->
    
    	</div><!-- #primary -->
    
    <?php
    get_footer(); ?>

    and with the sidebar removed:

    <?php
    /**
     * The Template for displaying single posts, also the default template for displaying custom post types
     *
     * @since Yaga 1.0
     */
    
    $yaga_post = new Yaga_Single_View();
    
    get_header(); ?>
    
    <div id="primary" class="content-area primary-content-area">
    
    	<div id="content" class="site-content cf" role="main">
    
    		<?php 
    
    		$yaga_post->header(); 
    
    		$yaga_post->start_container();
    
    			while ( have_posts() ) : the_post(); 
    
    				$yaga_post->content();
    
    			endwhile; 			
    
    		$yaga_post->get_sidebar(); 
    
    		$yaga_post->end_container();?>
    
    		</div><!-- #content -->
    
    	</div><!-- #primary -->
    
    <?php
    get_footer(); ?>

    thank you again for all your help!

    Moderator bcworkz

    (@bcworkz)

    A little bit of trouble, eh? So I see.

    This aspect of making room for sidebars is more complicated than I had imagined. On the archive page, the individual post positioning is done dynamically with javascript or jQuery, which makes my efforts to determine the proper CSS using a CSS inspector tool impossible. As that’s the only tool available to me, I can only speculate. On the single page, trying to use the inspector tool was crashing my tool, so I cannot even speculate there. Probably something about embedded videos I think.

    It may be setting the right CSS will cause the script to work properly, but it’s nearly impossible to determine what that might be. It may not even be the right approach.

    It appears your theme has a provision for sidebars, but it appears it’s an all or none proposition. We need some way to get the theme to think sidebars are called for without actually using the customizer setting. I’m guessing the mechanism is in the new Yaga_View object. Without open access to the theme’s source code, it’s impossible to say for sure. This is why the help provided in these forums is extremely limited when it comes to commercial themes. The official stance here is we cannot help at all.

    The only advice I can really offer is you should try getting an answer through your theme’s support mechanism. I’m sorry I can’t help any further. The custom template approach is still valid, but how the templates work needs to somehow work with your theme’s dynamic layout mechanism.

    Thread Starter Calvary Tucson

    (@cfurrow)

    ooh i see. Ok, I still have support with the theme so I’ll see if they can help me.

    Thank you again for your help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘sidebar only on certain archive pages’ is closed to new replies.