Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Code clean up WordPress

  • SOLVED

Hello experts,

I want to use two different widget in the same area for the purpose of the first widget will contain dynamic content like archives,recent comments, categories, etc. And the second widget is designated to have a static content like "capture or subscribe form". Now the task is to clean this code in a semantic way.

Ooops ok to make it more specific. I'm not an php expert and i think using 2 IF END IF CONDITIONAL STATEMENT is not good idea maybe combining the 2 IF END IF into one but providing the results in terms of displaying the 2 widgets what u think?


<?php
/**
* The sidebar containing the main widget area.
*
*/
?>
<div class="span4">
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div><!-- #secondary -->
<?php endif; ?>

<?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-2' ); ?>
</div><!-- #secondary -->
<?php endif; ?>
</div>

Answers (4)

2013-06-18

Abdelhadi Touil answers:

Hi.
I think the code can be used like this:

<?php

/**

* The sidebar containing the main widget area.

*

*/

?>

<div class="span4">

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>

<?php dynamic_sidebar( 'sidebar-1' ); ?>

<?php endif; ?>



<?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>

<?php dynamic_sidebar( 'sidebar-2' ); ?>

<?php endif; ?>

</div>


But if the theme use the "widget-area" class and the "secondary" id, then it's not good to use the same ID twice (I mean "secondary"). If the two widgets are used, then you'll have two ID, but in css the ID should be used just once.
For this you can change the code like this:

<?php

/**

* The sidebar containing the main widget area.

*

*/

?>

<div class="span4">

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>

<div id="secondary1" class="widget-area" role="complementary">

<?php dynamic_sidebar( 'sidebar-1' ); ?>

</div><!-- #secondary -->

<?php endif; ?>



<?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>

<div id="secondary2" class="widget-area" role="complementary">

<?php dynamic_sidebar( 'sidebar-2' ); ?>

</div><!-- #secondary -->

<?php endif; ?>

</div>


Good luck.


eric abao comments:

@Abdelhadi,

I think using 2 IF END CONDITIONAL STATEMENT is not good idea maybe combining the 2 IF END IFs into one but providing the results in terms of displaying the 2 widgets. This is only what i need...

2013-06-18

Giri answers:

I still don't understand your question. Could you explain little bit more? You don't have to use two different sidebars for static and dynamic content. If you are going to use html text then just drag and drop the text widget..


eric abao comments:

@Giri,

I'm not an php expert and i think using 2 IF END CONDITIONAL STATEMENT is not good idea maybe combining the 2 IF END IFs into one but providing the results in terms of displaying the 2 widgets what u think?


Giri comments:

Here is the thing. You can use any number of IF statements. It will not affect your site's performance. So don't worry about. But you are using one sidebar actually. So I don't think you need the second sidebar.


<?php

/**

* The sidebar containing the main widget area.

*

*/

?>

<div class="span4">

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>

<div id="secondary" class="widget-area" role="complementary">

<?php dynamic_sidebar( 'sidebar-1' ); ?>

</div><!-- #secondary -->

<?php endif; ?>

</div>



Here I deleted the second sidebar. Now you can use any number of widgets in that sidebar. It all will appear in you sidebar correctly


Giri comments:

And if still those IF statements bothering you use it like this


<?php

/**

* The sidebar containing the main widget area.

*

*/

?>

<div class="span4">

<div id="secondary" class="widget-area" role="complementary">

<?php dynamic_sidebar( 'sidebar-1' ); ?>

</div><!-- #secondary -->


<div id="secondary" class="widget-area" role="complementary">

<?php dynamic_sidebar( 'sidebar-2' ); ?>

</div><!-- #secondary -->

</div>


Giri comments:

By the way you should use unique ids in css.

<?php
/**
* The sidebar containing the main widget area.
*
*/
?>
<div class="span4">

<div id="secondary1" class="widget-area" role="complementary">

<?php dynamic_sidebar( 'sidebar-1' ); ?>

</div><!-- #secondary -->

<div id="secondary2" class="widget-area" role="complementary">

<?php dynamic_sidebar( 'sidebar-2' ); ?>

</div><!-- #secondary -->

</div>


Giri comments:

Also don't forget to read this question and answer..

[[LINK href="http://stackoverflow.com/questions/7895966/does-having-a-lot-of-if-statements-degrade-rendering-speed-of-php"]]http://stackoverflow.com/questions/7895966/does-having-a-lot-of-if-statements-degrade-rendering-speed-of-php[[/LINK]]


eric abao comments:

okay i think i got this now... i just didn't realize i can freely declare multiple if end ifs statement in the sidebar.


Giri comments:

Yes.. As I said earlier... You can use any number of if statements, it won't affect your site's performance.. So don't worry about.

2013-06-18

Remy answers:

By semantic way do you mean html5 semantic ? Here is what I would do

<?php

/**

* The sidebar containing the main widget area.

*

*/

?>

<aside class="span4" role="complementary">

<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>

<div id="dynamic-sidebar" class="widget-area">

<?php dynamic_sidebar( 'sidebar-1' ); ?>

</div><!-- #secondary -->

<?php endif; ?>

<?php if ( is_active_sidebar( 'sidebar-2' ) ) : ?>

<div id="static-sidebar" class="widget-area">

<?php dynamic_sidebar( 'sidebar-2' ); ?>

</div><!-- #secondary -->

<?php endif; ?>
</aside>


I think you could do without the two divs around the dynamic_sidebar(), but without knowing your css use of the widget-area class, and if you use the IDs for anything, I'm not sure. Not sure about the two different widgets area, since your second area seems to be only a text widget.

2013-06-18

Eric P. answers:

Why not just put all the widgets in a single sidebar? You can have multiple widgets in a sidebar. And multiple "text" widgets for fixed content.