在单页中获取自定义 post 类型的 id

get the id of custom post type in single page

我的 wordpress 模板中有一个自定义 post 类型,名为 downloads

问题是我无法使用这些代码在单个页面中获取此类型的 post 的 ID

global $post;
echo $post->ID;

但是我可以得到 posts 的 id,类型为 page 或 post,行。

我该怎么做?

谢谢

终于找到问题的答案了。

当我使用 register_post_type() 方法注册 post 类型时,我应该使用 flush_rewrite_rules() 重新创建 url 之后重写规则。

出现此问题是因为 url 重写规则导致我无法获取 post 内容,因此 single.php 页面中的 post id。

$args = array( 'post_type' => 'portfolio');

$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    the_ID();
endwhile;