Link to home
Start Free TrialLog in
Avatar of PitaMaria
PitaMariaFlag for United States of America

asked on

Wordpress PHP conditional tag issues

I'm on Wordpress 3.0.0. For reference, here's my page: http://www.cleverpapers.com/index.php/

I've successfully registered a new sidebar named 'Blog Widget Area' that I want to appear on every page of my site containing posts (front_page, single, category, archive, etc.) -- basically every page except for 'home' and 'page' I was instructed to replace the get_sidebar tag in my page.php with conditional tags but nothing I've tried is working.

Here's the code I've tried in my child sidebar.php file (attached). The 'if' is working but the 'else' is not. Please help!




<?php
if (!is_home() || !is_page())
    {get_sidebar(dynamic_sidebar( 'Blog Widget Area' ));
    get_sidebar(dynamic_sidebar( 'secondary-widget-area' ));}
    else
       {get_sidebar(dynamic_sidebar( 'secondary-widget-area' ));}
?>

Open in new window

Avatar of Heather Ritchey
Heather Ritchey
Flag of United States of America image

Try changing your || (or) to && (and) on line 2:

if (!is_home() && !is_page())

I think what you want is this:

if(!(is_home()) && !(is_page()))
{
  get_sidebar(dynamic_sidebar( 'Blog Widget Area' ));
  get_sidebar(dynamic_sidebar( 'secondary-widget-area' ));
}
else
{
   get_sidebar(dynamic_sidebar( 'secondary-widget-area' ));
}

this way if it isn't the home page  AND (&&) it isn't using the "page" template, it will display 2 sidebars, otherwise just 1. In
Avatar of PitaMaria

ASKER

@Dzynit and @jrm213jrm213: Each of your solutions only give me:
– no sidebars on 'home' nor any 'page'
– only the secondary-widget-area on the page I have specified as my front page
– both Blog Widget Area and secondary-widget-area on all other post pages
Anybody there? This is one of the last things I need to de-bug so I'm anxious to get some answers. Any help is much appreciated.
ok so just to get some wording out of the way.

1. is your homepage actually a POST or is it a PAGE as far as wordpress is concerned.
2. home and front_page can be different now, do you have a page called home that isn't also set as your front_page?


is_page() is true if a "page" is being displayed.
is_single() is true if a single "post" page is being displayed.
is_home() is true if the "main page" is being displayed (this is tricky now that front_page is available)
is_front_page() is true if the main page is being displayed and in your settings you have "Front Page Displays" set to either "a static page" or "Your Latest Posts".

so maybe we can switch this around and try something like




//display both sidebars for front_page, any posts page)
if(is_front_page() || is_single() || is_archive())  
{
  get_sidebar(dynamic_sidebar( 'Blog Widget Area' ));
  get_sidebar(dynamic_sidebar( 'secondary-widget-area'));
}

//only display secondary-widget-sidebar for non-posts pages
if(is_page())
{
  get_sidebar(dynamic_sidebar( 'secondary-widget-area'));
}

Open in new window

@jrm213jrm213: I really appreciate your attention to this -- it's been driving me nuts. Unfortunately your latest suggestion didn't work.

First, to clarify -- My front page is a static page named "Home" with dynamic body classes like so: "home page page-id-22 page-template" My latest posts (a.k.a. blog) page is named "What's Happening", is located at /index.php/whats-happening/ and has a dynamic body class "blog" It's the front page and all other static pages with the body class "page" that only get "secondary-widget-area";
all other pages with posts -- including the blog page -- get both sidebars.

For reference, here's the code -- right outta the install -- from the parent theme's sidebar.php file, followed by the code in my child sidebar.php file...

// Begin parent sidebar.php code.
<?php
	// A second sidebar for widgets, just because.
	if ( is_active_sidebar( 'secondary-widget-area' ) ) : ?>

		<div id="secondary" class="widget-area" role="complementary">
			<ul class="xoxo">
				<?php dynamic_sidebar( 'secondary-widget-area' ); ?>
			</ul>
		</div><!-- #secondary .widget-area -->

<?php endif; ?>
// End parent sidebar.php code.

// Begin child sidebar.php code.
<?php
	// A second sidebar for widgets, just because.
	if ( is_active_sidebar( 'secondary-widget-area' ) ) : ?>
		<div id="secondary" class="widget-area" role="complementary">
			<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
			<ul class="xoxo">
					<?php
					//display both sidebars for blog, any posts page)
					if(is_single() || is_archive())  
					{
					  get_sidebar(dynamic_sidebar( 'Blog Widget Area' ));
					  get_sidebar(dynamic_sidebar( 'secondary-widget-area'));
					}
					
					//only display secondary-widget-sidebar for non-posts pages
					if(is_front_page() || is_page())
					{
					  get_sidebar(dynamic_sidebar( 'secondary-widget-area'));
					}
                    ?>
                <div id="share">
                    <p>share this page: 
                    <a name="fb_share" type="icon" border="0" target="_blank"><img src="http://www.cleverpapers.com/wp-content/themes/twentyten-child/images/blank.gif" alt="recommend this page on Facebook" /></a>/ 
                    <a href="http://twitter.com/share?url=<?php the_permalink(); ?>&text=Check out <?php the_title(); ?> at The Watermark:" border="0" target="_blank"><img src="http://www.cleverpapers.com/wp-content/themes/twentyten-child/images/icon_TwitterMark.png" alt="tweet this page on Twitter" /></a> / 
					<?php if(function_exists('wp_email')) { email_link(); } ?></p>
                    
                </div>
			</ul>
		</div><!-- #secondary .widget-area -->

<?php endif; ?>
// End child sidebar.php code.

Open in new window

PitaMaria, sorry - I got bogged down with a project and missed your reply.

jrm213 can probably help you get this nailed down, but let him know also if you've covered including the entire sidebar code in each of those functions you've set up.

If you're not getting your whole sidebar displayed along with your custom widgets you made, you're probably missing some code or have it mis-located.

I think it sounds more like you're calling just a widget using get_sidebar rather than calling your sidebar and including your custom widget in that sidebar file.

Maybe give a little more detail or show some code on how you've setup your custom widgets.
@jrm213jrm213: At the suggestion of @Dzynit I'm including here complete code -- not just snippets like in my last post -- for the entire parent theme's sidebar.php file, followed by the code in my child sidebar.php file...
// Begin parent sidebar.php code.

<?php
/**
 * The Sidebar containing the primary and secondary widget areas.
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */
?>

		<div id="primary" class="widget-area" role="complementary">
			<ul class="xoxo">

<?php
	/* When we call the dynamic_sidebar() function, it'll spit out
	 * the widgets for that widget area. If it instead returns false,
	 * then the sidebar simply doesn't exist, so we'll hard-code in
	 * some default sidebar stuff just in case.
	 */
	if ( ! dynamic_sidebar( 'primary-widget-area' ) ) : ?>
	
			<li id="search" class="widget-container widget_search">
				<?php get_search_form(); ?>
			</li>

			<li id="archives" class="widget-container">
				<h3 class="widget-title"><?php _e( 'Archives', 'twentyten' ); ?></h3>
				<ul>
					<?php wp_get_archives( 'type=monthly' ); ?>
				</ul>
			</li>

			<li id="meta" class="widget-container">
				<h3 class="widget-title"><?php _e( 'Meta', 'twentyten' ); ?></h3>
				<ul>
					<?php wp_register(); ?>
					<li><?php wp_loginout(); ?></li>
					<?php wp_meta(); ?>
				</ul>
			</li>

		<?php endif; // end primary widget area ?>
			</ul>
		</div><!-- #primary .widget-area -->

<?php
	// A second sidebar for widgets, just because.
	if ( is_active_sidebar( 'secondary-widget-area' ) ) : ?>

		<div id="secondary" class="widget-area" role="complementary">
			<ul class="xoxo">
				<?php dynamic_sidebar( 'secondary-widget-area' ); ?>
			</ul>
		</div><!-- #secondary .widget-area -->

<?php endif; ?>

// End parent sidebar.php code.



// Begin child sidebar.php code.

<?php
/**
 * The Sidebar containing the primary and secondary widget areas.
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */
?>

<?php
	// A second sidebar for widgets, just because.
	if ( is_active_sidebar( 'secondary-widget-area' ) ) : ?>
		<div id="secondary" class="widget-area" role="complementary">
			<script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>
			<ul class="xoxo">
					<?php
					//display both sidebars for blog, any posts page)
					if(is_single() || is_archive())  
					{
					  get_sidebar(dynamic_sidebar( 'Blog Widget Area' ));
					  get_sidebar(dynamic_sidebar( 'secondary-widget-area'));
					}
					
					//only display secondary-widget-sidebar for non-posts pages
					if(is_front_page() || is_page())
					{
					  get_sidebar(dynamic_sidebar( 'secondary-widget-area'));
					}
                    ?>
                <div id="share">
                    <p>share this page: 
                    <a name="fb_share" type="icon" border="0" target="_blank"><img src="http://www.cleverpapers.com/wp-content/themes/twentyten-child/images/blank.gif" alt="recommend this page on Facebook" /></a>/ 
                    <a href="http://twitter.com/share?url=<?php the_permalink(); ?>&text=Check out <?php the_title(); ?> at The Watermark:" border="0" target="_blank"><img src="http://www.cleverpapers.com/wp-content/themes/twentyten-child/images/icon_TwitterMark.png" alt="tweet this page on Twitter" /></a> / 
					<?php if(function_exists('wp_email')) { email_link(); } ?></p>
                    
                </div>
			</ul>
		</div><!-- #secondary .widget-area -->

<?php endif; ?>

// End child sidebar.php code.

Open in new window

I was looking into the functions you have in your code and I think that you can't nest them like you are...

instead of calling: get_sidebar(dynamic_sidebar('Blog Widget Area'));
try
either
  get_sidebar('Blog Widget Area');
or
  dynamic_sidebar('Blog Widget Area');



get_sidebar() - Includes the sidebar.php template file from your current theme's directory. If a name ($name) is specified then a specialized sidebar sidebar-{name}.php will be included.

If the theme contains no sidebar.php file then the sidebar from the default theme wp-content/themes/default/sidebar.php will be included.


dynamic_sidebar() -  This function calls each of the active widget callbacks in order, which prints the markup for the sidebar. If you have more than one sidebar, you should give this function the name or number of the sidebar you want to print. This function returns true on success and false on failure.

The return value should be used to determine whether to display a static sidebar. This ensures that your theme will look good even when the Widgets plug-in is not active.

If your sidebars were registered by number, they should be retrieved by number. If they had names when you registered them, use their names to retrieve them.
ASKER CERTIFIED SOLUTION
Avatar of jrm213jrm213
jrm213jrm213
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@jrm213jrm213: Your suggestion helped and I was able to locate the issue. Thanks for your help.