2 WP_Querys repeating one after the other - Styled Differently - php

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;
?>

Related

Hide a Wordpress blog post from main list when it appears as featured

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'.

WordPress plugin pagenavi, I can't see more then 2 pages, even with 100+ posts

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 ) ); ?>

Wordpress - Custom loop query for displaying custom post type

I'm messing around with my code. My goal is to display 4 custom post type on the homepage in the HTML layout I've created. Here's my code. Actually I can get the href but I can't loop the code not even achieve my scope.
<div class="roundedframe ">
<div class="container-fluid">
<div class="row">
<div class="col-lg-4 col-sm-6">
<a class="portfolio-box" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="portfolio-box-caption">
<div class="portfolio-box-caption-content">
<div class="project-category text-faded">
Category
</div>
<div style="background-image: url('<?php the_post_thumbnail_url(); ?>');">
<div class="project-name"> <?php // WP_Query arguments
$args = array(
'name' => 'case-studies',
'nopaging' => true,
'posts_per_page' => '4',
);
// The Query
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
?>
Project Name
</div>
</div>
</div>
</a>
</div>
</div>
</div>
</div>
Assuming the post type you want is case-studies you should name the key post_type and not name. You also have to place the column inside the loop and close it afterwards. You also missed a </div> tag.
<?php $query = new WP_Query( [
'post_type' => 'case-studies',
'nopaging' => true,
'posts_per_page' => '4',
] ); ?>
<?php if ( $query->have_posts() ) : ?>
<div class="roundedframe ">
<div class="container-fluid">
<div class="row">
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="col-lg-4 col-sm-6">
<a class="portfolio-box" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<div class="portfolio-box-caption">
<div class="portfolio-box-caption-content">
<div class="project-category text-faded">
Category
</div>
<div style="background-image: url('<?php the_post_thumbnail_url(); ?>');">
<div class="project-name">
<h2><?php the_title(); ?></h2>
</div>
</div>
</div>
</div>
</a>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
You should put your code in the looping area. What i can see, you missed the endwhile also.
<div class="roundedframe ">
<div class="container-fluid">
<div class="row">
<?php // WP_Query arguments
$args = array(
'name' => 'case-studies',
'nopaging' => true,
'posts_per_page' => '4'
);
// The Query
$query = new WP_Query($args);
while ($query->have_posts()):
$query->the_post(); ?>
<div class="col-lg-4 col-sm-6">
<a class="portfolio-box" href="<?php
get_the_permalink();
?>" title="<?php
get_the_title();
?>">
<div class="project-category text-faded">
Category
</div>
<div style="background-image: url('<?php
the_post_thumbnail_url();
?>');">
<div class="project-name">
Project Name
</div>
</div>
</a>
</div>
<?php
endwhile;
?>
</div>
</div>
</div><!--.roundedframe-->
Try this and let me know. It may help you. Before that you should learn about wp_query
https://codex.wordpress.org/Class_Reference/WP_Query

How to get current item in wordpress loop under loop?

I am creating a portfolio section.the live site is here, http://www.nayeemriddhi.info/testproject/portfolio/. There need three loop for showing item. But fact is that, when i open portfolio item, the right sidebar item showing item from the beginning, as i created the loop. but i want to show right sidebar item as a current item for the portfolio images. is there any idea for showing right sidebar item as a current item.
the code is below,
<?php
/*
Template Name: Portfolio
*/
get_header(); ?>
<!-- Banner -->
<section class="page-banner" >
<div class="container">
<div class="row">
<div class="col-md-12">
<h1 class="page-title ">Portfolios</h1>
<span class="page-tag-line">See our recent works</span>
</div>
</div>
</div>
</section>
<div class="wave-divider-pages"></div>
<section>
<div class="container gal-container">
<?php
$args = array(
'post_type' => 'custom_portfolio',
'posts_per_page' => '-1',
);
// the query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) ?>
<?php while ( $query->have_posts() ) : $query->the_post() ; ?>
<?php $globalID = get_the_id(); ?>
<!-- Item-->
<div class="col-md-4 col-sm-6 co-xs-12 gal-item">
<div class="box">
<a class="trigger" data-iziModal-open="#modal<?php the_ID(); ?>">
<img src="<?php echo get_post_meta(get_the_ID(), 'portfolio_image', true); ?>">
</a>
</div>
</div>
<!-- Modal-->
<div id="modal<?php the_ID(); ?>" class="iziModal portfolio" data-izimodal-title="Portfolio Title" data-izimodal-subtitle="Web Design" style="max-width: 1200px important;">
<div class="col_one_third p-20">
<?php
$args2 = array(
'post_type' => 'custom_portfolio',
'posts_per_page' => '6',
);
// the query
$query2 = new WP_Query( $args2 );
// The Loop
if ( $query2->have_posts() ) ?>
<?php while ( $query2->have_posts() ) :
$query2->the_post() ; ?>
<div class="col_half p-10">
<a href="#<?php the_ID(); ?>-<?= $globalID; ?>" data-toggle="tab">
<img src="<?php echo get_post_meta(get_the_ID(), 'portfolio_image', true); ?>" class="portfolio-thumb"/>
</a>
</div>
<?php endwhile; wp_reset_query(); ?>
<div class="col_full p-10">
<div class="portfolio-links">
Launch Website
Request a Quote
</div>
</div>
</div>
<div class="col_two_third col_last">
<div class="tab-content ">
<?php
$args3 = array(
'post_type' => 'custom_portfolio',
'posts_per_page' => '-1',
);
// the query
$query3 = new WP_Query( $args3 );
// The Loop
if ( $query3->have_posts() ) ?>
<?php while ( $query3->have_posts() ) :
$query3->the_post() ; ?>
<div class="tab-pane active" id="<?php the_ID(); ?>-<?= $globalID; ?>">
<img src="<?php echo get_post_meta($globalID, 'portfolio_image', true); ?>" class="img-responsive"/>
</div>
<?php endwhile; wp_reset_query(); ?>
</div>
</div>
</div>
<!-- Item End-->
<?php endwhile; wp_reset_query(); ?>
</div>
</section>
<section>
</section>
<div class="wave-divider-common"></div>
<?php get_footer(); ?>
Thanks for help...
Modify your html for first anchor tag as
<a class="trigger" data-iziModal-open="#modal<?php the_ID(); ?>" data-id="<?php the_ID(); ?>-<?= $globalID; ?>">
<img src="<?php echo get_post_meta(get_the_ID(), 'portfolio_image', true); ?>">
</a>
Add this JS
<script type="text/javascript">
$(document).ready(function(){
$('a.trigger').on('click', function (e) {
var getDataId = $(this).data('id');
$('.iziModal a[href="#' + getDataId + '"]').tab('show');
});
});
</script>

Wordpress search template - not showing exact search results but all posts

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.

Categories