WordPress函数:WP_Query 按文章、页面的ID查询文章
按文章ID查询文章是最基本的功能,在WordPress中,WP_Query 提供了非常丰富的参数。
1. 按文章ID查询文章
$query = new WP_Query( array( 'p' => 7 ) );
2. 按页面ID查询页面
$query = new WP_Query( array( 'page_id' => 7 ) );
3. 按文章ID查询自定义文章,和第1条是类似的,WP_Query 默认 'post_type' = 'post'。
// 以下两行代码 作用是一样的
$query = new WP_Query( array( 'page_id' => 7 ) );
$query = new WP_Query( array( 'post_type' => 'page', 'p' => 7 ) );
// 如果自定义文章,指定自定义文章类型即可
$query = new WP_Query( array( 'post_type' => 'custom_post_type', 'p' => 7 ) );
4. 类似的,使用 name 和 pagename 参数
$query = new WP_Query( array( 'name' => 'about-my-life' ) )
$query = new WP_Query( array( 'pagename' => 'contact' ) );
$query = new WP_Query( array( 'pagename' => 'contact_us/canada' ) );
5. 查询文章的子文章
// 父文章ID是93
$query = new WP_Query( array( 'post_parent' => 93 ) );
// 无父文章
$query = new WP_Query( array( 'post_parent' => 0 ) );
// 指定多个父文章
$query = new WP_Query( array( 'post_parent__in' => array( 2, 5, 12, 14, 20 ) ) );
6. 指定多个文章ID
$query = new WP_Query( array( 'post_parent__in' => array( 2, 5, 12, 14, 20 ) ) );
$query = new WP_Query( array( 'post_type' => 'page', 'post__in' => array( 2, 5, 12, 14, 20 ) ) );
$query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) );
WP_Query 的基础用法可以参考文章:WP_Query 的基础用法简介
-
WordPress函数:is_login() 判断是否是登录页面千呼万唤始出来,和 is_admin() 同出一辙
-
WordPress函数:get_posts 查询一组文章的好方法get_posts 和 WP_Query 支持的参数一模一样
-
WordPress函数:has_post_thumbnail判断文章是否设置了特色图像函数has_post_thumbnail是一个用于判断文章是否设置了特色图像WordPress函数,也就是我们常说的微缩图。
-
WordPress 函数 query_posts 及查询文章方法总结WordPress 有各种各样的查询文章方法
-
WordPress函数:add_theme_support 为主题添加功能特性让自己的主题支持更多的特性
-
WordPress 函数:get_comments 评论查询 参数大全get_comments 参数太多了……
暂无评论,抢个沙发...