网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

Wordpress获得单个帖子ID

GG网络技术分享 2025-03-18 16:12 4


问题描述:

I\'m using a theme template and when i try to get post ID it returns the ID of the template not the ID of the actual single post.

the template ID is: 215

the post ID is: 1911

the following code will only output 215

function metavalue() {

global $post;

$meta = get_post_meta($post->ID, \'product_url\', true);

return $meta;

}

add_shortcode(\'url_short\', \'metavalue\');

get_the_ID(); the_id(); $post->ID; will also output 215. i need a way to get the actual single post ID so i can get the custom field value from \'product_url\'.

网友观点:

I\'ve contacted support with the theme authors on this topic as well but for the time being i\'ve found a way to work around it.

function metavalue() {

global $wp;

$url = home_url( $wp->request );

$correct_post_id = url_to_postid( $url );

$meta = get_post_meta($correct_post_id, \'product_url\', true);

return $meta;

}

add_shortcode(\'url_short\', \'metavalue\');

标签:

提交需求或反馈

Demand feedback