doutangshuan6473 2015-01-15 22:17
浏览 16

通过下拉选择的分类数据来提交动作和搜索

I know there are many questions asking the same thing, but i get lost when it comes about functions. I am using a php function stored in wordpress functions.php which i can call from template in order to get inside a dropdown all the data stored in a taxonomy

function custom_taxonomy_dropdown( $taxonomy, $exclude = array(''), $name,
 $show_option_all = null ) 
{
$args = array(
    'exclude' => $exclude,
    'order' => $order,
    'hide_empty' => 1,
    'order' => 'ASC',
    'echo' => 1
);
$terms = get_terms( $taxonomy, $args );
$name = ( $name ) ? $name : $taxonomy;
if ( $terms ) {
  printf( '<select name="%s" class="postform">', esc_attr( $name ) );
  if ( $show_option_all ) {
    printf( '<option value="0">%s</option>', esc_html( $show_option_all ) );
  }
  foreach ( $terms as $term ) {
    printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), 
esc_html( $term-    >name )     );
    }
    print( '</select>' );
    }
}

Template file where i call the function:

// Other func to get the ids of tags and categories to exclude in the dropdown; irrelevant
$noticias_ID = get_category_id('Noticias');
$estrenos_ID = get_tag_id('estrenos');
$proximamente_ID = get_tag_id('proximamente');
$peliculas_ID = get_tag_id('peliculas');

// Calling the function of above and writing parameters about taxonomies
custom_taxonomy_dropdown( 'category', ''.$noticias_ID, 'category', 'Select All' );
custom_taxonomy_dropdown( 'actor', '', 'actor', 'Select All' );
custom_taxonomy_dropdown( 'director', '', 'director', 'Select All' );
custom_taxonomy_dropdown( 'escritor', '', 'escritor', 'Select All' );
custom_taxonomy_dropdown( 'post_tag',
 ''.$estrenos_ID.','.$proximamente_ID.','.$peliculas_ID.','.$pending_ID.','.$pendiente_ID, 'post_tag', 'Select All' );

So, i get some pretty selectable dropdowns with all the taxonomy information. However, how can i pass the selected data in each of the dropdown to the submit action and therefore, click the submit button and get the correct results in my already done search.php?

<form action="<?php echo home_url('/'); ?>" method="get" class="">
<input type="submit" value="Submit">
</form>

EDIT: I have obtained the value for every select with $_GET and stored it on a variable like $value_get ($category_get, $director_get, etc...)

if(isset($_GET["category"]))
{
  $category_get = $_GET["category"];
}
if(isset($_GET["director"]))
{
  $director_get = $_GET["director"];
}
if(isset($_GET["actor"]))
{
  $actor_get = $_GET["actor"];
}
if(isset($_GET["escritor"]))
{
  $escritor_get = $_GET["escritor"];
}
if(isset($_GET["post_tag"]))
{
  $idioma_get = $_GET["post_tag"];
}

EDIT: So, by doing the last step i didn't knew if the next one was the query_posts($query_string), but anyways it didn't help me. Maybe i got confused here. Even when i made an echo of $query_string the query was different and it even was showing the pagename among it. So, although it is the best way to do it, i decided not to go further in there because i have wasted much time on this problem. So, i went by the easy way and decided to pass the variable values obtained by the isset($_GET["taxonomy"] to the $args and query it. It all resulted like this:

$category_ID = get_category_id(''.$category_get); // Calling function
$idioma_ID = get_tag_id(''.$idioma_get); // to get ID's by name
$args = array(
'cat' => ''.$category_ID.'',
'category_name' => ''.$category_get.'',
'tag' => ''.$idioma_get.'',
'tag_in' => ''.$idioma_ID.'',
'director' => ''.$director_get.'',
'escritor' => ''.$escritor_get.'',
'actor' => ''.$actor_get.'',
);

The reason that i included both cat and category_name was because the two words terms, so it's like a fix. 'Select All' option value must not be 0, as it will force all dropdowns to not stay on select all option but to select a different data. In my case, its value wasn't 0 it simply was: '' So, for future references, this is my problem already solved. My solution does not use the query string but uses the values obtained by $_GET which i can't consider completely right. The only answer made in this question helped me trough the problem but it didn't help me to pass the variables to the query, so i think i can't select a correct or complete answer here. If anyone else posts how to pass the correct data to $query_string that will be the correct answer.

  • 写回答

1条回答 默认 最新

  • duanjiao3754 2015-01-15 22:33
    关注

    If I understand your question you want to know how you can read the selected value in your search.php file.

    First, your form action should be action="path_to_searchphp/search.php".

    Then in search.php you can retrieve the selected values by accessing the $_GET[$name]. The value of this variable will be the value in the HTML form. From this you can figure which movie, etc was selected.

    So imagine your HTML is

    <form action="search.php" method="GET" class="">
      <select name="film">
        <option value="0">Film 1</option>
        <option value="1">Film 2</option>
        <option value="2">Film 3</option>
        <option value="3">Film 4</option>
      </select>
      <input type="submit" value="Submit">
    </form>
    

    And that the user selects Film 3. Then in your search.php the variable $_GET["film"] will be set and its value will be 2.

    You can verify if any option was selected by using the isset() function. For example

    if(isset($_GET["film"]))
    {
      $value = $_GET["film"];
    
      // do $value validation
      // do something with $value
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line