width dinamic to sidebar depend from if is published or not

Hello,
i have created 4 new position

<div class="top-bar">
<div id="sidebar-topheader1" class="sidebar">
<?php dynamic_sidebar( 'topheader1' ); ?>
</div>
<div id="sidebar-topheader2" class="sidebar">
<?php dynamic_sidebar( 'topheader2' ); ?>
</div>
<div id="sidebar-topheader3" class="sidebar">
<?php dynamic_sidebar( 'topheader3' ); ?>
</div>
<div id="sidebar-topheader4" class="sidebar">
<?php dynamic_sidebar( 'topheader4' ); ?>
</div>
</div>

all work fine but i have the problem width of this 4 sidebar are not dinamic, i want to put if i use for example sidebar 1 and 4 divide 50% of width for all two and other two hidden.

Anyone can help me please?

  • Vaughan

    hiya

    thanks for posting

    you would need some identifier for using an if() statement.

    then run through a for loop or something to create a number.

    <?php
    if($page="test") {
        $width = '50';
    } else {
        $width = '25';
    }
    ?>
    
    <div class="top-bar">
    <div id="sidebar-topheader1" class="sidebar-$width">
    <?php dynamic_sidebar( 'topheader1' ); ?>
    </div>
    <div id="sidebar-topheader2" class="sidebar-$width">
    <?php dynamic_sidebar( 'topheader2' ); ?>
    </div>
    <div id="sidebar-topheader3" class="sidebar-$width">
    <?php dynamic_sidebar( 'topheader3' ); ?>
    </div>
    <div id="sidebar-topheader4" class="sidebar-$width">
    <?php dynamic_sidebar( 'topheader4' ); ?>
    </div>
    </div>

    so once we've determined above in the if() you set $width.

    so then create a CSS for each of those classes above

    sidebar-25 {
        width: 25%;
    }
    sidebar-50 {
        width: 50%;
    }

    the hardest part here is the the if() condition to determine whether you use a 50% width or 25% width, & how you determine which dynamic sidebars are to be used.

    you could use an if() condition there to set display or not in a similar way to the width above, or make it more complicated with an array & array count them.

    hope this helps.

  • xhublist

    Thanks, but is not what i want .
    And is not possible look when sidebar is enableb ? Because i have one idea but i dn't know if is possible to make.

    if the all 4 sidebar are online count 1 else 0, create a division with 100 and use the result for assign the percent width.

    example topheader1 is online and count 1
    topheader2 no and count 0
    topheader3 no and count 2
    topheader4 no and count 0

    after count 100 / 2(the result of number of plugin online egual to 50% of width)

    <div class="top-bar">
    <div id="sidebar-topheader1" style="whidth:<?php echo $width ?>%;">
    <?php dynamic_sidebar( 'topheader1' ); ?>
    </div>
    <div class="top-bar">
    <div id="sidebar-topheader3" style="whidth:<?php echo $width ?>%;">
    <?php dynamic_sidebar( 'topheader3' ); ?>
    </div>

    in this mode i don't have to look where are published,.

    Anyone can help me?

  • xhublist

    i have solved by self, i have find much better solution, i post my solution if any have to use.
    php example code:

    <?php
    if ( is_active_sidebar( 'topheader1' ) ) {$count1 = '1';} else {$count1 = '0';};
    if ( is_active_sidebar( 'topheader2' ) ) {$count2 = '1';} else {$count2 = '0';};
    if ( is_active_sidebar( 'topheader3' ) ) {$count3 = '1';} else {$count3 = '0';};
    if ( is_active_sidebar( 'topheader4' ) ) {$count4 = '1';} else {$count4 = '0';};
    $totalcount = round($count1 + $count2 + $count3 + $count4);
    $width = round(100 / $totalcount);
    ?>

    html code

    <div class="top-bar">
    						<div id="sidebar-topheader1" class="sidebar" style="width:<?php echo $width; ?>%;">
    							<?php dynamic_sidebar( 'topheader1' ); ?>
    						</div>
    						<div id="sidebar-topheader2" class="sidebar" style="width:<?php echo $width; ?>%;">
    							<?php dynamic_sidebar( 'topheader2' ); ?>
    						</div>
    						<div id="sidebar-topheader3" class="sidebar" style="width:<?php echo $width; ?>%;">
    							<?php dynamic_sidebar( 'topheader3' ); ?>
    						</div>
    						<div id="sidebar-topheader4" class="sidebar" style="width:<?php echo $width; ?>%;">
    							<?php dynamic_sidebar( 'topheader4' ); ?>
    						</div>
    					</div>

    css code for show all sidebars in inline:

    div.top-bar div#sidebar-topheader1.sidebar, div.top-bar div#sidebar-topheader2.sidebar, div.top-bar div#sidebar-topheader3.sidebar, div.top-bar div#sidebar-topheader4.sidebar {
    position: relative;
    display: inline-block;
    vertical-align: middle;
    margin: auto auto;
    }
  • xhublist

    if exit division by zero warning this mod cut the error:

    <?php
    if ( is_active_sidebar( 'topheader1' ) ) {$count1 = '1';} else {$count1 = '0';};
    if ( is_active_sidebar( 'topheader2' ) ) {$count2 = '1';} else {$count2 = '0';};
    if ( is_active_sidebar( 'topheader3' ) ) {$count3 = '1';} else {$count3 = '0';};
    if ( is_active_sidebar( 'topheader4' ) ) {$count4 = '1';} else {$count4 = '0';};
    $totalcount = round($count1 + $count2 + $count3 + $count4);
    if ( $totalcount == 0 ){} else {$width = round(100 / $totalcount);}
    ?>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.