首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在WordPress中从URL中获取Post ID

在WordPress中从URL中获取Post ID
EN

Stack Overflow用户
提问于 2017-04-25 06:39:17
回答 2查看 2.4K关注 0票数 0

我需要帮助,因为我想从URL获得帖子ID,因为我使用

代码语言:javascript
复制
?id='1'

我希望获得这个ID,并在另一个WordPress页面上获取数据。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-25 06:56:27

要从GET参数中获取值,可以使用filter_input()或WordPress get_query_var()方法。获得ID后,可以使用WP_Query查询post。

代码语言:javascript
复制
$post_id = filter_input(INPUT_GET, 'id');
if (!empty($post_id))
{
    $args = [
        'post_type' => 'bcworks', //<--replace it with your post_type
        'p' => $post_id
    ];
    // The Query
    $the_query = new WP_Query($args);
}
else
{
    //no post found
}

希望这能有所帮助!

票数 1
EN

Stack Overflow用户

发布于 2017-04-25 06:50:20

要用id获取另一个WordPress上的数据,请使用下面的代码

代码语言:javascript
复制
<?php

// would echo post 7's content up until the <!--more--> tag
$post_7 = get_post(7); 
$excerpt = $post_7->post_excerpt;
echo $excerpt

// would get post 12's entire content after which you
// can manipulate it with your own trimming preferences
$post_12 = get_post(12); 
$trim_me = $post_12->post_content;
my_trim_function( $trim_me );

?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43603384

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档