I am using WP_Query to loop through a custom post type in wordpress. My loop looks like this:
<div class="bigRedStrip">
<h2>Available Now!</h2>
</div>
<ul>
<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
loop stuff here
</li>
<?php endwhile; ?>
</ul>
As you can see, before the loop there is a header that says "Available Now!". I want to reformat the loop so if there are no posts returned, then the div containing the title (div class bigRedStrip) will not be displayed. I have tried a number of potential solutions, but the problem I keep running into, is that all of these "solutions" require putting the <div class="bigRedStrip"> inside the loop, which results in the header repeating for every returned post. The idea is to have the header only displayed once. Any ideas how I can accomplish this?
You only need to pull the things a bit apart. First of all run the query:
<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
Then check if there is something:
<?php if ($loop->have_posts()) { ?>
<div class="bigRedStrip">
<h2>Available Now!</h2>
</div>
...
And if so, just iterate over the posts:
...
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
loop stuff here
</li>
<?php endwhile; ?>
</ul>
<?php } ?>
<?php $loop = new WP_Query( array( 'post_type' => 'films', 'post_child' => 0, 'posts_per_page' => 8,'orderby' => 'date', 'order' => 'ASC', 'film-categories' => 'blu-ray' ) ); ?>
<?php if ($loop->have_posts()){
<div class="bigRedStrip">
<h2>Available Now!</h2>
</div>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
loop stuff here
</li>
<?php endwhile; ?>
</ul>
<?php } ?>
Related
I created a WordPress loop for with specific arguments but it is just ignoring them. For example I like to list only posts but there are also pages listed.
<h1 id="" class="offset"><?php _e('Aktuell','Main'); ?></h1>
<?php
// Restore original Post Data
wp_reset_postdata();
// WP_Query arguments
$args = array (
'post_type' => 'post',
'cat' => '50,47',
'numberposts' => '3',
'posts_per_page' => '3',
'ignore_sticky_posts' => true,
'order' => 'ASC',
'orderby' => 'menu_order',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<article <?php post_class(); ?>>
</article>
<?php }
} else {
// no posts found
}
?>
</div>
This code is working correctly on my site. Perhaps try doing the loop as
<article <?php post_class(); ?>>
<?php echo get_the_title(); ?>
</article>
and confirm if the titles are definitely not what you're expecting
I've construced two Wordpress loops with numeric pagination as follows:
<div class="container" style="background:#ccc">
<?
$paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
// Custom Loop with Pagination 1
// http://codex.wordpress.org/Class_Reference/WP_Query#Usage
$args1 = array(
'paged' => false,
'category_name' => 'latest',
'posts_per_page' => 1,
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
?>
<!-- second -->
<?
$args2 = array(
'paged' => $paged2,
'category_name' => 'uncategorized',
'posts_per_page' => 2,
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
$pag_args2 = array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $query2->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args2 );
?>
</div>
<!-- container -->
Everything works fine, but the paginatin page also shows the 'latest' post loop, although I've set the pagination to false.
How could I hide the first loop on the pagination pages?
Advice appreciated.
use wp_reset_postdata() function after your loop end
follow this code, may be it'll help you
<?php
// example args
$args = array( 'posts_per_page' => 3 );
// the query
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- start of the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?><!-- end of the loop -->
<!-- put pagination functions here -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I am new to wordpress, I have a post that has a category_name of offer and it has a content, here is the permalink : http://localhost/jcjohn/2016/09/20/what-we-offer/
Now I want to display the contents of my post from my section page.
Here is my code inside the section :
<section id = "offer">
<?php
$args = array(
'type' => 'post',
'posts_per_page' => 3,
'category_name' => 'offer',
);
$offerBlog = new WP_Query($args);
if ($offerBlog->have_post()):
while ($offerBlog->have_posts()):
$offerBlog->the_post();
get_template_part('content', get_post_format());
endwhile;
endif;
wp_reset_postdata();
?>
</section>
So here is what you would need to do to display the post on the single page. You seem to have missed the s from have_post so it needs to be like below
Note: This would go inside index.php
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'offer',
);
$offerBlog = new WP_Query( $args );
?>
<!-- Blog Article -->
<section id="blog">
<?php
if ( $offerBlog->have_posts() ) :
while ( $offerBlog->have_posts() ) : $offerBlog->the_post();
the_content();
endwhile;
else : ?>
<div class="no-content">
<h3>Well... it looks like we forgot to put content here.</h3>
</div>
<?php
endif;
wp_reset_postdata();
?>
</section>
You try using this loop with the cat_id that you have for the category you create.
query_posts( array( 'cat' => '1', 'posts_per_page' => 5, 'orderby' => 'title', 'order' => 'DESC' ) );
You can have a try of the replaced code as follows.
<section id = "offer">
<?php
$args = array(
'post_type' => 'post',
'cat' => '1',
'posts_per_page' => 5,
'orderby' => 'title',
'order' => 'DESC'
);
$offerBlog = new WP_Query($args);
if ($offerBlog->have_posts()):
while ($offerBlog->have_posts()):
$offerBlog->the_post();
get_template_part('content', get_post_format());
endwhile;
endif;
wp_reset_postdata();
?>
</section>
You have missed the loops format.
Reference:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I have custom post type 'cars' and its child post type is 'carvariants'.
What I want to do is get child posts (carvariants) of current post (cars). I tried this code:
<div>
<?php
$parent_id = 1064;
$the_query = new WP_Query(array(
'post_parent' => $parent_id,
'post_type' => 'carvariants',
'posts_per_page' => 1,
'meta_key' => 'wpcf-minimum-price',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
$compprd = get_the_ID(); ?>
<?php the_title(); ?>
<?php
endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
I want to display child posts of Cars order by custom field wpcf-minimum-price
but 'post_parent' is not working. This code is showing blank output. Whats wrong in this?
I didn't try this. But I hope this will work.
If it will not work, leave me a comment, and I will try to make it work.
Also, if there are better solutions, I will be glad to see the code from professionals:
<div>
<?php
$parent_id = 1064;
$args = array( 'child_of' => $parent_id );
$children_pages = get_pages( $args );
if ( count( $children_pages ) != 0 ) :
foreach ( $children_pages as $children_page ) :
if ( $children_page->have_posts() ) :
$args_for_posts = array( 'posts_per_page' => 1,
'post_type' => 'carvariants',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'post_parent' => $children_page );
$postlist = get_posts( $args_for_posts );
foreach ( $postlist as $post) :
setup_postdata( $post ); ?>
<ul>
<?php
the_post();
?>
</ul>
<?php
endforeach;
wp_reset_postdata();
endif;
endforeach;
else : ?>
<p>No content to show.</p>
<?php
endif; ?>
</div>
I am using a custom post type in my wordpress theme, and I need help with the loop. Here is my code:
<?php $loop = new WP_Query( array( 'post_type' => 'magazine', 'posts_per_page' => 10 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail( 'magazine' ); ?>
<h2><?php the_title( ); ?></h2>
<?php the_content;?>
</li>
<?php endwhile; ?>
This returns the 10 latest posts in the custom field "magazine". I want it to only display the parents in the custom field "magazine". Just like pages, my custom fields have attributes, so you can select a hierarchy (parent/child). I want to edit the loop so it only returns the parents (the latest issues of the magazine, not the articles within each issue) Does anyone know how to do that using the wordpress loop above?
Just add 'post_parent' => 0 to the args array.
<?php $loop = new WP_Query( array( 'post_type' => 'magazine', 'posts_per_page' => 10, 'post_parent' => 0 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<?php the_post_thumbnail( 'magazine' ); ?>
<h2><?php the_title( ); ?></h2>
<?php the_content;?>
</li>
<?php endwhile; ?>