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


  #1  
Old Mar 20, 2013, 11:06 AM
jerryc
 
367 posts · Oct 2012
Florida
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.
  #2  
Old Mar 20, 2013, 11:32 AM
ericxjordan
 
2 posts · Mar 2013
I'm new to wordpress so could you post your full code and a link to an example where you have implemented this? I'd like to see it in action
  #3  
Old Mar 20, 2013, 11:40 AM
jerryc
 
367 posts · Oct 2012
Florida
This isn't a newbie technique (notice the first word of the thread title).

This post shows what I've implemented to far, and the links in that post show the howtos for various things.

You'll find on this forum that you're more likely to get concepts you can customize, use, and apply than you are to get copy and paste code -- fishing lessons rather than fish.
  #4  
Old Mar 20, 2013, 11:56 AM
ericxjordan
 
2 posts · Mar 2013
well can you point to a site showing it in action? it sounds interesting and I'd like to see it in action.
  #5  
Old Mar 20, 2013, 12:33 PM
jerryc
 
367 posts · Oct 2012
Florida
This post shows a lot of things made with custom php. The links in it show lots of other possibilities, including lots of conditions. You'll need to at least understand the two links in the first post on this thread to do any of this.

I posted this thread in response to an original poster who asked how to do have conditional widgets, and I asked him to post his results. Hopefully, he will soon, and you'll be able to see his implementation. Check back later.

Last edited by jerryc; Mar 20, 2013 at 12:36 PM.
  #6  
Old Mar 20, 2013, 01:46 PM
jerryc
 
367 posts · Oct 2012
Florida
Quote:
Originally Posted by newguy
well can you point to a site showing it in action? it sounds interesting and I'd like to see it in action.
Ok, I have posted a sample.

I have also edited the original post with all the code I used.

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 11:13 PM.


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