Forums

How can I detect the current page ID? (2 posts)

  1. rcwatson
    Member
    Posted 1 year ago #

    How do I detect the page ID value of the currently loaded page?

    I'd like to use the switch/case statement block to choose a sidebar (in sidebar.php) based on which page is currently loaded.

    In pseudocode, here's what I'd like to do as a set of switch/case statements, but I don't know if WordPress supports telling me what the current page ID is so I can pass it to switch.

    SWITCH (the current page id)
    in CASE the current page id is 123
    Use dynamic sidebar named 'about-us-widget-area'
    in CASE the current page is 234
    Use dynamic sidebar named 'press-widget-area'
    in CASE the current page is 345
    Use dynamic sidebar named 'careers-widget-area'
    etc.etc.etc.
    Otherwise
    Use dynamic sidebar named 'primary-widget-area'

    Thanks.

  2. rcwatson
    Member
    Posted 1 year ago #

    Never mind. I figured it out after finally finding this post.

    Here's what I did (currently just one case statement, but going to grow)

    $postid = get_the_ID();
    	$sidebar = '';
    	switch ($postid) {
    		case 55:
    			//$sidebar = 'custom-widget-area';
    			if ( ! dynamic_sidebar( 'custom-widget-area' ) ) :
    			endif;
    		break;
    		default:
    			//$sidebar = 'primary-widget-area';
    			if ( ! dynamic_sidebar( 'primary-widget-area' ) ) :
    			endif;
    	}

Topic Closed

This topic has been closed to new replies.

About this Topic