BuddyDev

Search

[Resolved] Adding Widget Area

Tagged: 


  • Participant
    Level: Enlightened
    Posts: 40
    Jessica Martin on #22213

    I am adding a widget area, but only want it to appear only on posts and pages. I tried to use if ( is_singular( array( 'post', 'page' ) ) && is_active_sidebar( 'igmr_widget_area_above_container' ) but I am getting an error.

    Here is what I have now:

    register_sidebar( array(
    			'name'          => 'IGMR Before Container Area',
    			'id'            => 'igmr_before_container_area',
    			'description'   => __( 'Displays above container', 'text_domain' ),
    			'before_widget' => '<div id="%1$s" class="widget %2$s">',
    			'after_widget'  => '</div>',
    			'before_title'  => '<h3 class="widgettitle">',
    			'after_title'   => '</h3>'
    		) );
        }
    
    function igmr_widget_area_above_container() {
    	
    		if ( is_active_sidebar( 'igmr_before_container_area' ) ) : ?>
    			<div id="above-container" class="inner clearfix">
    					<section id="above-container-area">
    			<?php dynamic_sidebar( 'igmr_before_container_area' ); ?>
    					</section>
    			</div>
    			
    		<!-- #above-container -->
    			
    		<?php endif; 
    	
    		}
    		
    	add_action( 'cb_before_container', 'igmr_widget_area_above_container' );
    

    Is there a way to do this so that new widget area only appears on pages and posts?


  • Keymaster
    (BuddyDev Team)
    Posts: 11428
    Brajesh Singh on #22230

    Hi Jessica,
    Can you please give it a try and se eif it works?

    
    
    /**
     * Register widgets.
     */
    function buddydev_custom_theme_register_widgets() {
    
    	register_sidebar( array(
    		'name'          => 'IGMR Before Container Area',
    		'id'            => 'igmr_before_container_area',
    		'description'   => __( 'Displays above container', 'text_domain' ),
    		'before_widget' => '<div id="%1$s" class="widget %2$s">',
    		'after_widget'  => '</div>',
    		'before_title'  => '<h3 class="widgettitle">',
    		'after_title'   => '</h3>'
    	) );
    }
    
    add_action( 'widgets_init', 'buddydev_custom_theme_register_widgets' );
    
    function igmr_widget_area_above_container() {
    
    	if ( is_active_sidebar( 'igmr_before_container_area' ) && is_singular( array( 'post', 'page' ) ) ) : ?>
    		<div id="above-container" class="inner clearfix">
    			<section id="above-container-area">
    				<?php dynamic_sidebar( 'igmr_before_container_area' ); ?>
    			</section>
    		</div>
    
    		<!-- #above-container -->
    
    	<?php endif;
    
    }
    
    add_action( 'cb_before_container', 'igmr_widget_area_above_container' );
    
    

    In the previous code you shared, it seems that was a misplaced bracket ‘}’

    Regards
    Brajesh


  • Participant
    Level: Enlightened
    Posts: 40
    Jessica Martin on #22724

    Thank you. I am using this for an Adsense area and wanted to avoid it displaying on 404 pages. When using the above methods, I lost the area on the blog page. I used this instead:

    
    
    function igmr_container() {
    			
    		if ( !is_404() && is_active_sidebar( 'igmr_before_container_area' ) ) : ?>
    			<div id="igmr-container"
    				
    			<?php dynamic_sidebar( 'igmr_before_container_area' ); ?>
    			
    		<!-- #igmr_container -->
    			
    		<?php endif; 
    	
    	}
    		add_action( 'cb_before_container_contents', 'igmr_container',1);
    
    

  • Keymaster
    (BuddyDev Team)
    Posts: 11428
    Brajesh Singh on #22729

    Hi Jessica,
    I am not sure but it seems the code in the forum post in incorrect. Please make sure you have the divs closed.

    Condition seems good to me.

    Regards
    Brajesh


  • Participant
    Level: Enlightened
    Posts: 40
    Jessica Martin on #22741

    I caught and corrected the div. Thank you! What do you mean by the “code in forum post”?


  • Keymaster
    (BuddyDev Team)
    Posts: 11428
    Brajesh Singh on #22753

    Hi Jessica,
    I am glad you fixed it.

    By forum post I mean code in your previous post in this topic. I thought that it might have got posted incorrectly.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved
Subscribe