Currently I am querying a post from my custom post type to my custom page template. This is the code I am using
<?php query_posts('post_type=testimonial&post_status=publish&posts_per_page=10&paged='.
get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
But I want to query a post from a specific category. For eg. my custom post type is "testimonial" it has 3 categories like category1, category2and category3 . I want to show only the post of category3 in my page template. How can I do that? thanks
Use this
<?php $posts = get_posts('category=3&orderby=rand&numberposts=5');
foreach($posts as $post) { ?>
<?php the_title(); ?>
<?php } ?>
Or
$args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 );
query_posts( $args );
Uncomment either one of the below as per you requirement.
If you want to query using the id of the category use the first one and replace 99 with your category id.
If you know the slug of your category use the second one with your category slug(not name).
<?php
//query_posts('cat=99&post_type=testimonial&post_status=publish&posts_per_page=10&paged='.get_query_var('paged'));
//query_posts('category_name=your_category_slug&post_type=testimonial&post_status=publish&posts_per_page=10&paged='.get_query_var('paged')); ?>
<?php if(have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
P.S: I recommend you to use wp_query() instead of query_post().
Related
How to get all posts from one category. i tried this code , it's not showing any output.Is it correct or any correction is here? Thanks.
include('wp-config.php');
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result = array(
"id"=>$args['ID'],
"type"=>$args['post_type'],
"url"=>$args['guid']);
endforeach;
wp_reset_postdata();
print($result);
Try below :-
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result[] = array(
"id"=>$post->ID, // changed $args to $post
"type"=>$post->post_type,
"url"=>$post->guid);
endforeach;
wp_reset_postdata();
print_r($result);
If you want to display posts from a specific category in the category page, you can use the below given code in your theme's category.php file.
<?php
if(have_posts()) :
while (have_posts()) : the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
?>
If you want to display the same in pages other than category page, just add the following code to the corresponding files.
<?php
$posts = new WP_Query();
$posts->query( "category_name='{enter your category slug here}'&posts_per_page=-1" );
if($posts->have_posts()) :
while ($posts->have_posts()) : $posts->the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
are you just trying to get posts from a category?
This is a handy code from the Codex that I keep around. Use this on any custom category page or anywhere on any page, for that matter, to start the loop. Make sure you put your category slug into the right spot in the code.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
// YOUR STUFF LIKE the_title(); or the_content();
endwhile; endif;
This is NOT a fix to your code, but it answers the question you asked. I think your problem may be in the use of $args inside the loop (seems odd), but if you want me to make sure I might need more of the code or a working example I can see.
http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category
AHEM...yeah... I'm an idiot... don't go pasting this around. Use WP_Query!! thanks Pieter.
On your theme directory, search category.php. If it doesn't contain, create a new file category.php and paste this code:
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_excerpt();?>
<?php endwhile; else :?>
<?php endif;?>
I have a custom post type called 'grhdogs'. The problem is that the loop inside the WordPress search.php is styling the custom post type like a normal hit on a post or page. I want to style the custom post type search results with a different template part. How can I do this? It speaks for it self that normal posts and pages should get the default template part (content,search).
This is the loop...
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
<?php wplook_content_navigation('postnav' ) ?>
The template part I want to use for the custom post type 'grhdogs' is:
<?php get_template_part( 'search', 'grhdogs' ); ?>
You should include the template part by comparing the post type using get_post_type() function -
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<?php // so if the current post type is your custom post type ?>
<?php if( 'grhdogs' == get_post_type() ): ?>
<?php get_template_part( 'search', 'grhdogs' ); ?>
<?php // for any other post type ?>
<?php else : ?>
<?php get_template_part( 'content', 'search' ); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
<?php wplook_content_navigation('postnav' ) ?>
Additionally, For including separate template file based on post type and current object page, a dynamic method be -
<?php $query_type = 'search'; // pv: archive, author, category ?>
<?php $post_type = get_post_type(); // pv: post, custom post type ?>
<?php get_template_part( $query_type, $post_type ); ?>
And example template filename would be then - 'search-post.php'
** pv = possible value
I am using a wordpress query to display 3 posts at random from a custom post type. I am using the following code which is working fine:
<?php $my_query = new WP_Query('post_type=my_post_type&orderby=rand&showposts=3'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php endwhile; ?>
<?wp_reset_query(); ?>
However, I want to mirror the same query below to show the same items once again. So two wordpress queries on one page, the first query picking 3 random posts and the second query showing the exact same results of the first query. Any help would be appreciated. Thanks
try this out :)
<?php $my_query = new WP_Query('post_type=post&orderby=rand&showposts=3'); ?>
<?php $i=0; ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php
$myPostVar[$i] = array (
'title' => get_the_title(),
'content' => get_the_content()
);
$i++;
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php foreach ($myPostVar as $Postvar) : ?>
<h2><?php echo $Postvar['title']; ?></h2>
<p><?php echo $Postvar['content']; ?></p>
<?php endforeach; ?>
I've created a custom post type to do hand-crafted excerpts from my blog on my portfolio site. I've got a content window, a link, and a featured image for the post type, which I have called blog.
The issue is that, no matter what I try, the posts are displayed oldest to newest, whereas I would like to display the newest first. Here's the query_posts() call:
<?php query_posts( 'post_type=blog&order=ASC'); ?>
But I've also tried more elaborate queries such as:
<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC')); ?>
My complete template file looks like:
`
">
<div class="sliderContent">
<!--first loop-->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(__('Read More »', THEMENAME)); ?>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php endif; ?>
<!--second loop, displays custom post type-->
<?php query_posts(array('post_type' => 'blog', 'orderby'=>'date','order'=>'ASC') ); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(__('Read More »', THEMENAME)); ?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php endif; ?>
</div>
</div>
<!-- content end -->
<?php } ?>
`
So I'm displaying the content from the page that this template is applied, then I'm displaying my custom post type.
Thanks for the help, I'm stumped!
Your query is in Ascending order. Ascending order IS oldest to newest. You want DESCENDING order if you want newest to oldest. Also, you should avoid using query_posts if at all possible, as it modifies the default Wordpress loop.
Your second query isn't that much more elaborate than the first. The only difference is you're using an array rather than a string to define the query parameters (which an array is arguably the correct way to go about it), and you're setting the orderby parameter.
Lastly, the default order is by date in descending order (newest to oldest) so you theoretically don't even NEED to define order and orderby parameters.
Try this:
<!--second loop, displays custom post type-->
<?php
$args = array('post_type' => 'blog', 'orderby'=>'date','order'=>'DESC');
/*Consider changing to: $args = array('post_type' => 'blog');*/
$query = new WP_Query($args);
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(__('Read More »', THEMENAME)); ?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Nothing found.', THEMENAME); ?></p>
<?php
endif;
wp_reset_postdata();
?>
</div>
Well, as majorano84 alluded to, after further reading query_posts() is not the right function to use at all (because I guess it makes more work for the server?)
Instead, I used get_posts(), and that displays the posts in the preferred order without any further effort on my part:
<?php
$args = array( 'post_type' => 'blog' );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<div class="contenttile">
<p><?php the_post_thumbnail('medium'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?php endforeach; ?>
`
So I'm going to call that the answer, because it solves the problem I was having. Thanks!
i want to show 4 posts in page and after 4 posts it will show next button to read next posts.
<?php while (have_posts()) : the_post(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&order=ASC&category=3');
foreach($myposts as $post) :
setup_postdata($post);
?>
here is content with html
<?php endforeach; ?>
<?php endwhile; ?>
<?php endif; ?>
You just need to add paged parameter to your query_posts:
$myposts = get_posts('numberposts=4&order=ASC&category=3&paged' . get_query_var('paged'));
And add post_nav_link() to display next and prev link.
<?php posts_nav_link(); ?>
Cheers, oh, btw, you can post specific wordpress question at wordpress.stackexchange.com.