我正在尝试在函数中的循环外获取post id。php。但我的错误是:Notice: Trying to get property of non-object in functions.php on line 549
function theme_myeffecto_disable() {
global $wp_query;
$post_id = $wp_query->post->ID;
$showreaction = get_post_meta( $post_id, \'post_reaction_show\', true );
$showreaction = isset($showreaction) ? $showreaction : true;
var_dump($showreaction);
}
add_action( \'init\', \'theme_myeffecto_disable\', 20 );
以及
$showrating
总是有错误的天气,无论是真是假:(
SO网友:Alvic
您只需将其添加到模板中即可
<?php
global $post;
if($post >= 1) {
echo $post->ID;
}
?>
或者如果你想在你的功能中使用它。php并在任何地方使用它
function get_my_post_id() {
global $post;
if($post >= 1) {
echo $post->ID;
}
}
add_shortcode( \'post_id\', \'get_my_post_id\' );
// then add the shortcode in your template file where you want the post_id output
<?php echo do_shortcode(\'[post_id]\'); ?>