I want two column related thumbnail like this screenshotI Want To create two-column thumbnail related posts like in screenshot I have linked to:
This is the code so far:
<!---Related Posts --->
<div class="singlerelated">
<div class="headingbig"><div class="shortcode-unorderedlist fa fa-hand-o-right"> Lees meer over <span class="headingorange"><?php the_category(', ') ?></span></div>
</div>
<div class="relatedentry">
<?php
$orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category)
$category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 8,
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
while($my_query->have_posts()) {
$my_query->the_post();
?>
<ul>
<li class="even">
<?php the_post_thumbnail(array(100,100), array('class' => 'relatedthumb')); ?>
<p class="title">
<?php the_title(); ?>
</p>
</li>
</ul>
<?
}
}
$post = $orig_post;
wp_reset_query();
?>
</div>
</div>
<!---Related Posts --->
I want it to be displayed in 2 columns, By Creating li class either even or odd generated alternatively so I can put even class in left side and other in right side. Please Help me.
Try using an $i auto-increment value in your loop. You probably want to do a for with a count and round up incase you have an odd number of posts. May not put an end </ul> on it if you don't do a count. This is the idea though:
$i = 1;
while($my_query->have_posts()) {
$my_query->the_post();
// If $i equals 1, start the <ul>
if($i == 1)
echo '<ul>'.PHP_EOL;
?> <li class="even">
<?php the_post_thumbnail(array(100,100), array('class' => 'relatedthumb')); ?>
<p class="title">
<?php the_title(); ?>
</p>
</li>
<?php
// end on the second loop
if($i == 2) {
echo '</ul>'.PHP_EOL;
// Reset the count back to 0 so it starts
// back at 1 after incrementing
$i = 0;
}
$i++;
}
EDIT To do even and odd, you can use a modulus calculation:
for($i = 0; $i < 10; $i++)
echo (($i % 2) == 0)? 'even:'.$i.'<br />' : 'odd: '.$i.'<br />';
Related
I created a database with 5 categories and created a page to display the available categories that I pull from the database. The problem is that I have 4 divs that have the following class ( col-md-3 ) and one that is ( col-md-6 ).
Code for gathering the first 5 categories ( col-md-3 ):
<?php
$this->db->limit(4);
$categories = $this->db->get_where('category', array('parent' => 0))->result_array();
foreach ($categories as $key => $category):
?>
<div class="gallery-item">
<div class="grid-item-holder">
<div class="listing-item-grid">
<img src="<?php echo base_url('uploads/category_thumbnails/').$category['thumbnail'];?>" alt="" />
<div class="listing-counter">
<span>2</span>Locations
</div>
<div class="listing-item-cat">
<h3><?php echo $category['name']; ?></h3>
<p><?php echo $category['name']; ?></p>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
Code for gathering another 1 category ( col-md-6 ):
<?php
$this->db->limit(1);
$categories = $this->db->get_where('category', array('parent' => 0))->result_array();
foreach ($categories as $key => $category):
?>
<div class="gallery-item gallery-item-second">
<div class="grid-item-holder">
<div class="listing-item-grid">
<img src="<?php echo base_url('uploads/category_thumbnails/').$category['thumbnail'];?>" alt="" />
<div class="listing-counter">
<span>2</span>Locations
</div>
<div class="listing-item-cat">
<h3><?php echo $category['name']; ?></h3>
<p><?php echo $category['name']; ?></p>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
With this code, I get a display of categories but instead of displaying 5 categories, it repeats one of the displayed 4. How can I fix the code so that the display automatically continues to display the categories in all divs? If I forgot to write something, correct it or ask, I will update the question.
It looks like you are getting the first 4 items for the first foreach loop and then getting the first one again for the second loop. Would it make sense to get all 5 and process them in one single foreach loop only adding 'gallery-item-second' on the 5th item, something like ...
<?php
$this->db->limit(5);
$categories = $this->db->get_where('category', array('parent' => 0))->result_array();
$index = 0;
foreach ($categories as $key => $category):
$index++;
if ($index == 5) {
echo '<div class="gallery-item gallery-item-second">';
} else {
echo '<div class="gallery-item">';
}
?>
<div class="grid-item-holder">
. . .
</div>
</div>
<?php endforeach; ?>
Or if you must have the second loop then get 5 items and only show the 5th.
And to make sure you get the same 5 you could use a sort criteria when you get the items.
The problem is resolved with math:
I have added the next code:
...
$num = 5;
$i = 0;
foreach ($categories as $key => $category):
if ($i < ($num - 1)) {
// show div col-md-3
} else {
// show div col-md-6
}
endif;
I'm working on WordPress custom fields and I need to write PHP if-else conditions in a loop whether it is the first child or last child of the ul list. but I don't know how to apply, kindly please help me to solve it
Here is my code below
<?php if( have_rows('chart') ): ?>
<ul class="list-unstyled">
<?php while( have_rows('chart') ): the_row();
$sr_no = get_sub_field('sr_no');
$list_content = get_sub_field('list_content');
?>
<li><div><span class="w-num"><?php echo $sr_no; ?></span><?php echo $list_content; ?></div><span class="chart-divider"><span class="chart-divider-r"></span></span></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
Count the entries for rows.
Set up a counter, and every time you iterate increase.
<?php if( have_rows('chart') ): ?>
<ul class="list-unstyled">
<?php
$counter = 1;
$total-row = // Figure out how to count(rows), or something like that
while( have_rows('chart') ): the_row();
$sr_no = get_sub_field('sr_no');
$list_content = get_sub_field('list_content');
if($counter == 1){
// First Row
} elseif ($counter == $total-row){
// Last Row
}
?>
<li><div><span class="w-num"><?php echo $sr_no; ?></span><?php echo $list_content; ?></div><span class="chart-divider"><span class="chart-divider-r"></span></span></li>
<?php
$counter++;
endwhile; ?>
</ul>
<?php endif; ?>
Please try with below:
The result you will see in the li as a class
If fist li, you will see the first_class
If last li, you will see the last_class
Else only some_class
<?php
$Total_charts_count = wp_count_posts( 'chart' )->publish; //Total count of charts
$chart_number = 1; //Loop starting point.
if( have_rows('chart') ): ?>
<ul class="list-unstyled">
<?php
while( have_rows('chart') ): the_row();
$sr_no = get_sub_field('sr_no');
$list_content = get_sub_field('list_content');
// First item
if($chart_number == 1) {
$class = 'some_class first_class';
// Last item
} else if ($chart_number == $Total_charts_count) {
$class = 'some_class last_class';
// Not first / Not last item
} else {
$class = 'some_class';
}
?>
<li class="<?php echo $class; ?>">
<div>
<span class="w-num"><?php echo $sr_no; ?></span>
<?php echo $list_content; ?>
</div>
<span class="chart-divider">
<span class="chart-divider-r"></span>
</span>
</li>
<?php
$chart_number++;
endwhile; ?>
</ul>
<?php endif;
?>
Hope it's work for you.
So basically I am building a wordpress gallery, I need to have the images output into 2 rows, each row is 3 images across so essentially I am just splitting the output every 3..
I have that functionality working with a for loop using the modulus operator, what I need is that every 2 rows I need to have them wrapped in an outer HTML container
<div class="wrappy">
but I cant seem to get this quite right having worked on it most of yesterday, Im not that great at PHP but I understand enough to get by so any help at all would be great... I am using wordpress advanced custom fields for storing the images etc so you can ignore that part, the magic Im working on starts at
<div class="wrappy">
<?php
$rows = get_field('staff_slides', 'options');
$countmax = count($rows) - 1;
//echo $row_count;
$ender = "";
$mainEnder = "";
$outer_wrapper = "";
if( have_rows('staff_slides', 'options') ):
// loop through the rows of data
while ( have_rows('staff_slides', 'options') ) : the_row();
$image_id = get_sub_field('slide_image', 'options');
$staff_members_name = get_sub_field('staff_members_name', 'options');
$staff_members_position = get_sub_field('staff_members_position', 'options');
$staff_image = wp_get_attachment_image_src( $image_id , 'homepage-staff');
?>
<?php
echo '<div class="wrappy">';
echo '<div class="row">';
if( $ender != "script-ended" ) {
for( $i=0; $i <= $countmax; )
{
?>
<div class="staff-img c3">
<div class="staff-caption">
<h3><?php echo $rows[$i]['staff_members_name']; ?></h3>
<span></span>
<h4><?php echo $rows[$i]['staff_members_position']; ?></h4>
</div>
<img src="<?php echo wp_get_attachment_image_src( $rows[$i]['slide_image'], 'homepage-staff')[0]; ?>">
</div>
<?php
if( $i % 3 == 2 )
{
echo '</div><div class="row">';
}
if( $i == $countmax )
{
$ender = "script-ended";
}
if( $i == 6){
$outer_wrapper = "set";
}
$i++;
}
}
?>
<?php
if( $outer_wrapper == "set" ){
echo '</div><div class="wrappy">';
}
?>
You should use the array_chunk function to split the array in smaller arrays.
I'm not familiar with the ACF functions but you can do something like:
<div class="wrappy">
<?php
if( have_rows('staff_slides', 'options') ) :
$slides = array_chunk(get_field('staff_slides', 'options'), 3);
foreach ($slides as $slides_row) :
?>
<div class="row">
<?php foreach ($slides_row as $slide_element) : the_row(); ?>
<div class="staff-img c3">
<div class="staff-caption">
<h3><?php the_sub_field('staff_members_name', 'options'); ?></h3>
<span></span>
<h4><?php the_sub_field('staff_members_position', 'options'); ?></h4>
</div>
<img src="<?php echo wp_get_attachment_image_src( get_sub_field('slide_image', 'options'), 'homepage-staff')[0]; ?>">
</div>
<?php endforeach; ?>
</div>
<?php endforeach; else : ?>
No slides do to show
<?php endif; ?>
</div>
This way you create a multidimensional array, divided in chunks of 3 elements, and iterate throught it.
In my version of Bootstrap there are 12 columns in a row.
In the below for loop each loop prints 2 columns wide.
What I want is that before the first column is printed (or $i === 0)<div class="row"> is printed.
Then after the number of columns is equal to 12 (or the variable $i is no longer less than 6) for the closing row tag to be printed (</div><!--row-->) and then for the variable $i to be reset to zero.
I have achieved the layout I desire but the problem is that Wordpress is looping infinitely and retrieving the logo of the same company hundreds of times.
<?php if (have_posts() ) : while (have_posts() ) : the_post(); ?>
<?php for ($i = 0; $i < 6; $i++) : ?>
<?php if ($i === 0) : ?>
<div class="row">
<?php endif; ?>
<div class="col-md-2">
<?php $image = get_field('sponsor_logo'); ?>
<a href="<?php the_permalink();?>">
<img class="img-responsive" src="<?php echo $image['sizes']['thumbnail-soft-crop'];?>" alt="<?php echo $image['alt']; ?>"/>
</a>
</div><!--col-md-2-->
<?php if ($i === 5) : ?>
</div><!--row-->
<?php $i = 0;
endif;
endfor; ?>
<?php endwhile; else : ?>
You are redeclaring $i = 0; at the bottom so when the loop reaches bottom after increment it is again reset to 0 and hence running again and again.
If i am right i think you want to start a new row after number of iterations you can try this with some modification according to your requirement
<?php if (have_posts()) : while (have_posts()) : the_post();
$i = 0;
if ($i!=0 && $i%3==0){ // add new row after 3 posts
echo "</div><div class='row'>";
// close previous row after 3 elements and start new.
}
$i++;
?>
Solution for Query in question.
<div class="row">
<?php if (have_posts()) : while (have_posts()) : the_post();
$i = 0;
if ($i != 0 && $i % 5 == 0) { // add new row after 5 posts
echo "</div><div class='row'>";
}
$i++;
?>
<div class="col-md-2">
<?php $image = get_field('sponsor_logo'); ?>
<a href="<?php the_permalink(); ?>">
<img class="img-responsive" src="<?php echo $image['sizes']['thumbnail-soft-crop']; ?>"
alt="<?php echo $image['alt']; ?>"/>
</a>
</div>
<?php endwhile; endif; ?>
</div>
I have this code, and I need to close a row every 4 post. Every post is inside a div. I tried some things but I coudn't implement to my code.
<?php
echo "<div class='row'>";
global $post;
$all_events = tribe_get_events(
array(
'eventDisplay'=>'upcoming',
//'posts_per_page'=>10,
)
);
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="col-sm-3">
<span class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'j M'); ?></span>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) { ?>
<div class="event-thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="event-excerpt">
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="event-content">
<?php the_content(); ?>
</div>
<?php } ?>
</div>
<?php } //endforeach ?>
<?php wp_reset_query(); ?>
<?php
echo "<div class='row'>";
global $post;
$all_events = tribe_get_events(
array(
'eventDisplay'=>'upcoming',
//'posts_per_page'=>10,
)
);
$count = 1;
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="col-sm-3">
<span class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'j M'); ?></span>
<h3><?php the_title(); ?></h3>
<?php if ( has_post_thumbnail() ) { ?>
<div class="event-thumb">
<?php the_post_thumbnail('thumbnail'); ?>
</div>
<div class="event-excerpt">
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="event-content">
<?php the_content(); ?>
</div>
<?php } ?>
</div>
<?php
if($count == 4){
echo "<div class='seperator'></div>";
$count =1;
}
?>
<?php $count++; } //endforeach ?>
<?php wp_reset_query(); ?>
I'd actually solve this 100% in CSS, so you don't need any counting or handling inside your PHP code.
Have a look at this JSFiddle.
float: left will cause the single elements to all follow each other (left aligned).
clear: left on every 4 * n + 1-th element (nth-child(4n+1)) will clear this, essentially forcing a line break.
There is one caveat to this: If there's no room for all 4 entries in one row, you'll end up with additional wrapping, which can be avoided by defining a fixed width for the container.
A simplified in-code version for PHP would just count the fields written and add a line break as necessary:
$i = 1; // counter
foreach ($events as $event) { // iterate over all events
if ($i++ % 4 == 0) // a % b will be 0 for 4, 8, etc.
echo '<br />'; // print the line break using whatever HTML you see fit.
print_event($event); // print the actual event
}
You might ask whether I check for the line break before actually printing an event: That's to prevent additional line breaks if the number of entries is a multiple of 4, i.e. I avoid having an empty trailing line.
You need the modulo operator. It works like this:
$i == 0;
foreach($some_array as $some_value){
if ($i % $number_to_divide_by == 0) {
// do something here every nth time
}
$i++;
}