wordpress loop, applying something new every third post - php

I have this loop which basically displays a block on the page with an image in it, the class gallerypic has a margin of 20px on the right, it also has the rule float:left;, the issue is every time the third div is created it starts on a new line, because the margin is pushing it there. So Ideally every third post I would like no margin, and to apply a div gallerypicright or something.
Im wondering does someone have a solution to this? Possibly an easier one that simply stops the margin from happening when its the third one? I need the margin on the other two as it creates a neat gap between the posts.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$archive_query = new WP_Query('cat=14&showposts=14&paged=' . $paged);
$id = get_the_ID();
while ($archive_query->have_posts()) : $archive_query->the_post(); ?>
<div class="events">
<div class="gallerypic"><div class="limerickguideblockheader"><p><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?>
</div>
<div class="gallerypiccontainer"><a href="<?php the_permalink(); ?>" >
<?php echo get_the_post_thumbnail( $id, 'gallery-thumb', $attr ); ?> </a></div>
</div>
</div>
<?php endwhile; ?>
edit: a picture paints a 1000 words, here is the link so far, with three posts...
http://limerickfc.hailstormcommerce.com/cms/?page_id=2466
A method via CSS would be even better if possible.
Cheers
Adrian

Try the below code.
<style>
.gallerypicright {
margin: 0;
}
</style>
...
<?php
$count = 0;
while ($archive_query->have_posts()) : $archive_query->the_post();
$count++;
$third_div = ($count%3 == 0) ? 'gallerypicright' : '';
?>
...
<div class="gallerypic <?php echo $third_div; ?>">

If you want a pure CSS solution, you could try using
.gallerypic:nth-child(3n + 1) {
margin:0;
}
n is a counter that goes up for every element. The counter (n) starts at 0 but the elements on the page start at 1, so the 3n + 1 means for every 3 * n + 1 elements, e.g.:
element 1 (3 * 0 + 1), 4 (3 * 1 + 1), 7 (3 * 2 + 1) etc.
This solution is only available in CSS3, so older browsers won't have it. (see: http://caniuse.com/#search=nth-child).
Note that :nth-child counts all children, so you should group the events in a div:
<div class="container">
<div class="gallerypic">...</div>
<div class="gallerypic">...</div>
<div class="gallerypic">...</div>
</div>

you have to count your post after that in loop you have to module by 3 to total no of post & apply the right class in given post such as
if ($cnt%3 == 0){ $class = 'right'}

You might try the trick of adding a balancing negative right margin on your container, so in your case, perhaps
div.events { margin-right: -20px; }
Or, if you can cope with the slightly patchy browser support (IE9+ only, better in other browsers, I think) perhaps style using nth-child:
.gallerypic:nth-child(3n+3) { margin-right: 0px; }

Related

Checking biggest heigh from dynamic generated divs

I have a structure of divs that are generated based on php code. Each row has 3 divs. It is generated like this:
Defining columns:
<?php
$numOfCols = 3;
$rowCount = 0;
$bootstrapColWidth = 12 / $numOfCols;
?>
First row, starting the loop and defining width:
<div class="row">
#foreach($datas as $data)
<div class="col-md-<?php echo $bootstrapColWidth; ?>">
Then there's a bunch of content.
Then, in the end, each loop adds one to rowcount, and checks if the row has ended, so we can add a new one:
<?php
$rowCount++;
if($rowCount % $numOfCols == 0) echo '</div><div class="row">';
?>
#endforeach
Now, it all works great, the problem is that as I add different amounts of content on each div, the heights become different and ends up becoming quite ugly.
What I'm trying to do: I'm trying to check in each row which div has the biggest height, so I can apply the same height to the other two.
-I've thought of adding a id to each div that increments, problem is I don't know how many divs there will be, so I can't prepare for each outcome.
-I've tried selecting each row with $('row div'), problem is then all divs will become the same size as the biggest of all. And I want to check by row.
Sorry for the long question, and thanks for any input!
Bootstrap 4 has flexbox classes that allow you to do this with just css. Take a look at this example: https://getbootstrap.com/docs/4.3/utilities/flex/#align-self
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="d-flex">
<div class="align-self-stretch mr-3" style="background-color:lightblue">Aligned flex item</div>
<div class="align-self-stretch mr-3" style="background-color:lightblue">Aligned flex item<br />With<br />More<br />Lines</div>
<div class="align-self-stretch" style="background-color:lightblue">Aligned flex item</div>
</div>
I think flex-box is your solution : all into flex container will have the same height.
<div style="display:flex">
<div>
... some content
</div>
<div>
... some content
</div>
</div>
You can use .row-eq-height in Bootstrap 4

How to add css properties inside the PHP tag

I need one help.i need to restrict some css display property with PHP condition.I am explaining my code below.
<div style="width:24%; float:left; padding:10px;display:none;" id="compid">Select Company :
</div>
Here initially my div's css property display:none is there.But i need to block this using some php condition like below.
<?php if($getcustomerobj->companypro == 1 and $_REQUEST['edit']!=""){display:block}else{display:none} ?>
I need to put the above condition inside the style properties.Please help me how to do it proper way.
you can instanciate your display value before outputing :
<?php
$display = ($getcustomerobj->companypro == 1 && $_REQUEST['edit']!="") ? 'block' : 'none';
?>
<div style="width:24%; float:left; padding:10px;display:<?= $display ?>;" id="compid">Select Company :
</div>
If you want to mix php and html, you can use php tags inside html :
<p><?php echo $something ?></p>
or short tags if enabled on your server
<p><?= $something ?></p>
Do something like
<div style="width:24%; float:left; padding:10px;<?php if($getcustomerobj->companypro == 1 && $_REQUEST['edit']!=""){echo 'display:block;';}else{echo 'display:none;';}?>" id="compid">Select Company :</div>
add your php and echo in the style attribute
Hope it helps !
You just need to set a variable to the string you want to use as the CSS property.
Just for brevity, I tend to use ternaries for this sort of thing:
<?php $display = ($getcustomerobj->companypro == 1 and $_REQUEST['edit']!="") ? 'display:block;' : 'display:none;'; ?>
<div style="width:24%; float:left; padding:10px;<?php echo $display;?>" id="compid">Select Company :
</div>
Its not the tidiest solution, you would be better of using HTML classes and separating your CSS, but this should get the job done.

Code need to be repeated in php

I have this code:
<div class="big">
<div class="small">
<img src="images/icons/AC.png" />
AC & Refrigeration
</div>
<div class="small">
<img src="images/icons/AC.png" />
AC & Refrigeration
</div>
</div>
The code is such that the class='big' has to be repeated to show the icons enclosed in the class='small'.
the class-'small' is to display only 2 icons inside the div for class='big' once the two icons in the class='small' has displayed two icons, the div big has to be repeated. the same shall continue for about 80 icons.
please suggest how i can use a counter variable (e.g. one that begins with 1, increments to 2 and then resets to 1). Use the counter value to wrap two folder items (i.e. div class="small" ../div) within the div class="big"../div.
Provided an even number of icons, something like this would work:
<?php
$counter = 0;
foreach( $icons as $icon ) {
if ( $counter % 2 == 0 ) echo '<div class="big">';
?>
<div class="small">
<img src="<?= $icon->src ?>" />
<?= $icon->name ?>
</div>
<?php
if ( $counter % 2 == 1 ) echo '</div>';
$counter++;
}
?>
You'd have to add a little extra code to account for odd cases in necessary. The trick is to keep track of when you are on an even or odd icon with $counter % 2

Display PHP info in a table based layout

I am trying to display a 'meet the team' page in a 2x5 table, working with code written by the previous web designer in Wordpress.
This is the page I am looking at - http://www.stirling-house.com/about-us/meet-the-team As I say, I want to display the pictures in a table, so it looks neater.
The code I have is -
<div id="maintext">
<h1>MEET THE TEAM</h1>
<h3>THE STIRLING HOUSE ADMINISTRATION TEAM</h3>
<?php $members =get_posts('numberposts=99&cat=4&order=ASC'); //print_r($members);
foreach ($members as $post) {
setup_postdata($post);?>
<div class="member-box">
<img src="<?php echo get('member_photo');?>" alt="" style="margin:5px 7px 0 0;" />
<h3><?php echo $post->post_title;?></h3>
<h4><?php echo get('member_designation');?></h4>
Can anyone help?!
One way of doing it is by adding a counter before the foreach (for example, $n), initialize it to zero and increment it every time the loop finishes. Then, make all the odd divs float to the left and the even ones float to the right.
This is how the code would look like (more or less):
<?
$n = 0; // Counter
foreach($members as $post){
if($n % 2) $style="float:left";
else $style="float:right";
<img src="<?php echo get('member_photo');?>" alt="" style="margin:5px 7px 0 0;" />
<h3><?php echo $post->post_title;?></h3>
<h4><?php echo get('member_designation');?></h4>
If that does not work, you can try using a table layout and placing a <tr> tag after $n rows (resetting the counter every time you add a <tr>).
It is the opinion of most web designers (post-2005 or so) that <table> elements should not be used for layout/design. If you add the following CSS rule to your sites, stylesheet:
.member-box { display: inline-block; }
You will get something that looks like this: http://imgur.com/DzEkLAU
Sorry to answer indirectly, but I think this is the answer you're really looking for... ;)

Adjusting php code to accommodate a 2nd div

long time reader first time poster. I downloaded a theme for wordpress recently and i'm trying to edit it but am having difficulty. I know a bit of css but php is slightly out of my capability. So anyway I have 3 excepts of pages which show up on the main page. I would like to give them different background colours. I've managed to give the first two the same colour and the last a different one. I would like to give the middle one a different color as well, but i don't know how to. It seems that the div control is managed through php. Here is the php code:
<?php if ( get_option('chameleon_display_blurbs') == 'on' ){ ?>
<div id="services" class="clearfix">
<?php for ($i=1; $i <= 3; $i++) { ?>
<?php query_posts('page_id=' . get_pageId(html_entity_decode(get_option('chameleon_home_page_'.$i)))); while (have_posts()) : the_post(); ?>
<?php
global $more; $more = 0;
?>
<div class="service<?php if ( $i == 3 ) echo ' last'; ?>">
And here is the css code:
#services { margin-bottom: 40px; }
.service { float: left; width: 244px; margin-right: 66px; background:#000; } .last { margin-right: 0px; background:#0F0; }
So just to reiterate, I just want to be able to apply a different colour to the middle div.
website address:
Thank you and any help would be much appreciated!
I think the easiest way would be to change this line:
<div class="service<?php if ( $i == 3 ) echo ' last'; ?>">
to
<div class="service<?php if ( $i == 3 ) echo ' last'; ?> service-<?php echo $i; ?>">
That will give each div a different class (service-1, service-2, and service-3) which you can style separately.

Categories