是否按层次对类别进行排序?

时间:2012-08-06 作者:onur

我想按层次排列我的类别。但我想使用如下自定义选择名称、id和类:

<select name="kategorisec" id="cat" class="postform">
...
</select>
我想这样使用我的自定义值:

<option value="<?php echo $cat->slug; ?>"><?php echo $cat->name; ?></option>
但它不按层次排序。如何按层次(带级别)对类别进行排序?提前谢谢。我的所有代码:

<?php $args2 = array(  
\'taxonomy\'     => \'fgaleri\',    
\'orderby\'      => \'name\', 
\'show_count\'   => 1,
\'hierarchical\' => 1,    
\'title_li\'     => \'\'  
);
?>
<?php $cats = get_categories($args2); ?>
<select name="kategorisec" id="cat" class="postform">
<option value="">Kategori Seç ve Ara</option>
<?php foreach ($cats as $cat) : ?>    
<option value="<?php echo $cat->slug; ?>">
<?php echo $cat->name; ?></option>  
<?php endforeach; ?>

1 个回复
SO网友:onur

我做到了!我找到了一个函数,使option value=\'slug\'。

功能:

class SH_Walker_TaxonomyDropdown extends Walker_CategoryDropdown{
function start_el(&$output, $category, $depth, $args) {
    $pad = str_repeat(\'&nbsp;\', $depth * 3);
    $cat_name = apply_filters(\'list_cats\', $category->name, $category);
    if( !isset($args[\'value\']) ){

        $args[\'value\'] = ( $category->taxonomy != \'category\' ? \'slug\' : \'id\' );
    }
    $value = ($args[\'value\']==\'slug\' ? $category->slug : $category->term_id );
    $output .= "\\t<option class=\\"level-$depth\\" value=\\"".$value."\\"";
    if ( $value === (string) $args[\'selected\'] ){ 
        $output .= \' selected="selected"\';
    }
    $output .= \'>\';
    $output .= $pad.$cat_name;
    if ( $args[\'show_count\'] )
        $output .= \'&nbsp;&nbsp;(\'. $category->count .\')\';
    $output .= "</option>\\n";
    }
}
和我的所有代码:

<?php /** * Displays the Pagination in Custom loop * */?>
<?php get_header(); ?>
<?php global $wp_query;
$aranan= $_GET[\'s\']; 
$kategorim = intval($_GET[\'cat\']);
$kateg1= $_GET[\'kategorisec\'];
if (intval($kateg1)<0) { $kateg = ""; } else { $kateg = $kateg1; }
$myterm = get_term( $kategorim, \'fgaleri\' ); 
global $myslug;
$myslug = $myterm->slug; ?>
<div id="content">
<?php $rakam= 0;
//The query
$wp_query->query( array( \'post_type\' =>\'fotograf\', \'s\' => $aranan, \'paged\' => $paged, \'taxonomy\' => \'fgaleri\', \'fgaleri\' => $kateg ) );
//The loop
?><div id="FGaleri_2"><div id="FGaleri_Ic"><div id="FGaleriBaslik">Fotoğraf Galerisi     <form action="<?php echo get_bloginfo(\'wpurl\'); ?>" id="searchformfoto" method="get" name="searchform">
<?php $args = array( \'walker\'=> new SH_Walker_TaxonomyDropdown(), \'class\' => \'postform\', \'id\' => \'kategorisec\', \'name\' => \'kategorisec\', \'value\'=>\'slug\', \'taxonomy\' => \'fgaleri\', \'show_option_none\' => \'Kategori Seç ve Ara\', \'hierarchical\' => 1 ); wp_dropdown_categories($args); ?>
<div id="aramabolumufoto">
<input name="s" id="sfoto" type="text" value=""/>
<input type="hidden" name="post_type" value="fotograf" />
<input id="searchsubmitfoto" type="submit" value="Ara"/>
</div></form></div>
<div class="FGaleri_Ic_2">
<?php while ($wp_query->have_posts()): $wp_query->the_post(); $rakam++; ?>
<div class="FGaleriItem2" style="<?php if ($rakam % 5 == 0 ) { ?>margin-right: 2px;<?php } ?>">
<?php 
$termargs=array(\'orderby\' => \'count\', \'order\' => \'desc\');
$terms = wp_get_post_terms( $post->ID , \'fgaleri\', $termargs );
$t = count($terms);
if ($t>0) { ?>
<div class="fkategori">
<?php $c = 0;
foreach($terms as $term) {  
$c++;  
if ($c==$t)   
{   echo \'<a class="foto_kategori" href="\'. home_url() . \'/?kategorisec=\' . $term->slug . \'&post_type=fotograf" title="\' . $term->name . \'" \' . \'>\' . $term->name.\'</a> \';  }} ?>
</div><?php } ?>
<div class="ResimUst">
<?php if (has_post_thumbnail()) { ?>
<a href="<?php the_permalink() ?>">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );$url2 = get_bloginfo(\'template_directory\') . "/timthumb.php?src=" . $url . "&amp;w=100&amp;h=100&amp;zc=1"; ?>
<img src="<?php if(get_option(\'of_timthumb\')=="true") { echo $url2; } else { echo $url; } ?>" alt="" title="" width="100" height="100" /></a>
<?php } ?>
</div><div class="ResimAlt"><a href="<?php the_permalink() ?>">
<?php the_title(); ?>
</a></div></div>
<?php if (($rakam % 5 == 0 ) && ($rakam > 4)) { ?><div class="clearboth"></div><?php } ?>
<?php endwhile; ?></div></div></div><div class="clearboth"></div>
<?php if(function_exists(\'wp_pagenavi\') ) {     
wp_pagenavi(); 
//function call for plugin pagination( wp pagenavi plugin)
} else { ?>     
<div id="navigasyon"><span class="previous-entries">
<?php next_posts_link(\'« Daha Eski Fotoğraflar\'); ?></span>
<span class="next-entries"><?php previous_posts_link(\'Daha Yeni Fotoğraflar »\'); ?></span>
</div><?php } //endif ?><?php ?></div><?php get_sidebar(); ?></div><?php get_footer(); ?>

结束

相关推荐

NEXT_POST_LINK()中的EXCLUDE_CATEGORIES参数行为异常

我有一个Wordpress模板。php页面。该页面有“下一页”和“上一页”箭头,允许浏览所有帖子。我想将某些类别的帖子排除在“下一个”和“上一个”计算中。我有以下代码: // in single.php next_post_link( \'%link\', \'&larr; Previous\', false, \'11 and 13 and 15\'); 这应该会显示到下一篇文章的链接。第11、13和15类的职位不应按照the $ignore_categories para