Solved

Wordpress PHP conditional tag issues

Posted on 2010-09-08
11
820 Views
Last Modified: 2012-05-10
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
Comment
Question by:PitaMaria
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 14

Expert Comment

by:Dzynit
ID: 33630479
Try changing your || (or) to && (and) on line 2:

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

0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33630514
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
 

Author Comment

by:PitaMaria
ID: 33631633
@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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 

Author Comment

by:PitaMaria
ID: 33633137
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
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33640378
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
 

Author Comment

by:PitaMaria
ID: 33641132
@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
 
LVL 14

Expert Comment

by:Dzynit
ID: 33641142
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
 

Author Comment

by:PitaMaria
ID: 33641372
@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
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 33641479
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
 
LVL 17

Accepted Solution

by:
jrm213jrm213 earned 500 total points
ID: 33641608
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
 

Author Closing Comment

by:PitaMaria
ID: 33655326
@jrm213jrm213: Your suggestion helped and I was able to locate the issue. Thanks for your help.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
The purpose of this video is to demonstrate how to update a WordPress Site’s version. WordPress releases new versions of its software frequently and it is important to update frequently in order to keep your site secure, and to get new WordPress…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question