我有一个带有显示下拉列表的短代码的页面。我只是不知道如何将该值返回到函数/页面以显示结果信息。第二页将获取该值并显示一个列表。我已经想尽了一切办法。如有任何建议,将不胜感激。示例代码将非常棒。非常感谢。
我想从一个短代码中传递一个值来显示不同的页面
1 个回复
SO网友:Mayeenul Islam
有很多方法可以做到这一点。下面是使用GET
方法和两个不同的短代码。您必须将代码放置在主题的functions.php
并创建两个不同的页面和位置[first-page]
第一页上的快捷代码,以及[second-page]
在第二页上进行快捷编码,并使用第一页上的表单。
<?php
/**
* Shortcode for the First page.
*
* Show form on the first page to grab information.
*/
function wpse380676_first_page_shortcode() {
$second_page_id = 1832;
ob_start();
?>
<form action="<?php echo get_the_permalink($second_page_id) ?>" method="GET">
<label><input type="radio" name="question" value="yes"> Yes</label>
<label><input type="radio" name="question" value="no"> No</label>
<button type="submit">Submit</button>
</form>
<?php
echo ob_get_clean();
}
add_shortcode( \'first-page\', \'wpse380676_first_page_shortcode\' );
/**
* Shortcode for the Second page.
*
* Handle data from the previous page and do something.
*/
function wpse380676_second_page_shortcode() {
$question = filter_input(INPUT_GET, \'question\', FILTER_SANITIZE_STRING);
if (!$question) {
return;
}
if (\'yes\' === $question) {
echo \'The answer is Yes\';
} else {
echo \'The answer is No\';
}
}
add_shortcode( \'second-page\', \'wpse380676_second_page_shortcode\' );
在WordPress 5.6中测试,默认主题为“二十二一”