[英]wp_dropdown_categories - retain value after submisssion
我在表單中使用wp_dropdown_categories執行搜索以過濾自定義帖子類型中的帖子。
<form method="get" action="#anchor" id="findresult">
<?php wp_dropdown_categories('name=location&child_of=2'); ?>
<input id="submit" type="submit" value="SEARCH"/>
</form>
提交表單后,我希望下拉列表保留用戶選擇的值。
首先,我不確定是否可以閱讀下面鏈接中的文檔?
https://codex.wordpress.org/Function_Reference/wp_dropdown_categories
我知道提交后,我在$ GET數組中有位置ID,並且我知道可以使用以下命令從中獲取名稱:
// Get id of category
<?php $locid = $_GET["location"]; ?>
//Get category name from id
<?php $locname = get_cat_name( $locid ) ?>
// Echo name to show it's working as expected
<h1><?php echo $locname; ?></h1>
我不知道該怎么辦,是在提交表單后將其設置為下拉菜單的“值”。
感謝您一直以來的幫助。
根據wp_dropdown_categories文檔 ,您只需傳入selected
作為參數即可。
例:
<?php
// initialize these to prevent errors
$locname = '';
$locid = '';
// check if form is posted, to prevent notices
if ( ! empty( $_GET['location'] ) ) {
// Get id of category
$locid = $_GET["location"]; ?>
//Get category name from id
$locname = get_cat_name( $locid );
}
?>
// Echo name to show it's working as expected
<h1><?php echo $locname; ?></h1>
<form method="get" action="#anchor" id="findresult">
<?php
// use array notation for arguments. It's cleaner /neater
$args = array(
'name' => 'location',
'child_of' => 2
);
// if the location id is set, assign it to the arguments
if ( ! empty( $locid ) ) {
$args['selected'] = $loc_id;
}
wp_dropdown_categories( $args ); ?>
<input id="submit" type="submit" value="SEARCH"/>
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.