在循环之外回显自定义页面的自定义字段

在循环之外回显自定义页面的自定义字段

问题描述:

我有一个包含特定自定义字段的自定义页面模板。我想在循环外显示这些自定义字段,但在同一页面内。在循环之外回显自定义页面的自定义字段

这一个工程:

<?php echo get_post_meta('244', 'custom_field_name', true) ?> 

但我要动态地工作,没有我进入页面的实际ID。

如何在echo中调用页面ID?

试试这个:

<?php 

    global $wp_query; 

    $postid = $wp_query->post->ID; 

    echo get_post_meta($postid, 'Your-Custom-Field', true); 

    wp_reset_query(); 

    ?> 
+0

就像一个魅力。谢谢! –

如果该调用是在循环,具有功能get_the_ID更换ID,此函数检索在WordPress循环当前项目的ID。

​​

参见:https://developer.wordpress.org/reference/functions/get_the_ID/


如果这一呼吁是在单页,更换对象项目$post->ID的ID。

$post = get_post(); 
<?php echo get_post_meta($post->ID, 'custom_field_name', true) ?> 

参见:https://codex.wordpress.org/Class_Reference/WP_Post


此外,你就可以用全局变量$post的访问后。

$post = get_post(); 
<?php echo get_post_meta($post->ID, 'custom_field_name', true) ?> 

参见:https://codex.wordpress.org/Global_Variables