• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 843
  • Last Modified:

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

0
PitaMaria
Asked:
PitaMaria
  • 5
  • 4
  • 2
1 Solution
 
DzynitCommented:
Try changing your || (or) to && (and) on line 2:

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

0
 
jrm213jrm213Commented:
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
0
 
PitaMariaAuthor Commented:
@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
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
PitaMariaAuthor Commented:
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.
0
 
jrm213jrm213Commented:
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

0
 
PitaMariaAuthor Commented:
@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

0
 
DzynitCommented:
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.
0
 
PitaMariaAuthor Commented:
@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

0
 
jrm213jrm213Commented:
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.
0
 
jrm213jrm213Commented:
ok, so that kind of makes sense that you are getting an unusual result. I think what you want is to edit the themes page.php, single.php, and archive.php and replace those get_sidebar() calls with calls to get the sidebars you want when those files are used to display content.

example is single.php from the twenty-ten theme, at the end it usually just has get_sidebar(); but we can make it display both sidebars.

Is that maybe more along the lines of what you need?
<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */

get_header(); ?>

		<div id="container">
			<div id="content" role="main">

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>

				<div id="nav-above" class="navigation">
					<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentyten' ) . '</span> %title' ); ?></div>
					<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentyten' ) . '</span>' ); ?></div>
				</div><!-- #nav-above -->

				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
					<h1 class="entry-title"><?php the_title(); ?></h1>

					<div class="entry-meta">
						<?php twentyten_posted_on(); ?>
					</div><!-- .entry-meta -->

					<div class="entry-content">
						<?php the_content(); ?>
						<?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
					</div><!-- .entry-content -->

<?php if ( get_the_author_meta( 'description' ) ) : // If a user has filled out their description, show a bio on their entries  ?>
					<div id="entry-author-info">
						<div id="author-avatar">
							<?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentyten_author_bio_avatar_size', 60 ) ); ?>
						</div><!-- #author-avatar -->
						<div id="author-description">
							<h2><?php printf( esc_attr__( 'About %s', 'twentyten' ), get_the_author() ); ?></h2>
							<?php the_author_meta( 'description' ); ?>
							<div id="author-link">
								<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>">
									<?php printf( __( 'View all posts by %s <span class="meta-nav">&rarr;</span>', 'twentyten' ), get_the_author() ); ?>
								</a>
							</div><!-- #author-link	-->
						</div><!-- #author-description -->
					</div><!-- #entry-author-info -->
<?php endif; ?>

					<div class="entry-utility">
						<?php twentyten_posted_in(); ?>
						<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
					</div><!-- .entry-utility -->
				</div><!-- #post-## -->

				<div id="nav-below" class="navigation">
					<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', 'twentyten' ) . '</span> %title' ); ?></div>
					<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', 'twentyten' ) . '</span>' ); ?></div>
				</div><!-- #nav-below -->

				<?php comments_template( '', true ); ?>

<?php endwhile; // end of the loop. ?>

			</div><!-- #content -->
		</div><!-- #container -->

<?php 
get_sidebar('Blog Widget Area');
get_sidebar('secondary-widget-area');
 ?>
<?php get_footer(); ?>

Open in new window

0
 
PitaMariaAuthor Commented:
@jrm213jrm213: Your suggestion helped and I was able to locate the issue. Thanks for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now