Wordpress Themes - WP Forum at BFA
There will be no more development for Atahualpa (or any other theme), and no support. Also no new registrations. I turned off the donation system. I may turn the forum to read only if it gets abused for spam. Unfortunately I have no time for the forum or the themes. Thanks a lot to the people who helped in all these years, especially Larry and of course: Paul. Take care and stay healthy -- Flynn, Atahualpa developer, Sep 2021

Wordpress Themes - WP Forum at BFA » WordPress Themes » Montezuma Theme »

Advanced: Conditional Widgets


 
Prev Previous Post   Next Post Next
  #1  
Old Mar 20, 2013, 11:06 AM
jerryc
 
367 posts · Oct 2012
Florida
Advanced: Conditional Widgets

Quote:
Originally Posted by asifnkhan
is there any way to add a condition to the widget areas? For instance, If there is nothing in the widget area then it just won't render on the site and if it has something then it will show on the site. Right now, empty or not, it just reserves the space on the site for the widget.
Let's take what you've been discussing as an example. You have three widgets across, above your footer, each taking up a third of the page with a col4 div. As I get your question, if one is, for some reason, empty, you want to only show two and not show the empty space.

This is not for beginners, but with some basic PHP knowledge, it is doable, and there are several ways, but I'll Dick and Jane it as best as I can. For starters, I'd recommend you read this thread and this post.

I made a sample like you described, and randomly made widget 2 empty half the time. In real life, if you were going to use this, you'd probably have functions building the widgets with conditions, which would or could result in an empty widget (otherwise you wouldn't need this technique).

First, in my template, I put:

HTML Code:
<?php dynamic_sidebar( 'footer-widgets' ); ?>
In the widget, I used the PHP widget and put:

HTML Code:
<?php echo custom_make_footer_widgets(); ?>
Then, in a snippet, I put this function:

PHP Code:
function custom_make_footer_widgets()
{    
$fw1 "<p>Whatever you want in footer widget one</p>";
$fw2 "<p>Whatever you want in footer widget two</p>";
$fw3 "<p>Whatever you want in footer widget three</p>";
    
/* make widget 2 empty half the time for testing */
if (rand(0,1))
$fw2 "";
    
/* build array of widgets with something in them */
$fw_array = array();
    
if (
$fw1
$fw_array[] = $fw1

if (
$fw2
$fw_array[] = $fw2

if (
$fw3
$fw_array[] = $fw3;  


/* make different layouts for different numbers of widgets */
if (count($fw_array) == 3
$grid =  '<div class = "col4">'

if (
count($fw_array) == 2
$grid =  '<div class = "col6">'

if (
count($fw_array) == 1
$grid =  '<div class = "col12">';  

/* compile html */
$html '<div class = "row">'

foreach (
$fw_array as $fw
$html .= $grid $fw "</div>"

$html .= "</div>";
    
return 
$html;

Please post your results.

Last edited by jerryc; Mar 20, 2013 at 01:49 PM.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help - Advanced - Create link boxes !!!!!!! Budun Page & Category Menu Bars 1 Oct 10, 2010 08:21 PM
Advanced - Create link boxes Budun Page & Category Menu Bars 0 Oct 5, 2010 10:11 AM
Tiny MCE Advanced GlobalNeat Plugins & Atahualpa 5 Apr 9, 2010 11:43 AM
TinyMCE Advanced plugin - Do we Atalhualpians like it? Shepherd Jim Plugins & Atahualpa 13 Jun 29, 2009 06:17 PM


All times are GMT -6. The time now is 03:55 AM.


Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.