I have a set of case studies which vary in size. Like so:
| Large | Large |
|Small | Small | Small |
| Large | Large |
...
I'm trying to run a loop which will check if the case study is the first or second result pulled, if so, use .case-card--large CSS, else use the default .case-card CSS.
I'm unsure on how to use select classes if the loop condition is true?
In plain English: if first two results, use .case-card--large, then for the next three use .case-card, then again, use .case-card--large for the next two and so on.
Here's my loop so far (incomplete):
<?php
$counter = 1;
$loop = new WP_Query( array( 'post_type' => 'case-studies') );
while ( $loop->have_posts() ) : $loop->the_post();
if ($counter % 3 == 1){
echo 'alpha';
} else {
echo 'omega';
}
$counter++ ;
endwhile
?>
Output: alpha omega omega alpha omega, when I need it to be alpha alpha omega omega omega.
The div and CSS:
.case-card--large {
width: 50%;
border: 1px solid red;
}
.case-card {
text-align: center;
padding: 40px;
width: 33%;
}
<div class="case-card case-card--large" <?php echo tp_bg_image($background, 'full' ); ?>
<div class="wrapper">
<p>ID:
<?php the_ID(); ?>
</p>
<h1>
<?php echo $heading; ?>
</h1>
<p>
<?php echo $subheading; ?>
</p>
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#case-study-video">
Click me
</button>
</div>
</div>
Is a loop the right away to go about this?
Related
I am retrievieng from a query 4 rows.
This rows recover the id (to set the href), the image path and the title.
For example the code for the first row is:
<div class="row">
<div class="col">
<a href="id">
<img src="file">
<p class="titulo-noticia">The title</p>
</a>
</div>
<div class="col">
<a href="id">
<img src="file">
<p class="titulo-noticia">Another title</p>
</a>
</div>
</div>
I achieve this using:
echo "<div class='table'>"
for($i=0;$i<2;$i++){
echo "<div class='row'>"
for($j=0;$j<2;$j++){
echo "<div class='col'>"
echo "<a href='id'>
<img src='file'>
<p class='titulo-noticia'>".$title."</p>
</a>"
echo "</div>"
} //end col loop
echo "</div>"
} //end row loop
echo "</div>"
CSS is:
.col{
display: table-cell;
}
.row{
display: row;
}
.table{
display: table;
}
Is this the correct way to show a 2*2 matrix of php content? Or there is a better way using css?
Thank you.
I don't think there's really a best way, but here's another way with CSS. Basically just echo out all your query results into one container div without worrying about rows. Display each result in a div with float: left and width: 50% and they'll make their own rows.
<div class="matrix">
<?php while ($row = however_youre_fetching_them()): ?>
<div class="cell">
<a href="<?= $row['id'] ?>">
<img src="<?= $row['path'] ?>">
<p><?= $row['title'] ?></p>
</a>
</div>
<?php endwhile ?>
</div>
And the CSS
.cell {
height: 100px;
width: 50%;
float: left;
}
Setting a constant height on the cells should ensure that the rows line up properly.
You over-complicating this.
You need to do it by chunks, array_chunk() perfect for that:
foreach(array_chunk($array, 2) as $chunks){
//<div class="row">
foreach($chunk as $chunk){
//<div class="col"></div>
}
//</div>
}
I have a portfolio listing page that loops though the posts like this:
<div class="row">
<!-- Loop though projects -->
<?php
$args = array('post_type' => 'project');
$the_query = new WP_Query( $args );
?>
<?php
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); ?>
<a href="<?php the_permalink(); ?>">
<div class="col-sm-6 project-entry">
<!--Add thumbnails to potfolio items and resize for responsive-->
<?php
$thumbnail_id = get_post_thumbnail_id();
$thumbnail_url = wp_get_attachment_image_src( $thumbnail_id, 'thumbnail-size', true);
?>
<!--Link images and titles to individual portfolio page -->
<p>
<img src="<?php echo $thumbnail_url[0]; ?>" alt="<?php the_title();?> project image">
</p>
<div class="project-text">
<h4>
<?php the_title(); ?> | <?php the_field('project_type_name_'); ?>
</h4>
<p><?php the_field('brief_description_'); ?></p>
</div>
</div>
</a>
I need to style EVERY first, second and third post. I presume the best way to do this is to add a class to each portfolio item.
So add need to add:
class-one to the first, fourth and so on.
class-two to the second, fifth and so on.
class-three to the third, sixth and so on.
Is this the best way and how do I do this?
Thank you.
EDIT: Let me explain a little more. I have a portfolio listing page, that links to single post pages.
Each listing is an image (project-entry), when you scroll over the image, text appears (project-text) on top of a background image, (on top of the original image). I want the hover image to be the same for every 1st, 4th, 7th item, a different one for every 2nd, 5th and 8th item, and another different one for every 3rd, 6th and 9th item etc etc.
Here is the CSS in use, (so I'm trying to change this part:
background: url('img/project-dark-grey.png');
.project-entry img {
width: 100%;
margin: 30px 0;
position: relative;
}
.project-text {
position: absolute;
z-index: 1;
top: 7%;
color: #fff;
margin-right: 13px;
padding: 24% 0;
background: url('img/project-dark-grey.png');
height: 381px;
visibility: hidden;
}
.project-entry:hover .project-text {
visibility: visible;
}
I need to style EVERY first, second and third post.
You can use nth-child
ul li:nth-child(3n+1) {
color: green;
}
ul li:nth-child(3n+2) {
color: blue;
}
ul li:nth-child(3n+0) {
color: red;
}
<ul>
<li>Item
</li>
<li>Item
</li>
<li>Item
</li>
<li>Item
</li>
<li>Item
</li>
<li>Item
</li>
<li>Item
</li>
<li>Item
</li>
<li>Item
</li>
<li>Item
</li>
<li>Item
</li>
</ul>
Just use nth-child selector.
p:nth-child(3n) {
background: #ff0000;
}
p:nth-child(3n+1) {
background: #ff0000;
}
p:nth-child(3n+2) {
background: #ff0000;
}
You can also use nth-of-type
li:nth-of-type(3n+0){
/* CSS Here */
}
li:nth-of-type(3n+1){
/* CSS Here */
}
li:nth-of-type(3n+2){
/* CSS Here */
}
Try this take a class with classes name set to indexes 0 1 2 as
$classes_arr = array('class-one','class-two','class-three');
Increment the counter $counter and $arr_key inside the Loop
$counter = 0;
$arr_key = 0;
So the over all code should be
$classes_arr = array('class-one','class-two','class-three');
$counter = 0;
$arr_key = 0;
if (have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
$counter++;
echo '<div class="col-sm-6 project-entry '.$classes_arr[$arr_key].' ">';
$arr_key++;
if($counter%3 == 0){
$arr_key = 0
}
endwhile;
endif;
So it was kind of nth-child, but I needed to use .row. Thank you to everyone who responded, I really appreciate it.
Here's what worked in the end:
.project-text {
/* common styles for all */
background: url('path/to/bg1.png');
}
/* selects .project-text within links 2, 5, 8,... */
/* Switch to background-image here in case you need to add more background properties to the earlier rule */
/* If you use the shorthand again here to only change the background image then you will lose any other previously set properties */
.row a:nth-child(3n + 2) .project-text {
background-image: url('path/to/bg2.png');
}
/* selects .project-text within links 3, 6, 9,... */
.row a:nth-child(3n + 3) .project-text { /* same as 3n */
background-image: url('path/to/bg3.png');
}
I am pulling two different variables from the database. A name, and an image path. I want to display this on the page to look like this:
|Image 1|Image 2|Image 3|
|Name 1 |Name 2 |Name 3 |
I currently have this code:
<?php do { ?>
<img src="<?php echo $row_UserInfo['image_path'];?>" width="150" height="150"/>
<a href="HorseProfile.php?recordID=<?php echo $row_UserInfo['id']; ?>"style="color:#000000; text-decoration: none; text-align: center;">
<?php echo $row_UserInfo['Name']?></a>
<?php } while ($row_UserInfo = mysql_fetch_assoc($UserInfo)); ?>
I tried to add a break after the image so that the name goes right under it, but then when the next image appears, it goes next to the name before. I know this is probably extremely simple, but this has been a problem for me for a while and if someone could open my eyes and show me what I'm doing wrong, that would be deeply appreciated.
You should enclose your image and link in a div, and then float the div. Here is an example:
<?php do { ?>
<div style="float:left;">
<div>
<img src="<?php echo $row_UserInfo['image_path'];?>" width="150" height="150"/>
</div>
<div>
<a href="HorseProfile.php?recordID=<?php echo $row_UserInfo['id']; ?>"style="color:#000000; text-decoration: none; text-align: center;">
<?php echo $row_UserInfo['Name']?>
</a>
</div>
</div>
<?php } while ($row_UserInfo = mysql_fetch_assoc($UserInfo)); ?>
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.
I have the code below to split my for each statement into two separate divs:
<?php
$previousLetter = false;
?>
<?php
$i=1; // have a counter variable
foreach($this->clubs as $clubs) : ?>
<?php
$firstLetter = substr($clubs->club_name, 0, 1);
if ($firstLetter != $previousLetter) {
if($i==1){
echo "<div class="left_class">"; // open left div
}
?>
<h3 id="club-link-header"><u><?php echo $firstLetter; ?></u></h3>
<?php } ?>
<a id="club-link" href="<?php echo $this->url(array('controller' => 'club-description', 'action' => 'index', 'club_id' => $clubs->id));?>"><br />
<?php echo $this->escape($clubs->club_name);?></a>
<?php $previousLetter = $firstLetter; ?>
<?php
if($i==25){
echo "</div>"; //close left div
echo "<div class="right_class">"; // open right div
}
if($i==50){
echo "</div>"; //close right div
}
$i++; // increment the counter variable for each loop
endforeach;
?>
The HTML:
<body>
<div id="top">
<a id="admin-link" href="/MN/public/index.php/admin/index/id/1"></a>
<div id="logged-in-as">
Hello! ric89. Logout </div>
</div>
<div id="header">
<div id="header-wrapper">
<div id="social">
<a id="fb" href="#"><img src="/MN/public/images/fb.png" /></a>
<a id="twitter" href="#"><img src="/MN/public/images/twitter.png" /></a>
</div>
<div id="nav">
<div id="nav-left">
Home
</div>
<div id="nav-middle">
<a id="clubs-link" href="/MN/public/index.php/clubs/index/id/1">Clubs</a>
</div>
<div id="nav-right">
<a id="admin-link" href="/MN/public/index.php/admin/index/id/1">Admin</a>
</div>
</div>
<div id="logo">
</div>
</div>
</div>
<div id="content">
<h1>Clubs</h1>
//database content is echo'd here, 50 items like this:
<h3 id="club-link-header"><u>5</u></h3>
<a id="club-link" href="/MN/public/index.php/club-description/index/id/1/club_id/1"><br />
5th Avenue</a>
</div>
<div id="footer">
<p id="footer-text">created & designed by Richard Knight-Gregson</p>
</div>
</body>
The CSS:
/*Content styles */
.clubs-left {
width: 450px;
}
.clubs-right {
float: right;
margin-left: 450px;
position: absolute;
width: 450px;
}
.right_class {
float: right;
width: 450px;
}
.left_class {
position: absolute;
width: 450px;
}
.clear {
clear: both;
}
Here is an image of the problem -> http://imageshack.us/photo/my-images/84/screenshot20120426at211.png/ The footer should be 100% width.
The issue is I can't float the div right without breaking the layout as the right div needs to be on top of the left in the code but doing so will break the PHP.
Any suggestions?
Thanks
Since the problem that you are describing seams to be purely cosmetic, I believe that you need to clear the float to allow the document to continue its normal rendering:
After your <div class="right_class">...</div>:
HTML
<div class="clear"></div>
CSS
.clear {clear: both;}
wouldn't something like this be more efficient?
Break up your list by alphabet to start...
var glossary=array();
var $half=(int)count($this=>clubs);
var $count=0;
foreach ($this->clubs as $clubs){
$glossary[substr($clubs->club_name, 0, 1)][] = $clubs;
}
# Start a definition list. (http://www.w3schools.com/tags/tag_dl.asp)
echo'<dl>';
foreach ($glossary as $letter => $clubs){
$count++;
$class=($count>=$half)?'float_left':'float_right';
# list all the clbs broken down by letter
echo"<dt class='$class'>$letter</dt>";
foreach ($clubs as $club)
{
echo"<dd class='$class'>
<a id='club-link' href='" . $this->url(array('controller' => 'club-description', 'action' => 'index', 'club_id' => $club->id)) . "'>
" . $this->escape($clubs->club_name) .
"</a>
</dd>";
}
}
echo '</dl>';
and the css:
dl{
width:100%;
}
.float_left{
width:49%;
float:left;
clear:left;
}
.float_right{
width:49%;
float:right;
clear:right;
}
this way after you hit the halfway point in clubs, the dt and dd elements stack on the right side, and the list will automatically balance, no matter how many clubs you have.
It turns out the footer was inside the content div and I hadn't closed the content div properly one more extra before the footer starts was all it needed! Sorry to waste your time..