I am making a theme for my site which is WordPress based. I wanted a small help as I am not an expert on php. My issue is like this:
I have a side bar div which again within it has 4 sidebars:
<div id="jp-double-bar" class="equal">
<?php if ( is_active_sidebar( 'double-top' ) ) : ?>
<div class="jp-double-top">
<?php dynamic_sidebar( 'double-top' ); ?>
</div>
<?php endif; ?>
<div class="double-middle">
<?php if ( is_active_sidebar( 'middle-2' ) ) : ?>
<div class="jp-middle-2 equalmiddle">
<?php dynamic_sidebar( 'middle-2' ); ?>
</div>
<?php endif; ?>
<?php if ( is_active_sidebar( 'middle-1' ) ) : ?>
<div class="jp-middle-1 equalmiddle">
<?php dynamic_sidebar( 'middle-1' ); ?>
</div>
<?php endif; ?>
</div>
<?php if ( is_active_sidebar( 'double-bottom' ) ) : ?>
<div class="jp-double-bottom">
<?php dynamic_sidebar( 'double-bottom' ); ?>
</div>
<?php endif; ?>
</div>
Here my issue is that if I do not publish any widgets in any of the sidebars, still the outer <div id="jp-double-bar"> shows up.
My question is....... The way we are wrapping for individual sidebar with an <?php if ():?> statement, is there a way to wrap the main outer div with 4 if statements.
Like if there is no widget published in any of the 4 sidebars then the main outer div should not display at all.
Kindly help.
Regards
It seems that you have to be using the wp_get_sidebars api call to retrieve the bars. Check and see if this has a length of zero (empty).
Or just use and statements
if (is_active_sidebar( 'double-bottom' ) && is_active_sidebar('') && ...) {}
Related
I added a new custom field to a fields group to be used only to render the homepage. When trying read and display the contents of this field with the code below, nothing happens. I am sure the way to read custom field is how it should be. I don't get any error message. Could you please help fix this? This is being done on a WordPress site.
<div class="bg-white-base relative pt-row pb-row pl pr">
<div class="wrap-x">
<div class="inside mini">
<?php if ( $headline = get_sub_field( 'slider_-_h1' ) ) : ?>
<h2 class="mb color-purple-base">
<?php echo $headline; ?>
</h2>
<?php endif; ?>
<?php if ( $subheadline = get_sub_field( 'slider_-_h2' ) ) : ?>
<h3 class="mb alt-heading h5">
<?php echo $subheadline; ?>
</h3>
<?php endif; ?>
<?php if ( $text_area = get_sub_field( 'slider-_shortcode' ) ) : ?>
<article class="body-area mt2x">
<?php echo $text_area; ?>
</article>
<?php endif; ?>
</div>
</div>
</div>
get_sub_field() is supposed to be use inside a loop. When you use a group, you may use a loop like :
while( have_rows('hero') ): the_row();
........
endwhile;
Everything is explained here : https://www.advancedcustomfields.com/resources/group/
I'm not a wordpress expert, so wondering if someone can help me. I created a custom page template for one page so I could aggregate and display posts from one category on that page. However, I created one post in that category so far, but it gets displayed twice on the page. The posts that get pulled in from the category get displayed in the "div id=main" div. Here is the page template code:
<?php /* Template Name: Deals Page */ ?>
<?php get_header(); ?>
</div>
//<?php while ( have_posts() ) : the_post(); ?>
<div id="title-bar">
<h1 class="bar-entry-title container"><?php the_title(); ?></h1>
</div>
<?php endwhile; ?>
<div class="container col-md-12"><!-- restart previous section-->
<div id="primary" class="content-area col-md-8">
<main id="main" class="site-main" role="main">
<?php query_posts('category_name=deals &posts_per_page=20'); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h2>', '</h2>' ); ?>
<?php the_content(); ?>
//<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )
comments_template();
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_sidebar('footer'); ?>
<?php get_footer(); ?>
Do you know why it is displaying the post twice? I can't see why.
You are running two loops. Your comment line is outside of the PHP on the 5th line.
Change this //<?php while ( have_posts() ) : the_post(); ?>
to this <?php // while ( have_posts() ) : the_post(); ?>
Also comment out the first <?php endwhile; ?> around line 9 or else you will get a PHP error
my blog page in Wordpress is using a specific widget but I wanted to use different widget for a post in the blog.
I used an example (https://gist.github.com/anonymous/1308851) to write my code in sidebar.php.
<?php
if ( 'content' != $current_layout ) :
?>
<?php
//for rest of posts
if (is_active_sidebar('blog_widget_area') ) : ?>
<div id="secondary" class="blog_widget_area bordered" role="complementary">
<?php dynamic_sidebar( 'blog_widget_area' ); ?>
</div><!-- #secondary .widget-area -->
<?php endif;
//for specific post "Loyalty Program Trends in the Restaurant Industry"
if (is_active_sidebar('loyalty_programs_widget_area') && is_single('4063') ) : ?>
<div id="secondary" class="blog_widget_area bordered" role="complementary">
<?php dynamic_sidebar( 'loyalty_programs_widget_area' ); ?>
</div>
<?php endif;
?>
<?php endif; ?>
However, I couldn't get the second widget to display in that specific post....
Possible solutions:
Before trying below solution make sure some data/widget already loading on sideabar
loyalty_programs_widget_area.
solution 1
// use array inside is_single function
<?php if (is_active_sidebar('loyalty_programs_widget_area') &&
is_single( array('4063','4063-page-name-here') ) ) : ?>
<?php endif; ?>
solution 2
// put separate if check inside sidebar
<?php if (is_active_sidebar('loyalty_programs_widget_area') ) : ?>
if( is_single('4063') ) { } or
if( is_single(array('4063','pagename')) ) { }
<?php endif; ?>
Other possible solutions:
Try:
-deactivating ALL plugins temporarily to narrow down and possibly fix the problem . If the problem goes away, activate them individually to find the culprit?
-switching to the default theme (Twenty Ten) for a moment by renaming your current theme's folder in wp-content/themes. The idea is to force WordPress to fall back to the default theme to rule out any theme-specific issue?
For further details read official documentation regarding " sidebar "
Note:
is_single() offer different other ways to check post id or name(when permalink isenabled)
- is_single(array(17,'beef-stew','Irish Stew'));
- is_single('17'); or is_single(17);
Try this code below:
<?php // only show on 31 post not any other. ?>
<?php if ( is_single('31') ) { ?>
<?php // if blog_widget_area2 is active then show otherwise don't :) ?>
<?php if ( is_active_sidebar( 'blog_widget_area2' ) ) : ?>
<div class="template_2_widget_area bordered">
<?php dynamic_sidebar("blog_widget_area2"); ?>
</div>
<?php endif; ?>
<?php }else{ // otherwise load this sidebar ?>
<div class="template_2_widget_area bordered">
<?php dynamic_sidebar("blog_widget_area"); ?>
</div>
<?php } ?>
With the help of codewizz (Thanks codewizz!), we got it to work. The things we need to change is to move the code over from sidebar.php to single.php. However, the way it's coded, it displays both the first widget and the second widget on the post's sidebar.
So he suggested that I use http://wordpress.org/plugins/display-widgets/. And now it works.
Additionally, if one want to start from scratch, you should use that code
<?php // only show on 31 post not any other. ?>
<?php if ( is_single('31') ) { ?>
<?php // if blog_widget_area2 is active then show otherwise don't :) ?>
<?php if ( is_active_sidebar( 'blog_widget_area2' ) ) : ?>
<div class="template_2_widget_area bordered">
<?php dynamic_sidebar("blog_widget_area2"); ?>
</div>
<?php endif; ?>
<?php }else{ // otherwise load this sidebar ?>
<div class="template_2_widget_area bordered">
<?php dynamic_sidebar("blog_widget_area"); ?>
</div>
<?php } ?>
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 9 years ago.
Hi I'm getting this error message Parse error: syntax error, unexpected end of file in woocommerce.php on line 56, when I am trying to view my products in woocommerce. Here is the code for that file. Line 56 is the last line.
">
<div class="container" role="main">
<div class="row">
<?php do_action( '__before_article_container'); ##hook of left sidebar?>
<div id="content" class="<?php echo tc__f( '__screen_layout' , tc__f ( '__ID' ) , 'class' ) ?> article-container">
<?php do_action ('__before_loop');##hooks the header of the list of post : archive, search... ?>
<?php if ( tc__f('__is_no_results') || is_404() ) : ##no search results or 404 cases ?>
<article <?php tc__f('__article_selectors') ?>>
<?php do_action( '__loop' ); ?>
</article>
<?php endif; ?>
<?php if ( have_posts() && !is_404() ) : ?>
<?php while ( have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
<?php the_post(); ?>
<?php do_action ('__before_article') ?>
<article <?php tc__f('__article_selectors') ?>>
<?php do_action( '__loop' ); ?>
</article>
<?php do_action ('__after_article') ?>
<?php endwhile; ?>
<?php woocommerce_content(); ?>
<?php do_action ('__after_loop');##hook of the comments and the posts navigation with priorities 10 and 20 ?>
</div><!--.article-container -->
<?php do_action( '__after_article_container'); ##hook of left sidebar?>
</div><!--.row -->
</div><!-- .container role: main -->
<?php do_action( '__after_main_container' ); ?>
Where's the endif for this:
<?php if ( have_posts() && !is_404() ) : ?>
You have unclosed IF statement:
<?php if ( have_posts() && !is_404() ) : ?>
try to add or delete : symbol if you want to use it only with next while
Here is the error free code, you are not closing an if statement
<div class="container" role="main">
<div class="row">
<?php do_action( '__before_article_container'); ##hook of left sidebar?>
<div id="content"
class="<?php echo tc__f( '__screen_layout' , tc__f ( '__ID' ) , 'class' ) ?> article-container">
<?php do_action ('__before_loop');##hooks the header of the list of post : archive, search... ?>
<?php if ( tc__f('__is_no_results') || is_404() ) : ##no search results or 404 cases ?>
<article <?php tc__f('__article_selectors') ?>>
<?php do_action( '__loop' ); ?>
</article>
<?php endif; ?>
<?php if ( have_posts() && !is_404() ) : ?>
<?php while ( have_posts() ) : ##all other cases for single and lists: post, custom post type, page, archives, search, 404 ?>
<?php the_post(); ?>
<?php do_action ('__before_article') ?>
<article <?php tc__f('__article_selectors') ?>>
<?php do_action( '__loop' ); ?>
</article>
<?php do_action ('__after_article') ?>
<?php endwhile; ?>
<?php endif; ?>
<?php woocommerce_content(); ?>
<?php do_action ('__after_loop');##hook of the comments and the posts navigation with priorities 10 and 20 ?>
</div>
<!--.article-container -->
<?php do_action( '__after_article_container'); ##hook of left sidebar?>
</div>
<!--.row -->
</div>
<!-- .container role: main -->
<?php do_action( '__after_main_container' ); ?>
I've tried many variations and can't seem to get the syntax right. In one of my Wordpress theme pages I have the following Widget code. I want to append the existing functionality to where it removes this widget if the page name is 'event-view' or page id is '640'...how do I do this?
<?php
// A third sidebar for widgets
if ( is_active_sidebar( 'third-widget-area' ) ) : ?>
<div id="third" class="widget-area" role="complementary">
<ul class="xoxo">
<?php dynamic_sidebar( 'third-widget-area' ); ?>
</ul>
</div><!-- #secondary .widget-area -->
Try this:
<?php
// A third sidebar for widgets
if ( is_active_sidebar('third-widget-area') && !is_page(640) ) : ?>
<div id="third" class="widget-area" role="complementary">
<ul class="xoxo">
<?php dynamic_sidebar( 'third-widget-area' ); ?>
</ul>
</div>
You can also check page by it's name is_page('event-view')
try changing
if ( is_active_sidebar( 'third-widget-area' ) )
to
if ( is_active_sidebar( 'third-widget-area' ) && !is_page(640) )
this way you can check if the current page is any other than the one with the id 640; see http://codex.wordpress.org/Function_Reference/is_page
Try out this.
You can give pass page_id in is_page() function.
if(is_page('640') || is_page('49'))
And for more details see below.
http://wordpress.org/support/topic/display-widget-on-certain-pages