How to get a custom query running in Wordpress?
I'm trying to get a Page to display a custom query using the snippet posted here. The followin开发者_如何学运维g is in my functions.php
:
function alter_the_query($request) {
$dummy_query = new WP_Query();
$dummy_query->parse_query($request);
// this is the actual manipulation
if($dummy_query->is_page('blog')) $request['category_name'] = 'Blog';
// and then
return $request;
}
add_filter('request','alter_the_query');
The page called "Blog" has a basic loop inside- so it should be displaying whatever WP_Query
tells it to. However, it is only displaying the Page content- which should have been ignored.
I can't seem to find a reference that explains the properties of the $request
object, so I'm not sure what I should do to make this snippet work. Any ideas?
I suppose this happens because you didn't change $request['pagename'] value, so wp try to load content for this pages.
Update this should work for you
function alter_the_query($request) {
$dummy_query = new WP_Query();
$dummy_query->parse_query($request);
// this is the actual manipulation
if($dummy_query->is_page('blog')) {
$request['category_name'] = 'Blog';
unset($request['pagename']);
}
// and then
return $request;
}
精彩评论