wordpress类别下拉选择链接已损坏

时间:2016-03-12 00:35:35

标签: javascript wordpress select hyperlink dropdown

我的wordpress博客上有一个博客类别下拉选择列表,以下代码为列表中的类别创建了一个链接:

    <script type="text/javascript">
        <!--
        var dropdown = document.getElementById("cat");
        function onCatChange() {
            if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                location.href = "<?php echo esc_url( home_url( '/' ) ); 
?>?cat="+dropdown.options[dropdown.selectedIndex].value;
            } 
        }
        dropdown.onchange = onCatChange;
        -->
    </script>

我想我需要编辑JavaScript,这样当我选择“所有帖子”选项时,它将链接到www.website.com/blog。 “所有帖子”的“价值”为0.我试过这个但它打破了所有类别的链接,并没有解决问题:

    <script type="text/javascript">
        <!--
        var dropdown = document.getElementById("cat");
        function onCatChange() {
            if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                location.href = "<?php echo esc_url( home_url( '/' ) ); 
?>?cat="+dropdown.options[dropdown.selectedIndex].value;
            } elseif( dropdown.options[dropdown.selectedIndex].value == 0 ){
                location.href = "<?php echo esc_url( home_url( '/' ) ); ?>blog";
            }
        }
        dropdown.onchange = onCatChange;
        -->
    </script>

继承人所需的html输出:

<select name='cat' id='cat' class='postform' >
    <option value='0'>Show all posts</option>
    <option value='-1'>Select category</option>
    <option class="level-0" value="1">Uncategorized</option>
    <option class="level-0" value="8">Blog</option>
</select>

1 个答案:

答案 0 :(得分:0)

我自己想出来......工作代码:

<script type="text/javascript">
    <!--
    var dropdown = document.getElementById("cat");
    function onCatChange() {
        if ( dropdown.options[dropdown.selectedIndex].value == 0 ) {
            location.href = "<?php echo esc_url( home_url( '/' ) ); ?>blog";
        } 
        else if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
            location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
        } 
    }
    dropdown.onchange = onCatChange;
    -->
</script>