自定义分类法使用JavaScript

时间:2017-10-24 07:45:16

标签: javascript php jquery wordpress

我开发了一个项目,我陷入了一个非常小的PHP专家:D我不是

我正在尝试制作自定义分类法的下拉列表,该列表适用于选择转到自定义分类法页面。

但经过大量搜索后,我找到了解决方案但没有采取行动去选择自定义分类法

首先我找到了

<?php wp_dropdown_categories( 'taxonomy=my_custom_taxonomy' ); ?>

第二我找到了

function fjarrett_custom_taxonomy_dropdown( $taxonomy ) {
$terms = get_terms( $taxonomy );
if ( $terms ) {
    printf( '<select name="%s" class="postform">', esc_attr( $taxonomy ) );
    foreach ( $terms as $term ) {
        printf( '<option value="%s">%s</option>', esc_attr( $term->slug ), esc_html( $term->name ) );
    }
    print( '</select>' );
}
}

当我插入任何页面代码滚滚时,我可以使用它

<?php fjarrett_custom_taxonomy_dropdown( 'my_custom_taxonomy' ); ?>
  

信用   https://frankiejarrett.com/2011/09/create-a-dropdown-of-custom-taxonomies-in-wordpress-the-easy-way/

但我现在不知道GONNA如何使其工作

希望你能帮助我找到解决方案,从上面任何一个解决方案我都可以选择并去做。

提前致谢

可能的答案 - 1

我找到了可能的答案

<form id="category-select" class="category-select" action="<?php echo esc_url( home_url( '/' ) ); ?>" method="get">

    <?php
    $args = array(
        'show_option_none' => __( 'Select category' ),
        'show_count'       => 1,
        'orderby'          => 'name',
        'name'             => 'cat',
        'echo'             => 0,
        'taxonomy'         => 'MyCustomTaxonomys',
        'value_field'      => 'slug'
    );
    ?>

    <?php $select  = wp_dropdown_categories( $args ); ?>
    <?php $replace = "<select$1 onchange='return this.form.submit()'>"; ?>
    <?php $select  = preg_replace( '#<select([^>]*)>#', $replace, $select ); ?>

    <?php echo $select; ?>

    <noscript>
        <input type="submit" value="View" />
    </noscript>

</form>

它给我网址

  

www.website.com/?cat=xxx其中xxx是我的自定义分类

但我想要网址

  

www.website.com/cat/xxx其中xxx是我的自定义分类

有可能吗?

3 个答案:

答案 0 :(得分:0)

可以使用这样的东西:

<?php
        function fjarrett_custom_taxonomy_dropdown( $taxonomy ) {
        $terms = get_terms( $taxonomy );
        if ( $terms ) {
        printf( '<select name="%s" class="postform">', esc_attr( $taxonomy ) );
            foreach ( $terms as $term ) {
            printf( '<option value="%s" data-location="%s">%s</option>', esc_attr( $term->slug ), get_term_link(term_id), esc_html( $term->name ) );
        }
    print( '</select>' );
}
}
?>

然后使用您的javascript代码重定向,具体取决于属性data-location

的值

答案 1 :(得分:0)

我这样做就像你用PHP用wordpres说的那样:

<?php function click_taxonomy_dropdown($taxonomy, $title) { ?>
  <select name="<?php echo $taxonomy;?>" id="<?php echo $taxonomy;?>">
  <option value="-1"><?php echo $title;?></option>
  <?php
  $terms = get_terms($taxonomy);
  foreach ($terms as $term) {
      $link = get_term_link( $term, $taxonomy );

      if($term->parent == 0){
    printf( '<option class="level-1" value="'.$link.'">%s</option>', $term->name );
  }
  }
  echo '</select>';
  ?>
<?php } ?>

然后用JS:

var dropdown1=document.getElementById("yourtaxonomy");
    function onCatChange1(){
      if(dropdown1.options[dropdown1.selectedIndex].value>"")
        location.href=dropdown1.options[dropdown1.selectedIndex].value
    }

JS只获取您选择的ID,然后在选择选项时转到值

答案 2 :(得分:0)

最后我找到了一个替代工作解决方案在这里写作,这样可以帮助有需要的人

<?php

  $categories = get_categories('taxonomy=xxxx');

  $select = "<select name='' id='cat' class='postform'>n";
  $select.= "<option value='-1'>Select category</option>n";

 foreach($categories as $category){
if($category->count > 0){
    $select.= "<option value='".$category->slug."'>".$category->name."
</option>";
  }
  }

  $select.= "</select>";

  echo $select;
?>

<script type="text/javascript">
    <!--
    var dropdown = document.getElementById("cat");

    function onCatChange() {
        if (dropdown.options[dropdown.selectedIndex].value != -1) {
            location.href = "<?php echo home_url();?>/yyyy/" + dropdown.options[dropdown.selectedIndex].value + "/";
        }
    }
    dropdown.onchange = onCatChange;
    -->
</script>

只需设置正确的XXXX和YYYY值并解决。

  

图片来源:John B. Hartley   http://www.johnbhartley.com/2012/custom-taxonomy-category-dropdown/

再次感谢大家的努力