This is searchable archive of our old support forums, which operated from 2012 - 2016. To find out how to get support for your current theme, please visit our support page.

Query for sidebar layout

  • Creator
    Topic
  • #5767
    askwpgirl
    Participant

    Hi Jason,

    I am trying to get a custom field value at the top of the right sidebar. The sidebar hook “themeblvd_fixed_sidebar_before” is applied to both sidebars, and I only want the custom field value to display at the top of one sidebar not both.

    I am using this code:

    function sidebar_content (){
    global $post;
    
    	?>
    	
    	<?php $sidebarcustom = get_post_meta($post->ID, 'right_sidebar', true); ?>
    	<?php if($sidebarcustom !== '') { ?>
    	<div class="sidebar-custom"><?php echo $sidebarcustom; ?></div>
    	<?php } else { echo 'NOTHING HERE'; } ?>	
    	<?php
    }
    
    if( themeblvd_config( 'sidebar_layout' ) == 'sidebar_right' ){
        
        add_action('themeblvd_fixed_sidebar_before','sidebar_content');
    
    }

    This works fine without the conditional if(themeblvd_config…. But then it gets the content at the top of both sidebars. What if any would be the appropriate syntax to query for the sidebar layout?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Author
    Replies
  • #5777
    Jason Bobich
    Keymaster

    You have a $position variable passed in to the themeblvd_fixed_sidebar_before action you can use.

    function sidebar_content( $position ) {
    	if( $position == 'right' )
    		// do whatever ...
    }
    add_action( 'themeblvd_fixed_sidebar_before', 'sidebar_content' );

    Note: This doesn’t have anything to do with the Sidebar Layout. The $position variable is simply being used to determine if it is the right or left sidebar, no matter what the sidebar layout.

    function sidebar_content( $position ){
    	global $post;
    	if( $position == 'right'  ) {
    		$sidebarcustom = get_post_meta($post->ID, 'right_sidebar', true);
    		if( $sidebarcustom )
    			echo '<div class="sidebar-custom">'.$sidebarcustom.'</div>';
    		else
    			echo 'NOTHING HERE';
    	}
    }
    add_action( 'themeblvd_fixed_sidebar_before', 'sidebar_content' );
    #5780
    askwpgirl
    Participant

    Thanks for that rapid reply. Of course, you would say things like “pass variable” to me, like I’m so good at that 😉

    That worked, btw. Thanks. Happy Saturday.

    #5790
    askwpgirl
    Participant

    So, I changed the add_action to show:

    add_action( 'themeblvd_fixed_sidebar_after', 'sidebar_content' );

    And it wouldn’t work. Works perfectly with themeblvd_fixed_sidebar_before.

    This works:

    function sidebar_content( $position ){
    	global $post;
    	if( $position == 'right'  ) {
    		echo 'YO';
    	}
    }
    add_action( 'themeblvd_fixed_sidebar_before', 'sidebar_content' );

    This doesn’t work:

    function sidebar_content( $position ){
    	global $post;
    	if( $position == 'right'  ) {
    		echo 'YO';
    	}
    }
    add_action( 'themeblvd_fixed_sidebar_after', 'sidebar_content' );

    Though, this does work:

    function sidebar_content(  ){
    		echo 'YO';
    }
    add_action( 'themeblvd_fixed_sidebar_after', 'sidebar_content' );

    But it places YO outside the sidebar inner div, so somehow that after hook is weird. I tested this in a non-edited child theme to eliminate any trouble I may have caused.

    #5799
    Jason Bobich
    Keymaster

    Yeah, we talked about this along time ago via email, remember? The themeblvd_fixed_sidebar_after doesn’t have the $position variable passed in, but I added for the next update because you asked.

    But it places YO outside the sidebar inner div, so somehow that after hook is weird. I tested this in a non-edited child theme to eliminate any trouble I may have caused.

    I’m not quite sure why you wouldn’t expect this to happen? — Think about this logically for a minute in terms of the order in which everything is happenning. The theme calls the action —

    do_action( 'themeblvd_fixed_sidebar_after' );

    Then the framework hooks on a function that outputs whatever there. Then, after that, you hook a function that does your thing and so when the theme calls this do_action you get:

    1) Framework’s thing…
    2) Your thing…

    This is exactly what happened when you hooked to themeblvd_fixed_sidebar_before —

    1) Framework’s thing…
    2) Your thing…

    lol, right? Ok, so what’s the solution? — If you want your action to come first, you need to either (A) give it a higher priority or (B) I guess place it before the framework is included in your Child theme. I would just give it a higher priority:

    add_action( 'themeblvd_fixed_sidebar_after', 'sidebar_content', 9 );

    Note: Any time you call add_action, by default the priority is 10 unless you pass in something different.

    #5809
    askwpgirl
    Participant

    Every day it’s something new. I’m glad that other people think I’m a WP goddess, because you and I are the only ones who know I am not. Do I get a badge for getting this? 🙂

    #5810
    askwpgirl
    Participant

    I just had some coffee. Yes, logically, I get that my thing is coming after the frameworks thing. I had 4 hours sleep before I wrote to you, so I was being a true airhead. And, what’s funny about that, is that I’ve actually used the after hook and knew exactly what to expect. Lol.

    Thanks for adding in passing in the variable for the sidebar after. Sweet. I do a lot of sites with custom field to be placed after widgets. I have a creative workaround for now.

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.