i need to create something like this picture
Picture
How to put three posts in each Wordpress loop? with different classes?
Within each loop, put three posts.
PHP
$args = array(
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12,
'ignore_sticky_posts' => false,
'no_found_rows' => false,
'paged' => 1
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) { ?>
<div class="owl-carousel">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="row">
<div class="col-md-6">
<?php the_post_thumbnail( 've' ); ?> <!-- Vertical Image Size : 300*600 px (width/height) -->
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<?php the_post_thumbnail( 'ho' ); ?> <!-- Horizontal Image Size : 600*300 px (width/height) -->
</div>
<div class="col-md-12">
<?php the_post_thumbnail( 'ho' ); ?> <!-- Horizontal Image Size : 600*300 px (width/height) -->
</div>
</div>
</div>
</div>
<?php
endwhile;
?> </div> <!-- / .owl-carousel --> <?php
}
I am using OWL Carousel and i Need to put three posts in each Slide,
HTML
<div class="owl-carousel">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="row">
<div class="col-md-6">
<img src="Vertical" alt="">
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<img src="Horizontal" alt="">
</div>
<div class="col-md-12">
<img src="Horizontal" alt="">
</div>
</div>
</div>
</div>
<?php endwhile; ?>
any Suggestion, any Ideas
Thanks a lot!
I would use a counting variable and use that with some if statements to determine output:
$args = array(
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12,
'ignore_sticky_posts' => false,
'no_found_rows' => false,
'paged' => 1
);
$the_query = new WP_Query( $args );
$i = 1;
if ( $the_query->have_posts() ) { ?>
<div class="owl-carousel">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="post-<?php if ($i > 1) {echo "right";} else { echo "left"; } ?>">
<?php the_post_thumbnail(); ?>
</div>
<?php $i++; ?>
<?php endwhile;?>
</div>
<?php endif; ?>
I think you don't put three post in each lopp but you simply manage them with counter:
$args = array(
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 12,
'ignore_sticky_posts' => false,
'no_found_rows' => false,
'paged' => 1
);
$the_query = new WP_Query( $args );
$cont = 0;
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) : $the_query->the_post();
$supp = $cont % 3;
switch($supp)
{
case 0: $vert = get_the_post_thumbnail_url(); break;
case 1: $oriz1 = get_the_post_thumbnail_url(); break;
case 2: $oriz2 = get_the_post_thumbnail_url(); break;
}
$cont ++;
endwhile;
}
After you print html
<div class="row">
<div class="col-md-6">
<img src="<?php echo $vert; ?>" alt="">
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<img src="<?php echo $oriz1; ?>" alt="">
</div>
<div class="col-md-12">
<img src="<?php echo $oriz2; ?>" alt="">
</div>
</div>
</div>
</div>
First sort all posts by image type in another array like this:
<?php
$counter = 0;
$index = 0;
$all_posts = array();
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ($counter != 3) {
$all_posts[$index][] = get_the_post_thumbnail_url();
$counter ++;
} else {
$index ++;
$counter = $index;
$all_posts[$index][] = get_the_post_thumbnail_url();
}
}
After this, You can foreach new array and set images by size
<?php foreach ($all_posts as $key => $current_posts): ?>
<div class="row">
<div class="col-md-6">
<img src="<?php $current_posts[$key][0]; ?>" alt="">
</div>
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<img src="<?php $current_posts[$key][1]; ?>" alt="">
</div>
<div class="col-md-12">
<img src="<?php $current_posts[$key][2]; ?>" alt="">
</div>
</div>
</div>
</div>
Related
I have created this page template for jobs positions in wordpress. I want to implement an ajax load more system if there are more than ten positions available. I don't know how to paginate the results and I never implemented something similar in wordpress. can anyone help me?
<?php get_header(); ?>
<?php if( has_post_thumbnail() ): ?>
<div class="jumbotron jumbotron-fluid page-cover">
<div class="parallax" data-parallax-image="<?php the_post_thumbnail_url(); ?>"></div>
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 text-right inner-parallax">
<h1 class="parallax-section-title"><?php the_title(); ?></h1>
</div>
</div>
</div>
<div class="parallax-overlay"></div>
</div>
<?php endif; ?>
<div class="container-fluid content-wrapper" id="wrapper">
<div class="row page-row-justified justify-content-center" style="padding-bottom:0;">
<?php if( have_posts() ): while( have_posts() ): the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php $careers = new WP_Query( ['post_type' => 'careers', 'posts_per_page' => 10 ] ); ?>
<div class="row page-row-justified justify-content-center">
<?php if( $careers->have_posts() ): while( $careers->have_posts() ): $careers->the_post(); ?>
<div class="col-sm-12 col-md-8 col-lg-8 card career-card">
<h4 class=""><a class="career-link" data-toggle="collapse" href=".position-details" class=""><?php the_title(); ?></a></h4>
<small class="career-date"><i class=""></i> <?php the_date('d/m/Y'); ?></small>
<div class="row">
<div class="col-sm-12 col-md-12 col-lg-12 position-details collapse">
<p class="career-details text-justify"><?php echo get_the_content(); ?></p>
<hr>
<h4><a class="career-mail-link" href="mailto:example#demo.com?subject=<?php the_title(); ?>"><i class="fas fa-envelope"></i> <?php _e('Apply'); ?></a></h4>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else: ?>
<div class="col-sm-12 col-md-8 col-lg-8">
<p class="section-claime text-danger hide"><?php _e('No position available.'); ?></p>
</div>
<?php endif; ?>
</div>
</div>
<?php get_footer(); ?>
Try This
<?php
$args= array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'ignore_sticky_posts' => true,
'paged' => 1
);
$my_posts = new WP_Query( $args );
if ( $my_posts->have_posts() ) :
while ( $my_posts->have_posts() ) : $my_posts->the_post();
// Your code for post
endwhile;
endif;
?>
<button class="btn a loadmore_event">View More</button>
Ajax
var ajaxurl = "<?php echo admin_url( 'admin-ajax.php' ); ?>";
var page = 2;
jQuery(function($) {
$('body').on('click', '.loadmore_event', function() {
var data = {
'action': 'load_posts_by_event',
'page': page,
'security': '<?php echo wp_create_nonce("load_more_posts"); ?>'
};
$.post(ajaxurl, data, function(response) {
if(response != '') {
$('your class name').append(response);
page++;
} else {
}
});
});
});
Functions.php
add_action('wp_ajax_nopriv_load_posts_by_event', 'load_posts_event');
function load_posts_event() {
$paged = $_POST['page'];
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'ignore_sticky_posts' => true,
'paged' => $paged
);
$my_posts = new WP_Query( $args );
if ( $my_posts->have_posts() ) :
while ( $my_posts->have_posts() ) : $my_posts->the_post();
// Your code for post
endwhile;
endif;
}// function closing tag
As per the image below - I have a blog archive page which shows a featured blog post as a large one and then shows all of the blog posts below that.
I would like to add something to the main blog loop to say:
IF the blog blog post is featured, don't display it here
So I can stop duplicate blog posts showing up (one featured and then the same one in the main blog loop).
I have two bits of code, one that pulls the featured post through and one that loops all of the posts through. Here
<!-- Featured Blog Item -->
<?php
$loop = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'featured',
'meta_value' => 'yes'
)
);
?>
<div class="container">
<div class="row no-gutters">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-12">
<a href="<?php the_permalink(); ?>">
<div class="hero__overlay featured-grad-blog">
</div>
</a>
<div class="featured-blog-container">
<h3><?php the_title(); ?></h3>
<p><?php the_date(); ?></p>
</div>
<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div class="featured-blog-grid-image-container" style="background-image:url(<?php echo $feat_image; ?>);"></div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
<!-- All Blog Items -->
<?php
$loop = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1
)
);
?>
<div class="container blog-page-container">
<div class="row blog-page-row">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-lg-4 col-sm-6 col-xs-12 blog-page-col">
<div class="blog-image" style="position: relative;">
<a href="<?php the_permalink(); ?>">
<div class="hero__overlay grad-blog-hover"></div>
</a>
<?php $feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<div class="blog-grid-image-container blog-page-image blog-post-image-div" style="background-image:url(<?php echo $feat_image; ?>);"></div>
</div>
<div class="blog-title">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<div class="blog-bars-outside-container">
<div class="blog-bars-inside-container">
<div class="blog-all-bar blog-bar1"></div>
<div class="blog-all-bar blog-bar2"></div>
<div class="blog-all-bar blog-bar3"></div>
<div class="blog-all-bar blog-bar4"></div>
<div class="blog-all-bar blog-bar5"></div>
</div>
</div>
<div class="blog-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="meta-details">
<p><?php the_author_meta( 'display_name', $author_id ); ?> - <?php echo get_the_date(); ?></p>
</div>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
I guess I would need to add the opposite of this maybe, into the array for the main blog posts:
'meta_key' => 'featured',
'meta_value' => 'yes'
But I can't get anything to work...
Any help would be greatly appreciated!
For the non-featured part, please try this:
<!-- All Blog Items -->
<?php
$loop = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => '!='
)
)
)
);
?>
This should select all posts where 'featured' is not equal '!=' 'yes'.
I have the following templates made using pods which are used in the same page. The output of the first template is right but the second template also displays the same data as the first template. I have set the where condition to their respective categories but it seems that the while loop is not resetting.
first template section
<?php
$terms = get_terms( 'front_page', array(
'orderby' => 'count',
'hide_empty' => 0
));
foreach ($terms as $term) {
$args = array(
'post_type' => array(
'trekking',
'trek_peak',
'expedition',
'great_himalayan_trai'
),
'front_page' => $term->slug
);
$query = new WP_Query( $args );
}
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="featured-card featured">
<div class="featured-card__image featured-card__image--featured">
<div class="book">
<a href="https://khumbu-shangrila.com/booking/">
<p>BOOK NOW</p>
</a>
</div>
<img src="<?php the_post_thumbnail_url(); ?>">
</div>
<div class="featured-card__unit-name">
<?php the_title(); ?>
</div>
<div class="featured-card__unit-stats featured-card__unit-stats--featured clearfix">
<div class="one-third">
<div class="stat"><?php the_field('group_size')?></div>
<div class="stat-value">Group Size</div>
</div>
<div class="one-third">
<div class="stat"><?php the_field('duration'); ?></div>
<div class="stat-value">Duration</div>
</div>
<div class="one-third no-border">
<div class="stat"><?php the_field('price'); ?></div>
<div class="stat-value">Cost</div>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
second section template
<?php
$terms = get_terms('front_page', array(
'orderby' => 'count',
'hide_empty' => 0
));
foreach( $terms as $term ) {
$args = array(
'post_type' => array(
'trekking',
'trek_peak',
'expedition',
'great_himalayan_trai'
),
'front_page' => $term->slug
);
$query = new WP_Query( $args );
}
while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="featured-card featured">
<div class="featured-card__image featured-card__image--featured">
<div class="book">
<a href="https://khumbu-shangrila.com/booking/">
<p>BOOK NOW</p>
</a>
</div>
<img src="<?php the_post_thumbnail_url(); ?>"/>
</div>
<div class="featured-card__unit-name">
<?php the_title(); ?>
<p class="status">Trek Confirmed</p>
<p class="departure">Departure Date : 2018/01/12</p>
</div>
<div class="featured-card__unit-stats featured-card__unit-stats--featured clearfix">
<div class="one-third">
<div class="stat">2-10 pax</div>
<div class="stat-value">Group Size</div>
</div>
<div class="one-third">
<div class="stat">16 Days</div>
<div class="stat-value">Duration</div>
</div>
<div class="one-third no-border">
<div class="stat">$1500</div>
<div class="stat-value">Cost</div>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_postdata();
?>
I think your problem comes from the WP_Query you need to specify the category type you wish it to display in each template so that i does not repeat .
$query = new WP_Query( array( 'cat' => 4 ) );
$query = new WP_Query( array( 'category_name' => 'staff' ) );
You can read more about it Below
https://codex.wordpress.org/Class_Reference/WP_Query
I have two loop with orderby random(all work good), but I want to show same post for each loop,
example: I have post 1 to 10;
loop 1 show post 1 2 and 3;
loop 2 need to show same post 1 2 and 3, my code is:
<div class="col-md-12" data-wow-delay="0.2s">
<div class="carousel slide" data-ride="carousel" id="quote-carousel">
<!-- Bottom Carousel Indicators -->
<ol class="carousel-indicators">
<?php
$args = array(
'post_type' => 'testimonials',
'orderby' => 'rand',
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$avatar_testimonials = get_field('avatar-testimonials');
?>
<li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>" class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>" src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>" alt=""></li>
<?php $i++; endwhile; ?>
</ol>
<!-- Carousel Slides / Quotes -->
<div class="carousel-inner text-center">
<?php
$args = array(
'post_type' => 'testimonials',
'orderby' => 'rand',
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
?>
<!-- Quote 1 -->
<div class="item <?php if ($i == 0) echo 'active'; ?>">
<blockquote>
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<?php echo the_excerpt(); ?>
<small><?php echo the_title(); ?></small>
</div>
</div>
</blockquote>
</div>
<?php $i++; endwhile; wp_reset_postdata(); ?>
</div>
</div>
</div>
Try this, I think it will work for you.
<div class="col-md-12" data-wow-delay="0.2s">
<div class="carousel slide" data-ride="carousel" id="quote-carousel">
<!-- Bottom Carousel Indicators -->
<ol class="carousel-indicators">
<?php
$args = array(
'post_type' => 'testimonials',
'orderby' => 'rand',
'posts_per_page' => 3 );
$loop = new WP_Query( $args );
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
$avatar_testimonials = get_field('avatar-testimonials');
?>
<li data-target="#quote-carousel" data-slide-to="<?php echo $i; ?>" class="<?php if ($i == 0) echo 'active'; ?>"><img class="img-responsive" alt="<?php echo the_title(); ?>" src="<?php if ($avatar_testimonials) {echo $avatar_testimonials['url'];} else {the_post_thumbnail_url('thumbnail');} ?>" alt=""></li>
<?php $i++; endwhile; ?>
</ol>
<!-- Carousel Slides / Quotes -->
<div class="carousel-inner text-center">
<?php
$i = 0;
while ( $loop->have_posts() ) : $loop->the_post();
?>
<!-- Quote 1 -->
<div class="item <?php if ($i == 0) echo 'active'; ?>">
<blockquote>
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<?php echo the_excerpt(); ?>
<small><?php echo the_title(); ?></small>
</div>
</div>
</blockquote>
</div>
<?php $i++; endwhile; wp_reset_postdata(); ?>
</div>
</div>
</div>
I'm trying to get 2 different styled posts to show up one after the other and then back around. So it should look like
DB
Uncategorized
DB
Uncategorized
etc
And to have them both styled differently. I'm not great with PHP and the best I got so far was all in one category and then all in the other.
<section class="home-middle">
<div class="container">
<div class="row">
<div class="col-sm-8">
<?php
$args = array('category_name' => 'designer backstage',
'post_status' => 'publish',
'posts_per_page' => '3' );
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div class="stylists-book">
<div class="image">
<div class="meta-designer">
<div class="pro-pic"><img src="images/stylists-pro1.jpg"></div>
<h3>Designer<hr></h3>
<p><?php the_author(); ?></p>
<span><?php the_date(); ?></span>
</div>
<img><?php the_post_thumbnail(); ?>
</div>
<?php
$args = array('category_name' => 'uncategorized',
'post_status' => 'publish',
'posts_per_page' => '3');
$category_posts = new WP_Query($args);
if($category_posts->have_posts()) :
while($category_posts->have_posts()) :
$category_posts->the_post();
?>
<div class="look" style="max-height: 200px">
<div class="row">
<div class="col-md-4">
<div class="team-modster">
<span><?php the_author(); ?></span>
<?php the_title(); ?>
</div>
</div>
<div class="col-md-8">
<a href="<?php the_permalink() ?>">
<img style="height:200px" src="<?php echo catch_that_image() ?>" />
</a>
</div>
</div>
</div>
<?php endwhile; else: >
Oops, there are no posts.
<?php endif; ?>
</div>
</div>
<?php get_sidebar(); ?>
</div>
</div>
</section>
A co-worker took a look at it and got it working. Figured I would post the solution.
<?php
$args = array('category_name' => 'designer-backstage',
'post_status' => 'publish',
'posts_per_page' => '5' );
$designer_posts = new WP_Query($args);
$args = array('category_name' => 'uncategorized',
'post_status' => 'publish',
'posts_per_page' => '5' );
$uncategorized_posts = new WP_Query($args);
if ($designer_posts->have_posts() && $uncategorized_posts->have_posts()) :
// If a new category is added, add it to this array
$category_posts = [$designer_posts, $uncategorized_posts];
$cat_posts_idx = 0;
$new_category_posts = [];
$post_count = 0;
$max_posts = 10;
// Alternate categories and store into a new posts array
while ($post_count < $max_posts) {
// Iterate the post
$category_posts[$cat_posts_idx]->the_post();
// get_the_category() returns an array with one item in this case
$category = get_the_category()[0];
if ($category->slug == 'designer-backstage') { ?>
<div class="stylists-book">
<div class="image">
<div class="meta-designer">
<div class="pro-pic"><img src="images/stylists-pro1.jpg"></div>
<h3>Designer<hr></h3>
<p><?php the_author(); ?></p>
<span><?php the_date(); ?></span>
</div>
<img><?php the_post_thumbnail(); ?>
</div>
<?php }
else if ($category->slug == 'uncategorized') { ?>
<div class="look">
<div class="row">
<div class="col-md-4">
<div class="team-m">
<span><?php the_author(); ?></span>
<?php the_title(); ?>
</div>
</div>
<div class="col-md-8" style="max-height: 225px; overflow: hidden;"><img style="" src="<?php echo catch_that_image() ?>" /></div>
</div>
</div>
</div>
<?php }
else:
?>
Oops, there are no posts.
<?php
endif;
?>