Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在WordPress循环中显示每篇文章中的php变量_Php_Wordpress_Variables - Fatal编程技术网

如何在WordPress循环中显示每篇文章中的php变量

如何在WordPress循环中显示每篇文章中的php变量,php,wordpress,variables,Php,Wordpress,Variables,我有一个自定义的post类型,在每个post中我都有一个名为$grandtotal的变量。$grandtotal的值是根据在post前端表单上输入的数据计算得出的结果 在我的帖子模板中,我有以下内容: <?php echo '<h5>Grand Total: '. $grandtotal . '</h5>' ?> 然后在循环中,我用这个来调用它: <?php echo get_post_meta( get_the_ID(), 'grandtotal'

我有一个自定义的post类型,在每个post中我都有一个名为
$grandtotal
的变量。
$grandtotal
的值是根据在post前端表单上输入的数据计算得出的结果

在我的帖子模板中,我有以下内容:

<?php echo '<h5>Grand Total: '. $grandtotal . '</h5>' ?>
然后在循环中,我用这个来调用它:

 <?php echo get_post_meta( get_the_ID(), 'grandtotal', true ); ?>

您可以将变量值存储为自定义Post Meta,并且可以使用“是”在循环中获取值

update_post_meta($postid, meta_key, value); 
e.g. update_post_meta(232,"_grandtotal", 200);

$value= get_post_meta(232,"_grandtotal");

你好,大卫,谢谢你!您必须指定一个帖子ID吗?我查了一下法典,看起来你查过了。我想对无限数量的帖子执行此操作,因此我宁愿它只是“当前帖子ID”,而不是每次都必须指定它。可能吗?或者,等等。。。我可以使用$post_id,这应该会给出当前id。。。我想?我试试看,好的。这起作用了。我用获取ID()来代替特定ID。谢谢!
$value= get_post_meta(232,"_grandtotal");