admin管理员组文章数量:1430685
Why on earth is this piece of code not working?
$my_query = new WP_Query('category_name=feature');
while ($my_query->have_posts()) : $my_query->the_post();
echo 'test';
endwhile;
I've tried with Feature
and feature
, no difference.
I need to get post by category name.
Any help appreciated.
update
This is not working either: $my_query = new WP_Query('cat=3');
code
// index.php
<?php
include_once('header.php');
?>
<div id="fp-slider-container" class="blue-gradient">
<div class="main-content-container">
<?php
query_posts( 'cat=3&posts_per_page=5' );
while ( have_posts() ) : the_post();
echo 'test';
endwhile;
?>
</div>
</div>
<?php
include_once('footer.php');
?>
Why on earth is this piece of code not working?
$my_query = new WP_Query('category_name=feature');
while ($my_query->have_posts()) : $my_query->the_post();
echo 'test';
endwhile;
I've tried with Feature
and feature
, no difference.
I need to get post by category name.
Any help appreciated.
update
This is not working either: $my_query = new WP_Query('cat=3');
code
// index.php
<?php
include_once('header.php');
?>
<div id="fp-slider-container" class="blue-gradient">
<div class="main-content-container">
<?php
query_posts( 'cat=3&posts_per_page=5' );
while ( have_posts() ) : the_post();
echo 'test';
endwhile;
?>
</div>
</div>
<?php
include_once('footer.php');
?>
Share
Improve this question
edited Sep 7, 2012 at 22:01
Steven
asked Sep 7, 2012 at 21:31
StevenSteven
2,6207 gold badges41 silver badges61 bronze badges
5
- I'll ask the obvious first: do you definitely have at least one post in that category? – Pippin Commented Sep 7, 2012 at 21:34
- he he, yes. I got 3 :) – Steven Commented Sep 7, 2012 at 21:35
- Can you provide the complete context that this query is being used in? – Pippin Commented Sep 7, 2012 at 21:46
- @Pippin here you go – Steven Commented Sep 7, 2012 at 22:01
- You do not want to use query_posts(). Does WP_Query return anything if you don't specify a category? – Pippin Commented Sep 7, 2012 at 22:26
3 Answers
Reset to default 8A common pitfall is the fact that:
category_name takes the category SLUG NOT the name as attribute
This is often mistaken.
instead of hacking index.php why not use the pre_get_posts filter?
add_filter('pre_get_posts', 'filter_homepage_posts');
function filter_homepage_posts($query) {
$limit_number_of_posts = 5;
$featured_category_id = get_cat_id('Reviews'); // by cat name...
if ($query->is_home) {
$query->set('cat', $featured_category_id);
$query->set('showposts', $limit_number_of_posts);
}
return $query;
}
copied from: http://www.seanbehan/intercepting-the-wordpress-loop
should also work like this since technically the query passes by reference
add_action('pre_get_posts', 'filter_homepage_posts');
function filter_homepage_posts( $query) {
$limit_number_of_posts = 5;
$featured_category = 'bacon-category'; // by cat slug...
if ($query->is_home) {
$query->set('category_name', $featured_category);
$query->set('showposts', $limit_number_of_posts);
}
return $query;
}
As Hexodus mentioned, category_name
actually requires a category slug.
If you're determined to use the actual category name as an input, then you can do something like this:
$args['category_id'] = get_cat_ID('Category Name');
or this:
$args['category_name'] = (get_category(get_cat_ID('Category Name'))) -> slug;
Documentation:
get_cat_ID: Codex, Developer Resources
get_category: Codex, Developer Resources
WP_Query: Codex, Developer Resources
get_posts: Codex, Developer Resources
本文标签: wp queryWhy is WPQuery not working with categoryname
版权声明:本文标题:wp query - Why is WP_Query not working with category_name? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745549718a2662880.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论