custom browse by

时间:2017-02-20 作者:Greg Skala

我正在尝试创建自己的简单侧栏,以显示以下内容:

browse by archives (monthly) - it works
brows by categories (only main, no subcategories) does not work, misisng ref link.
browse by tag (all tags with links). does not work. Could someone look at the code and let me know what i do wrong.
我尝试对以上所有内容使用相同的选择选项下拉结构。代码如下。谢谢你的帮助

<div class="tag-archive" style="margin: 20px 0;"> 
    <select class="" name="archive-dropdown"  onchange="document.location.href = this.options[this.selectedIndex].value;">
        <option value=""><?php echo esc_attr(__(\'Wybierz Miesiąc\')); ?></option>  
        <?php wp_get_archives(array(\'type\' => \'monthly\', \'format\' => \'option\', \'show_post_count\' => 1)); ?>
    </select>                
</div>


<div class="tag-category" style="margin: 20px 0;">  
    <?php
    echo \'<select name="categories">\';
    // Add custom option as default
    echo \'<option>\' . __(\'Select categories\', \'text-domain\') . \'</option>\';
    // Get categories as array
    $args = array(
        \'hide_empty\' => 0,
        \'taxonomy\' => \'category\',
        \'orderby\' => \'name\',
        \'order\' => \'ASC\',
        \'show_count\' => true,
        \'hierarchical\' => true
    );
    $categories = get_categories($args);
    foreach ($categories as $category) :
        // Check if current term ID is equal to term ID stored in database
        $selected = ( $stored_category_id == $category->term_id ) ? \'selected\' : \'\';
        echo \'<option value="\' . $category->term_id . \'" \' . $selected . \'>\' . $category->name . \'</option>\';
    endforeach;
    echo \'</select>\';
    ?>

<div class="tag-cloud" style="margin: 20px 0;">
        <?php
        echo \'<select class="tags">\';
        // Add custom option as default
        echo \'<option>\' . __(\'Select Tag\', \'text-domain\') . \'</option>\';
        // Get categories as array
        $tags = get_tags(array(\'orderby\' => \'count\', \'order\' => \'DESC\'));
        foreach ((array) $tags as $tag){
        // Check if current term ID is equal to term ID stored in database
            $selected = ($tag == $tag->term_id ) ? \'selected\' : \'\';
        echo \'<option value="<a href="\' . get_tag_link ($tag->term_id) . \'" rel = "tag">\' . $tag->name . \'(\' . $tag->count . \')\'.\'</option>\';
        }
        echo \'</select>\';
        ?>                  
 </div>

1 个回复
SO网友:David Lee

在标记代码中,缺少几个引号字符$tag var,你不能endforeach 就像这样,我为它添加了括号:

<div class="tag-cloud" style="margin: 20px 0;">
        <?php
        echo \'<select class="tags">\';
        // Add custom option as default
        echo \'<option>\' . __(\'Select Tag\', \'text-domain\') . \'</option>\';
        // Get categories as array
        $tags = get_tags(array(\'orderby\' => \'count\', \'order\' => \'DESC\'));
        foreach ((array) $tags as $tag){
        // Check if current term ID is equal to term ID stored in database
            $selected = ($tag == $tag->term_id ) ? \'selected\' : \'\';
        echo \'<option value="<a href="\' . get_tag_link ($tag->term_id) . \'" rel = "tag">\' . $tag->name . \'(\' . $tag->count . \')\'.\'</option>\';
        }
        echo \'</select>\';
        ?>                  
    </div>

相关推荐

WP_DROPDOWN_CATEGORIES-如何在Widget中保存?

我想用wp_dropdown_categories 在自定义小部件中。所有内容都显示得很好,但由于某些原因,无法正确保存。这是form() 和update() 小部件的功能-我做错什么了吗?public function form( $instance ) { /* Set up some default widget settings. */ $defaults = array( \'title\' => \'Classes by Category\' );&#x