• 29-09-2014, 21:59:55
    #1
    Merhaba arkadaşlar,
    Wordpress'de kategorileri döngü içinde çekiyorum. yani tek satırlık default kodu kullanmıyorum.

    Kodum şu şekilde ;
    <?php foreach (get_categories() as $cat) :
    			?>
    				/* div ve kategori adı url çekme kodlarım */
    			<?php endforeach; ?>
    yalnız alt kategoriler gelmiyor. bunun için wordpress'in kendi sayfasında
    ( http://codex.wordpress.org/Function_Reference/get_categories )

    hierarchical diye bir kod vermiş. bu zaten default olarak 1 gelmekte. sanırsam alt kategoriler ile ilgili bir kod. onu da koduma eklememe rağmen gelmiyor.
  • 30-09-2014, 00:39:50
    #2
    Üyeliği durduruldu
    alt kategorilerinizde yazı yoksa ekrana yazmayabilir.
    hide_empty=0 kullanın

    $echo = '<ul>' . "\n";
    $childcats = get_categories('child_of=' . $cat . '&hide_empty=1');
    foreach ($childcats as $childcat) {
        if (1 == $childcat->category_parent) {
            $echo .= "\t" . '<li><a href="' . get_category_link($childcat->cat_ID).'" title="' . $childcat->category_description . '">';
            $echo .= $childcat->cat_name . '</a>';
            $echo .= '</li>' . "\n";
        }
    }
    $echo .= '</ul>' . "\n";
    echo $echo;
    en kötü ihtimal

    wp_list_categories kullanın
  • 30-09-2014, 15:08:00
    #3
    tolgatasci adlı üyeden alıntı: mesajı görüntüle
    alt kategorilerinizde yazı yoksa ekrana yazmayabilir.
    hide_empty=0 kullanın

    $echo = '<ul>' . "\n";
    $childcats = get_categories('child_of=' . $cat . '&hide_empty=1');
    foreach ($childcats as $childcat) {
        if (1 == $childcat->category_parent) {
            $echo .= "\t" . '<li><a href="' . get_category_link($childcat->cat_ID).'" title="' . $childcat->category_description . '">';
            $echo .= $childcat->cat_name . '</a>';
            $echo .= '</li>' . "\n";
        }
    }
    $echo .= '</ul>' . "\n";
    echo $echo;
    en kötü ihtimal

    wp_list_categories kullanın
    Yazı yokmuş
    Şuan kullandığım kodlar ile geldi ancak ben alt kategorileri
    <li>cat1
    <ul>
    <li>alt cat</li>
    </ul>
    </li>

    böyle yapacağım da ona göre bir kontrol edebileceğimiz kod yapısı varmı
  • 01-10-2014, 00:05:57
    #4
    Şu kodu bir dene;
    <ul>
        <?php $hiterms = get_terms("category", array("orderby" => "slug", "parent" => 0)); ?>
        <?php foreach($hiterms as $key => $hiterm) : ?>
            <li>
                <?php echo $hiterm->name; ?>
                <?php $loterms = get_terms("category", array("orderby" => "slug", "parent" => $hiterm->term_id)); ?>
                <?php if($loterms) : ?>
                    <ul>
                        <?php foreach($loterms as $key => $loterm) : ?>
                            <li><?php echo $loterm->name; ?></li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
    Eğer o kod olmazsa aşağıdaki kodu dene;
    functions.php e yazdır;
    /**
     * Recursively sort an array of taxonomy terms hierarchically. Child categories will be
     * placed under a 'children' member of their parent term.
     * @param Array   $cats     taxonomy term objects to sort
     * @param Array   $into     result array to put them in
     * @param integer $parentId the current parent ID to put them in
     */
    function sort_terms_hierarchicaly(Array &$cats, Array &$into, $parentId = 0)
    {
        foreach ($cats as $i => $cat) {
            if ($cat->parent == $parentId) {
                $into[$cat->term_id] = $cat;
                unset($cats[$i]);
            }
        }
    
        foreach ($into as $topCat) {
            $topCat->children = array();
            sort_terms_hierarchicaly($cats, $topCat->children, $topCat->term_id);
        }
    }
    Kullanım şekli;
    $categories = get_terms('category', array('hide_empty' => false));
    $categoryHierarchy = array();
    sort_terms_hierarchicaly($categories, $categoryHierarchy);
    
    var_dump($categoryHierarchy);
    Not: Kodları denemedim. Bir araştırma yaparken denk gelmişti tekrar buldum senin için
  • 01-10-2014, 15:39:48
    #5
    Tamamdır hallettim
    Başka kod yapısı kullanınca oldu.