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
Related
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'.
That's how it's looks with 20 posts, and only four posts shows.
With this, I have 20 posts in this category, I'm showing on the single page only four of them, so i should get 5 pages of navi page plagin, but I get only two of them, and it's doesn't metter how many posts I have.
<?php
/**
* Template Name: strategy
*/
get_header(); ?>
<div class="template">
<div class="container">
<div class="wrapper">
<div class="breadcrumb">
<?php if (function_exists('bcn_display')) {
bcn_display();
} ?>
</div>
<div class="page_title">
<h1><?php single_cat_title(); ?></h1>
</div>
<div class="articles_page__wrapper">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 0;
global $post;
$args = array(
'numberposts' => 20,
'category' => 3,
'orderby' => 'date',
'order' => 'DESC',
'include' => array(),
'exclude' => array(),
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'paged' => $paged,
);
$myposts = get_posts($args);
foreach ($myposts as $post) {
setup_postdata($post); ?>
<div class="item">
<div class="img">
<a href="<?php the_permalink(); ?>">
<img src="<?php the_post_thumbnail_url('small'); ?>"
alt="<?php the_title(); ?>">
</a>
</div>
<div class="group">
<div class="title">
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
</div>
<div class="description">
<p><?php echo excerpt(40); ?></p>
</div>
</div>
</div>
<?php }
wp_reset_postdata(); ?>
</div>
<article>
<?php
$id = 8;
$post = get_post($id);
echo $content = $post->post_content;
?>
</article>
<?php if (function_exists('wp_pagenavi')) wp_pagenavi(); ?>
</div>
</div>
</div>
<?php get_footer(); ?>
Try updating the wp_pagenavi use like this:
<?php if (function_exists('wp_pagenavi')) wp_pagenavi( array( 'query' => $myposts ) ); ?>
i've got a own wordpress template (still in progress). It includes of course search.php template which looks like this:
<?php
get_header(); ?>
<section class="row page_intro">
<div class="row m0 inner">
<div class="container">
<div class="row">
<h5><?php
/* translators: %s: search query. */
printf( esc_html__( 'Search Results for: %s', 'vetsandpets' ), '<span>' . get_search_query() . '</span>' );
?></h5>
<h1><?php _e('News and veterinary advices', 'vetsandpets'); ?></h1>
</div>
</div>
</div>
</section>
<section class="row breadcrumbRow">
<div class="container">
<div class="row inner m0">
<?php
if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('
<p id="breadcrumbs">','</p>
');
}
?>
</div>
</div>
</section>
<section class="row content_section">
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-8 blog_list">
<?php
global $post;
setup_postdata( $post );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array(
'post_type' => 'post', // You can add a custom post type if you like
'posts_per_page' => '6',
'paged' => $paged
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="row m0 blog blog2">
<div class="image_row row m0">
<?php the_post_thumbnail('looppostthumbnail', array( 'class' => "img-responsive loop-post-image")); ?>
</div>
<h3><?php echo get_the_title(); ?></h3>
<div class="row m0 meta"><?php _e('Posted on', 'vetsandpets'); ?>: <?php the_time('j F Y'); ?></div>
<p><?php echo excerpt(50); ?></p>
<?php _e('Read more', 'vetsandpets'); ?>
</div> <!--Single Post-->
<?php endwhile; ?>
<?php echo wpse247219_custom_pagination(); ?>
<?php else : ?>
<div class="center"><?php _e('Nope:( no posts yet.', 'vetsandpets'); ?></div>
<?php endif; wp_reset_postdata(); ?>
</div>
<div class="col-sm-12 col-md-4 sidebar">
<div class="row m0 widget categories">
<h5 class="widget_heading"><?php _e('Categories', 'vetsandpets'); ?></h5>
<ul class="list-unstyled">
<?php
$args = array(
'orderby' => 'count',
'depth' => 0,
'title_li' => '',
'use_desc_for_title' => '',
'order' => 'DESC',
'hide_empty' => 0
);
wp_list_categories($args);
?>
</ul>
</div>
<div class="row m0 widget recent_posts">
<h5 class="widget_heading"><?php _e('Recent posts', 'vetsandpets'); ?></h5>
<?php
// the query
$the_query = new WP_Query( array(
'posts_per_page' => 3
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="media recent_post">
<div class="media-left">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('recentpostthumbnail', array( 'class' => "img-responsive recentpostimage")); ?>
</a>
</div>
<div class="media-body">
<h5><?php the_title(); ?></h5>
<p><?php _e('Posted on', 'vetsandpets'); ?>: <?php the_time('j F Y'); ?></p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e('Nope:( no posts yet.', 'vetsandpets'); ?></p>
<?php endif; ?>
</div>
</div>
</div>
</div>
</section>
<?php
get_sidebar();
get_footer();
?>
And that's it. Then I have a search form which is included always in a navigation modal. Below you can check the php code:
<form role="search" method="get" id="searchform" action="' . home_url( '/' ) . '" >
<div>
<input class="form-control" type="search" value="<?php get_search_query(); ?>" id="example-search-input" name="s" id="s" />
<button type="submit" class="btn searchbtn" id="searchsubmit"><?php _e('Submit', 'vetsandpets') ?></button>
</div>
</form>
but it doesnt work - meaning: it always displays all posts.. what im doing wrong? it is somehow linked to arguments in this code/loop?
The problem is that you reset the global $post object by the query_posts() call. As it is stated in the WordPress Docs: This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible.
So, you should delete these lines:
query_posts(array(
'post_type' => 'post',
'posts_per_page' => '6',
'paged' => $paged
));
The first while loop <?php while ( have_posts() ) : the_post(); ?> will already iterate over the search results.
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>
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;
?>