WordPress 函数:如何利用get_post() 获取指定ID文章相关信息!

wordpress主题制作中可能需要单独调用指定文章的标题、链接或内容等,可以通过get_post()函数指定文章ID来获取文章标题、文章内容、文章链接、时间等文章相关信息。

WordPress 函数:如何利用get_post() 获取指定ID文章相关信息!

WordPress 函数:如何利用get_post() 获取指定ID文章相关信息!

get_post()函数用法:

<?php get_post( $post_id, $output );?>

参数说明:

  • ⭕$post_id:文章ID。必须传递一个含有整数的变量(如$id),如果直接添加数字会报错,这个需要注意。默认值为空。
  • ⭕$output:需要返回的参数,可选参数。object对象 – (默认) 返回对象模式,ARRAY_A – Returns an associative array of field names to values(返回字段名称关联数组);ARRAY_N – returns a numeric array of field values(返回数字数组)

get_post()函数返回文章参数:

ID(文章ID编号)
post_author(文章作者编号)
post_date(文章发表的日期和时间[格式:年-月-日 时-分-秒])
post_data_gmt(文章发表的格林尼治标准时间[缩写:GMT,格式:年-月-日 时-分-秒])
post_content(文章内容)
post_title(文章标题)
post_category(文章分类编号[注:在2.1之后的版本值总为0。所以定义文章的分类可使用get_the_category()函数])
post_excerpt(文章摘要)
post_status(文章状态包括已发布,准备发布,草稿,私人文章等等)
comment_status(评论状态包括开启评论,关闭评论,只要注册用户可以评论)
ping_status(pingback/trackback状态[关闭或者开启])
post_password(文章密码)
post_name(文章的URL嵌套)
to_ping(要引用文章的URL链接)
pinged(引用过的文章链接)
post_modified(文章最后修改时间[格式:年-月-日 时-分-秒])
post_modified_gmt(文章最后修改格林尼治标准时间[缩写:GMT,格式:年-月-日 时-分-秒])
post_type(文章类型包括页面,文章,附件)
comment_count(评论总数)

示例:获取指定ID文章内容

格式一:

<?php
// 获取文章ID编号为10的标题名称,返回对象数据格式
$post_id = 100; // 文章ID
echo get_post( $post_id )->post_content; // 输出文章的内容
?>

格式二:

<?php
// 获取文章ID编号为10的标题名称,返回字段关联数组数据格式
$post_id = 100;
$post = get_post($post_id, ARRAY_A); // 这样返回的值变成了数组形式
$post_title = $post['post_title'];
$post_date = $post['post_date'];
$post_content = $post['post_content'];
?>

实际应用场景

WordPress 发布文章,或定时发布文章,需要实时推送文章给百度收录

老古在主题下面的 functions.php 自定义的函数,实际应用如下:

// 百度百熊号(原创文章保护)
function post_baidu_xzh2($post_id){
date_default_timezone_set('Asia/Shanghai');
$post = get_post($post_id, ARRAY_A);
$post_date_gmt = $post['post_date_gmt'];
$post_content = $post['post_content'];
$post_date = $post['post_date'];
$now_date = date("Y-m-d H:i:s");
$old_date = date("Y-m-d H:i:s",strtotime("-3 day"));
// 未来定时发布,或新建文章,直接返回,不推送给百度收录
if($post_date > $now_date || $post_content == "" || $post_date_gmt == "0000-00-00 00:00:00") {
return;
}
$urls_old = array(
'https://www.wzxiu.com/sitemap-mimvp.xml',
'https://www.wzxiu.com/sitemap_baidu.xml',
'https://www.wzxiu.com/sitemap.html',
'https://www.wzxiu.com/sitemap.xml',
'https://www.wzxiu.com/index.php',
'https://www.wzxiu.com/',
'https://www.wzxiu.com/skill',
'https://www.wzxiu.com/color',
'https://www.wzxiu.com/music',
'https://www.wzxiu.com/fund',
'https://www.wzxiu.com/donate',
'https://www.wzxiu.com/about',
);
$urls = array();
$url = get_permalink($post_id);
array_push($urls, $url);
$retry_status = false;
$retry_NUM = 3;
$retry_idx = 0;
$api = 'http://data.zz.baidu.com/urls?appid=15512345678771&token=42gabcdef2qegx&type=realtime';
if($post_date < $old_date) {
$api = 'http://data.zz.baidu.com/urls?appid=15512345678771&token=42gabcdef2qegx&type=batch';
$urls = array_merge($urls, $urls_old);
$urls = array_unique($urls);
}
while($retry_status == false && $retry_idx < $retry_NUM) {
$retry_idx += 1;
$ch = curl_init();
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$result_json = json_decode($result, true);
$retry_status = $result_json["success_realtime"] == 1 ? true : false;
}
$result_json['retry_idx'] = $retry_idx;
$result_json['post_date'] = $post_date;
$result_json['now_date'] = $now_date;
$result_json['old_date'] = $old_date;
$from = "functions add_action";
$xzhFilename = "/var/www/html/wzxiu/post_baidu_xzh.txt";
$xzhFile = fopen($xzhFilename, 'a') or die("unable to open file");
fwrite($xzhFile, "\n\n\n===== $dtime -- from $from =====\n");
fwrite($xzhFile, $url . "\n");
fwrite($xzhFile, json_encode($result_json) . "\n");
fclose($xzhFile);
}
#add_action('save_post', 'post_baidu_xzh2', 10, 1); // 发布或更新文章时就会被触发
add_action('publish_post', 'post_baidu_xzh2', 10, 1); // 点击发布文章时就会被触发
add_action('publish_future_post', 'post_baidu_xzh2', 10, 1); // 到定时发布文章设定的时间点就会被触发

在老古先前写的一篇博客里 WordPress 发布文章触发动作钩子 ,可以查到动作钩子的函数参数和个数

动作钩子:

publish_post:参数一个($post_ID),点击发布文章时就会被触发,仅含已发布;(推荐)
save_post:参数一个($post_ID),发布或更新文章时就会被触发,包含已发布、定时未发布;
edit_post:参数两个($post_ID, $post),只要编辑已经存在的文章就会被触发;
publish_future_post:参数一个($post_ID),到定时发布文章设定的时间点就会被触发,如果设定的时间早于发布时间,不会被触发;(推荐)
transition_post_status:参数三个($new_status, $old_status, $post),实现功能比较强大,可以根据文章发布状态的变化实现一些功能。
{$old_status}_to_{$new_status}:参数一个($post),比如draft_to_publish是通常的文章发布事件,future_to_publish则可以用来处理定时文章发布事件。
post_updated:参数三个($post_ID, $post_after, $post_before),已存在文章更新了就会被触发。类似内核函数 wp_update_post()

sem运营未来有出路吗?未来前景怎么样呢?

上一篇

《勉为其难》原唱王冕因洗钱被判8年狱中近照曝光:身穿囚衣受访和记者开玩笑!

下一篇

99%的人还看了

发表评论

插入图片

欢迎登陆本站

 | 注册

必应搜索 站长推荐

在线工具

VIP视频解析

衡天云优质VPS 站长推荐

注册

登录

忘记密码 ?