我想做的是让用户在WordPress中编写帖子或页面时从下拉菜单中选择一个页面,然后将所选页面(user_selection)显示为额外的循环。
我所拥有的就是这个,它在主题中使用时工作正常:
<?php $user_selection = get_post_meta($wp_query->post->ID, 'user_selection', true); ?>
<?php
$id = $user_selection;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
但是当在插件中使用并添加到wp_head或wp_footer时,ID 不会被传递。我读过WordPress不会传递这样的变量,所以我(相当)有点卡住了。
有关如何解决此问题的任何帮助将不胜感激。
--------
更新。我通过以下方式让它工作:
<?php global $wp_query; $user_selection = get_post_meta($wp_query->post->ID, 'user_selection', true); ?>
<?php $id=$user_selection;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content; ?>
一切都很好,除了在已经存在的帖子上,滑出页面默认为当前帖子/页面(应默认为无)。保存帖子后,它可以工作,但这意味着重新保存所有帖子,这将是无稽之谈:)我错过了什么?
后端中的wp_dropdown_pages
输入如下:
<?php wp_dropdown_pages( array( 'name' => 'user_selection', 'id' => 'user_selection', 'selected' => $selected, 'show_option_none' => '-- no selection --', 'option_none_value' => '-1' ) ); ?>
谢谢!
----------
更新 2.我想我明白了:
<?php global $wp_query; $user_selection = get_post_meta($wp_query->post->ID, 'user_selection', true); ?>
<?php $var = 0; if (empty($user_selection)) { ?>
NO SELECTION MADE
<?php } else { ?>
<?php
$id=$user_selection;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
<?php } ?>
谢谢。
在传递信息的表单中,添加一个隐藏的输入字段,例如 <input type='hidden' id='id' name='id' value='<php echo $post->ID;>?'/>
。您将能够查询 ID。