在单个页面中获取自定义帖子类型的ID

我的wordpress模板中有一个名为posts的自定义帖子类型。

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

global $post;
echo $post->ID;

但是我可以获取类型为page或带有该行的帖子的ID。

我怎样才能做到这一点?

谢谢


#1


终于我找到了问题的答案。

当我使用register_post_type()方法注册帖子类型时, 应该使用flush_rewrite_rules()重新创建URL重写规则。

发生此问题是因为url重写规则导致我无法获取帖子内容, 因此在single.php页面中发布了id。


#2


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

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

来源:

https://www.srcmini02.com/66503.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37081.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?