I'm developing a WordPress site using Bootstrap. I want to get the first three posts in a category, then loop through them and put all three in a single row.
The trouble is that when a category has only one or two posts the layout is broken, because no markup for the div is written to the page.
<div class="row">
<?php
foreach(array_slice($posts_array, 0, 3) as $post):
?>
<div class="col col-12 col-lg-4">
<?php echo get_the_post_thumbnail(); ?>
<?php echo $post->post_title; ?>
</div>
<?php
endforeach;
?>
</div>
I need this to generate
<div class="row">
<div class="col col-12 col-lg-4">
<img src=image.jpg">
Title
</div>
<div class="col col-12 col-lg-4">
<img src=image2.jpg">
Second Title
</div>
<div class="col col-12 col-lg-4">
<img src=image3.jpg">
Third Title
</div>
</div>
But if a category only has two posts then I get
<div class="row">
<div class="col col-12 col-lg-4">
<img src=image.jpg">
Title
</div>
<div class="col col-12 col-lg-4">
<img src=image2.jpg">
Second Title
</div>
</div>
I'm sure this can't be too hard, but my brain doesn't seem to be working. Please help!
Count the array, try and see if $count % 3 == 0, if not, add fake array elements onto the end, and in your code, just check for something in the fake array that shows it is a real row or not, then you can display a blank box or whatever for the remaining elements.
The other way is starting $x = 1, and incremementing within the loop.
Try This:
<div class="row">
<?php
$val = 3;
$i = 1;
foreach(array_slice($posts_array, 0, $val) as $post):
?>
<div class="col col-12 col-lg-4">
<?php echo get_the_post_thumbnail(); ?>
<?php echo $post->post_title; ?>
</div>
<?php
$i++; endforeach;
?>
<?php for ($j = 0; $j < ($val - $i); $j++) { ?>
// Your Extra Divs.
<?php } ?>
Related
I have the following piece of code which essentially creates bootstrap 4 cards dynamically after obtaining some values from a db. The problem is however each new entry keeps getting stacked horizontally and I am struggling to write a piece of logic to tell the html to break a new line to stack cards once its more than 3 cards.
Here is what I have so far, really appreciate some guidance on it
<div class="row row-container">
<?php foreach ($data as $row) : ?>
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title">
<?php echo $row->title?></h5>
<p class="card-text"><?php echo $row->ID?></p>
View
</div>
</div>
</div>
<?php endforeach; ?>
</div>
My initial code that works fine other than the fact that it keeps stacking new elements horizontally.Essentailly to break a new row I need a new line of <div class="row row-container"> which is what I cant think of at the moment on how to get.
I tried adding the logic as below which would check using a count variable whether the number of items is divisible by 3 or not (it should break a new row if it is) but I dont know how to conttine
<div class="row row-container">
<?php $count=0; ?>
<?php foreach ($data as $row) : ?>
<?php
$count++
?>
<?php
if (($count % 3) ==0 ){
echo "<div class="row row-container">";
}
echo "$count" ?>
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title"><?php echo $row->title ?></h5>
<p class="card-text"><?php echo $row->description ?></p>
View
</div>
</div>
</div>
<?php endforeach; ?>
</div>
There are different ways of doing this, but if the sole purpose is to ensure that every 4th column goes to a new row, I would use array_chunk to achieve this:
<?php foreach (array_chunk($data, 3) as $row) : ?>
<div class="row row-container">
<?php foreach ($row as $col) : ?>
<div class="col">
<div class="card">
<div class="card-body">
<h5 class="card-title">
<?php echo $col->title?></h5>
<p class="card-text"><?php echo $col->ID?></p>
View
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
Here's a live demo for your quick reference.
The array_chunk function breaks an array into smaller chuncks. More details are available at php.net.
I have this page in Wordpress:
link
CODE PHP:
<?php
query_posts('cat=16');
$count = 0;
while (have_posts()) : the_post();
$count++;
if ($count == 1) { ?>
<div class="row">
<div class="col-lg-6 col-height" >
<div class="text-box-right">
<div class="title-home"><?php the_title(); ?></div>
<div class="content-home"><?php the_content(); ?></div>
</div>
</div>
<div class="col-lg-6 col-height" >
<div class="prod-box-left">
<?php the_post_thumbnail('news'); ?>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4"><div class="content-home"><?php the_content(); ?></div></div>
<div class="col-md-4">2</div>
<div class="col-md-4">3</div>
</div>
<?php } else { ?>
<?php $count = 0; ?>
<div class="row">
<div class="col-lg-6 col-height" >
<div class=" prod-box-right">
<?php the_post_thumbnail('news'); ?>
</div>
</div>
<div class="col-lg-6 col-height">
<div class="text-box-right">
<div class="title-home"><?php the_title(); ?></div>
<div class="conteont-home"><?php the_content(); ?></div>
</div>
</div>
</div>
<?php } ?>
<?php endwhile; ?>
The problem is that some items are displayed twice ... and I just want to be displayed at the bottom.
I put a picture to better understand what I mean.
What is wrong with my function?
Do you think you can help me solve this problem please?
Thanks in advance!
When your if statement is true for if ($count == 1) the block of code that is used has the function the_content() twice. I imagine this is why you're getting duplicate content. Remove one of those functions to eliminate the repetion.
you would normally see the latest 10 posts. If you want to show only 2 posts, you can use query_posts() like so:
<?php
query_posts( 'posts_per_page=2' );
?>
So you have use like this.
query_posts( 'cat=16&posts_per_page=2' );
For more reference, see the Wordpress function query_posts() documentation at following link -https://codex.wordpress.org/Function_Reference/query_posts.
I have the following problem: i need to create really simple layout, on each line i would like to have 3 box all of the same size, and if i understood right, in order to achieve this i need to construct a structure like the following:
<div class="row">
<div class=" news col-md-3 col-centered">
</div>
<div class=" news col-md-3 col-centered">
</div>
<div class=" news col-md-3 col-centered">
</div>
</div>
This is my php script in the index.php:
<?php while(have_posts()) : the_post();?>
<div class="row">
<div class=" news col-md-3 col-centered">
<h4><?php the_title();?></h4>
<p><?php the_excerpt(); ?> </p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
With this code, each box get a <div class="row"> element like the following:
<div class="row">
<div class=" news col-md-3 col-centered">
</div>
</div>
...
How can i fix this?
This is what i get now: if a box as more height than another, it leave the space under it without any element.
the height of the box depends on the content. what i would like to have is something like this:
Just move the row outside of The Loop, so that it's not repeated:
<div class="row">
<?php while(have_posts()) : the_post(); ?>
<div class="news col-md-4 col-centered">
<h4><?php the_title(); ?></h4>
<p><?php the_excerpt(); ?> </p>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
Also, you need to change your column width to col-md-4, since Bootstrap uses a 12-column grid, and you want 3 columns per row.
If you need to actually close out each row after 3 columns are shown, you need to use a counter:
<div class="row">
<?php $i = 1; while(have_posts()) : the_post();?>
<div class="news col-md-3 col-centered">
<h4><?php the_title();?></h4>
<p><?php the_excerpt(); ?> </p>
</div>
<?php if ( $i % 3 === 0 ) { echo '</div><div class="row">'; } ?>
<?php $i++; endwhile; wp_reset_query(); ?>
</div>
This last example will ensure that all floats are cleared, and that each row of elements are on their own lines.
I've added to my single-post.php a code to display the post categories above the title, it looks good here:
http://thenoirportrait.com/2014/08/23/review-chanel-perfection-lumiere-velvet/
But not here:
http://thenoirportrait.com/2015/03/04/interior-design-black-home/
I have different post templates. What can I do to make the category change it's position with every post?
I think I should put my category code in the Archives.php but I'm not sure where. I don't want to mess up my code, any help is welcome.
This is what I have at the moment:
<div class="row<?php echo (($behind_title_fw) ? ' full-width' : ''); ?>">
<div class="inner_content">
<div class="row<?php echo (($has_sidebar) ? ' has-sidebar' : ''); ?>">
<div itemscope="" itemtype="http://schema.org/BlogPosting">
<?php if($sidebar_below_title && !$is_endless_template) : ?>
<div class="medium-12">
<?php foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
<br>
<br>
<article id="post-header-<?php the_ID(); ?>" class="article-header-above" data-postid="<?php echo esc_attr($post->ID); ?>" data-guid="<?php echo esc_attr(get_the_guid($post->ID)); ?>" data-permalink="<?php echo esc_url(get_permalink($post->ID)); ?>">
Thank you
The first post's class has a width of 12:
<div class="row">
<div class="inner_content">
<div class="row">
<div itemscope="" itemtype="http://schema.org/BlogPosting">
REVIEWS <br><br>
<div class="medium-12 column">
...
But the second one, a width of 8:
<div class="row">
<div class="inner_content">
<div class="row">
<div itemscope="" itemtype="http://schema.org/BlogPosting">
INTERIOR DESIGN<br><br>
<div class="medium-8 column">
...
The key to align the second category (or whatever) is by putting it inside the div which defines the column size. Therefore, the category will be always center-aligned with the post:
<div class="row">
<div class="inner_content">
<div class="row">
<div itemscope="" itemtype="http://schema.org/BlogPosting">
<div class="medium-8 column">
INTERIOR DESIGN<br><br>
...
I am using database for retrieving products.
I want to display product in following pattern.
What can be the php Or .net loop code for this ? Help.
<div class="big-row">
<div class="first-row-left">
<div class="product-1a">Product1</div>
<div class="product-1a">Product2</div>
<div class="product-1a">Product3</div>
<div class="product-1b">Product4</div>
<div class="product-1b">Product5</div>
<div class="product-1b">Product6</div>
</div>
<div class="first-row-right">
<div class="bigproduct-right">Product7</div>
</div>
</div>
<div class="single-row">
<div class="product-single-row">Product8</div>
<div class="product-single-row">Product9</div>
<div class="product-single-row">Product10</div>
<div class="product-single-row">Product11</div>
<div class="product-single-row">Product12</div>
</div>
<div class="big-row">
<div class="first-row-left">
<?php
for($i = 0, $intSize = sizeof($arr); $i < $intSize ; $i++){
?> <div class="product-1a"><?php echo $arr['product']; ?></div>
<?php
}
?>
</div>
A small example of for loop