I have following code
<?php
$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
'paged' => $paged,
'posts_per_page' => 3,
'cat_id'=> 5,
));
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I want to show category whose id is 5 on my bog page. But this is showing other categories as well like 4 & 3.
Similarity issue is for archive page as well. Where i am wrong?
check this, i think this is helpful for you.
<?php
$posts = get_posts('category=5&orderby=rand&numberposts=5');
foreach($posts as $post) {
?>
<?php the_title(); ?>
<?php } ?>
Please try with below code :
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
this is working at my end
Try this
<?php
$args3 = array(
'numberposts' => 1,
'cat' => 5,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$homepage_post = get_posts($args3);
foreach ($homepage_post as $post) : setup_postdata($post);
// write your code here ...
endforeach;
wp_reset_postdata();
?>
--
Thanks
Related
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 have three loops on same page.
The first loop displays the most recent post of category "highlight".
The second loop displays others posts of this same category in chronological order.
The third loop displays all the posts, except the posts in others loops.
All works fine, but in pagination (pagenavi), the max_num_pages considers all the posts, ignoring criterias like 'post__not_in' or 'cat'.
If I use this loop, the last page of pagenavi stills in blank (counts hidden posts yet, but don't shows them):
if ($loop3->have_posts()) : while ($loop3->have_posts()) : $loop3->the_post();
And If I use this loop (loop 3), the last page of pagenavi shows the "hidden" posts:
if (have_posts()) : while (have_posts()) : the_post();
How to force the wp_query loop to exclude the hidden posts of the max_num_pages count?
//loop 1
<?php
$loop1 = new WP_query(array(
'category_name' => 'highlight',
'posts_per_page' => 1,
));
if($loop1->have_posts()) : $firstPosts = array(); while($loop1->have_posts()) : $loop1->the_post();
$firstPosts[] = $post->ID;
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
//loop 2
<?php
$loop2 = new WP_query(array(
'post__not_in' => $firstPosts,
'category_name' => 'highlight',
'posts_per_page' => 2,
));
if($loop2->have_posts()) : while($loop2->have_posts()) : $loop2->the_post();
$firstPosts[] = $post->ID;
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
//loop 3
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'paged' => $paged,
'post__not_in' => $firstPosts,
'cat' => -23,
);
$loop3 = new WP_Query( $args );
if ($loop3->have_posts()) : while ($loop3->have_posts()) : $loop3->the_post();
?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
//other stuff
//pagenavi
<?php wp_pagenavi("", "", array(
'query' => $loop3,
'first_text' => 'lorem ipsum',
'last_text' => 'lorem ipsum',
)); ?>
If you're using WP-PageNavi, there is a special way to use it with custom queries. For example:
$my_query = new WP_Query(
array(
'tag' => 'foo',
'paged' => get_query_var('paged')
)
);
while ( $my_query->have_posts() ) : $my_query->the_post();
the_title();
// more stuff here
endwhile;
wp_pagenavi( array( 'query' => $my_query ) );
wp_reset_postdata(); // avoid errors further down the page
So make sure you are passing your custom query object into the wp_pagenavi() function call.
~Edit~
Here's the link to the documentation: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
~Edit 2~
Try this code that has been customized for your particular application:
$args = array(
'post_type' => 'post',
'paged' => $paged,
'post__not_in' => $firstPosts,
'cat' => -23,
);
$loop3 = new WP_Query( $args );
...
wp_pagenavi( array( 'query' => $loop3 ) );
wp_reset_postdata();
I have custom query on my wordpress page. Query looks like this:
$args = array(
'post_type' => array( 'tworca' ),
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=>12,
'post_parent' => 0
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;
// The Query
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
... this is content of my query.....
}
?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php wp_pagenavi(); ?>
</nav>
<?php
} else {
echo 'no results';
}
/* Restore original Post Data */
wp_reset_postdata();
?>
But my pagination doesn't show. Any idea why? I have read many posts with similar problem, but solution always was to add 'paged' parameter to wp query. And I have this parameter in my query and it doesn't help.
Thanks in advance for your help!
Try This..
`$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 12,
'paged' => $paged,
'orderby' => 'title',
'order' => 'ASC',
'post_parent' => 0,
'post_status' => 'publish',
);
query_posts( $args );
if ( have_posts() ) {
$i = 0;
while ( have_posts() ) {
the_post();
Your data here you want display like title
the_title();
}
wp_pagenavi();
}
wp_reset_query();
?>`
Please try by adding your query object to the wp_pagenavi(), which would be like,
wp_pagenavi( array( 'query' => $the_query ) );
Reference: http://scribu.net/wordpress/wp-pagenavi/wpn-2-74.html
Change wp_pagenavi() to wp_pagenavi( array( 'query' => $the_query) );
In wordpress I have a page template called news that I want to display all the posts from one category - 'News'. I don't want to use the category.php because there is a massive blog on the site already.
query_posts('cat=145');
while ( have_posts() ) : the_post();
//do something
endwhile;
Works fine but I have read that query_posts has drawbacks (like speed)
I tried doing this but it just showed me nothing:
while ( have_posts() ) : the_post();
if ( in_category( '145' ) ) : //also tried 'News'
//do something
Why doesn't' in_category work here?
please try this code:
$args = array('post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'news' // please pass here you news category slugs
),
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print_r($post);
endwhile;
wp_reset_postdata();
You could WP Query for achieving your requirement.
Documentation: https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Example:
<?php
$args = array(
'cat' => 145,
);
$the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Try to use get_posts() function:
$get_p_args = array('category'=> 145,'posts_per_page'=>-1);
$myposts = get_posts( $get_p_args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<div>
<?php the_title(); ?>
</div>
<?php endforeach;
wp_reset_postdata();?>
Try this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
global $wp_query;
$args = array_merge( $wp_query->query_vars, array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 6, // limit of posts
'post_status' => 'publish',
'orderby' => 'publish_date',
'order' => 'DESC',
'lang' => 'en', // use language slug in the query
) );
query_posts( $args );