Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Montezuma Theme (http://forum.bytesforall.com/forumdisplay.php?f=53)
-   -   Advanced: Conditional Widgets (http://forum.bytesforall.com/showthread.php?t=19945)

jerryc Mar 20, 2013 11:06 AM

Advanced: Conditional Widgets
 
Quote:

Originally Posted by asifnkhan (Post 98397)
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.

ericxjordan Mar 20, 2013 11:32 AM

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

jerryc Mar 20, 2013 11:40 AM

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.

ericxjordan Mar 20, 2013 11:56 AM

well can you point to a site showing it in action? it sounds interesting and I'd like to see it in action.

jerryc Mar 20, 2013 12:33 PM

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.

jerryc Mar 20, 2013 01:46 PM

Quote:

Originally Posted by newguy (Post 98412)
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.


All times are GMT -6. The time now is 12:08 PM.

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