这篇关于wordpress根据页面别名获取页面数据的方法的文章,主要介绍wordpress根据页面别名获取页面数据的方法,觉得挺不错的,感兴趣的php开发者可以参考下,希望对大家在学习php的成长路上有所帮助!
最近用wordpress做了一个外包项目,记录一下自己写的一个通过页面别名获取页面数据的方法,代码如下:
/**
* 根据别名检索内容
*
* @global wpdb $ wpdb WordPress数据库抽象对象。
*
* @param string|array $page_slug 页面别名
* @param string $output 可选。 所需的返回类型。 OBJECT,ARRAY_A或ARRAY_N之一,对应于
* 分别是WP_Post对象,关联数组或数字数组。 默认OBJECT。
* @param string|array $post_type 可选。 帖子类型或帖子类型数组。 默认“页面”。
*
* @return WP_Post|array|null 成功时为WP_Post(或数组),失败时为null。
*/
function get_page_by_slug($page_slug, $output = OBJECT, $post_type = 'page') {
global $wpdb;
$op_symbol_slug = $op_symbol_type = '=';
if (is_array($page_slug)) {
$page_slug = esc_sql($page_slug);
$page_slug = '(' . implode("','", $page_slug) . ')';
$op_symbol_slug = 'IN';
}
if (is_array($post_type)) {
$post_type = esc_sql($post_type);
$post_type = '(' . implode("','", $post_type) . ')';
$op_symbol_type = 'IN';
}
$id = $wpdb->get_var(sprintf(
'SELECT ID FROM %s WHERE post_name %s %s AND post_type % %',
$op_symbol_slug,
$page_slug,
$op_symbol_type,
$post_type
));
return $id ? get_post($id, $output) : null;
}
将该函数拷贝到 /wp-includes/post.php 文件中去,即可全站使用。
总结
以上就是关于wordpress根据页面别名获取页面数据的方法全部内容,希望这篇wordpress根据页面别名获取页面数据的方法文章能够帮你解决如相关的PHP问题,更多请关注PHP栏目的其它相关文章!