簡體   English   中英

在層次結構的一頁上顯示所有類別和帖子

[英]Displaying all categories and posts on one page in a hierarchy

我的客戶堅持將所有類別和帖子顯示在一頁上。 我知道這是個壞主意,但我需要這樣做。

無論如何,結構是這樣的:

一級類別標題>二級類別標題> ...>二級類別標題>帖子內容。

這將以HTML格式進行組織,如下所示:

<div class="cat primary">
    <h2>1st Level Category Title</h2>
    <div class="cat secondary">
        <h3>2nd Level Category Title</h3>
        <div class="cat tertiary">
            <h4>3rd Level Category Title</h4>
            ...
                <div class="cat tertairy">
                    <h4>nth Level Category Title</h4>
                    <div class="product">
                        <p>Post Content</p>
                    </div>
                </div>
            ...
        </div>
    </div>
    <div class="cat secondary">
        <h3>2nd Level Category Title</h3>
        <div class="product">
            <p>Post Content</p>
        </div>
    </div>
</div>

功能說明:

  • 第一個類別為“主要”類別,第二個為“第二”類別,以及所有后續的“第三”類別。 如果需要,我可以使用CSS來解決。
  • <h2>用於主要標題, <h3>用於輔助標題, <h4>用於所有后續標題。 如果需要,我可以使用CSS來解決。
  • 樹中最深的類別級別顯示該類別中的所有產品。

我已經嘗試過使用get_terms()get_categories() ,但是我無法弄清楚如何判斷某個類別是否處於最深層次,而且我也無法弄清楚如何無限深入該類別。樹(我最終不得不為每個新層重復我的代碼)。

我目前正在對此進行試驗:

$categories = get_terms("product-category"); 
if ($categories && !is_wp_error($categories)) {
    foreach($categories as $category) {
        $children = get_terms("product-category", array(
            "parent"   => $category->term_id,
        ));
        if (count($children) == 0) {
            echo $category->name;
        }
    }
}

這確實會檢查它是否在樹中最深,但實際上並沒有構造樹。 我會繼續努力,並報告任何進展。 幫助將不勝感激。


更新4:在@Nemutaisama的大力幫助下,我得以弄清楚這一點! 這是我的最終代碼(從下面的回答中略作修改):

function loadCategories($categories, $level) {
    foreach($categories as $category) {
        $cat_class = "";
        $heading_tag = "";
        if ($level == 1) {
            $cat_class = "primary";
            $heading_tag = "h2 style='text-align:center;'";
        } elseif ($level == 2) {
            $cat_class = "secondary";
            $heading_tag = "h3";
        } else {
            $cat_class = "tertiary";
            $heading_tag = "h4";
        }
        echo "<section class='cat $cat_class'>";
        echo "<header>";
        echo "<$heading_tag>{$category->name}<button>Expand</button></$heading_tag>";
        if ($level == 1) {
            echo "<hr  class='short' />";
        }
        echo "</header>";
        if ($level > 1) {
            echo "<div class='expander'>";
        }
        $children = get_terms("product-category", array(
            "parent"   => $category->term_id,
        ));
        if (count($children) == 0) {
            $posts = get_posts(array(
                "post_type" => "products",
                "tax_query" => array(
                    array(
                        "field"    => "term_id",
                        "taxonomy" => "product-category",
                        "terms"    => $category->term_id,
                )),
            ));
            foreach ($posts as $post) {
                if ($level < 2) {
                    $cat_class = "secondary";
                    $heading_tag = "h3";
                } else {
                    $cat_class = "tertiary";
                    $heading_tag = "h4";
                }
                echo "<section class='cat $cat_class'>";
                echo "<header><$heading_tag>{$post->post_title}<button>Expand</button></$heading_tag></header>";
                echo "<div class='expander'>";
                echo "<article>";
                if (get_field("product_number", $post->ID)) {
                    echo "<div class='productNumber'><p># " . get_field("product_number", $post->ID) . "</p></div>";
                }
                echo "<div class='content'>";
                echo wpautop($post->post_content);
                echo "</div><!--/.content-->";
                echo "</article>";
                echo "</div><!--/.expander-->";
                echo "</section><!--/.cat.$cat_class-->";
            }
        }
        loadCategories($children, $level+1);
        if ($level > 1) {
            echo "</div><!--/.expander-->";
        }
        echo "</section><!--/.cat.$cat_class-->";
    }
}
$categories = get_terms("product-category", array(
    "parent" => 0,
));
if ($categories && !is_wp_error($categories)) {
    loadCategories($categories, 1);
}

我認為遞歸函數將為您提供幫助。 像這樣的東西

function loadCategories($categories, $level) {
    foreach($categories as $category) {
        $children = get_terms("product-category", array(
            "parent"   => $category->term_id,
        ));
        $cat_class = "";
        $heading_tag = "";
        if ($level == 1) {
            $cat_class = "primary";
            $heading_tag = "h2 style='text-align:center;'";
        } elseif ($level == 2) {
            $cat_class = "secondary";
            $heading_tag = "h3";
        } else {
            $cat_class = "tertiary";
            $heading_tag = "h4";
        }
        echo "<section class='cat $cat_class'>";
        echo "<header>";
        echo "<$heading_tag>{$category->name}<button>Expand</button></$heading_tag>";
        if ($level == 1) {
            echo "<hr  class='short' />";
        }
        echo "</header>";
        if ($level > 1) {
            echo "<div class='expander'>";
        }
        if (count($children) == 0) {
            $posts = get_posts(array(
                "post_type" => "products",
                "tax_query" => array(
                    array(
                        "field"    => "term_id",
                        "taxonomy" => "product-category",
                        "terms"    => $category->term_id,
                )),
            ));
            foreach ($posts as $post) {
                if ($level < 2) {
                    $cat_class = "secondary";
                    $heading_tag = "h3";
                } else {
                    $cat_class = "tertiary";
                    $heading_tag = "h4";
                }
                echo "<section class='cat $cat_class'>";
                echo "<header><$heading_tag>{$post->post_title}<button>Expand</button></$heading_tag></header>";
                echo "<div class='expander'>";
                echo "<article>";
                if (get_field("product_number", $post->ID)) {
                    echo "<div class='productNumber'><p># " . get_field("product_number", $post->ID) . "</p></div>";
                }
                echo "<div class='content'>";
                echo wpautop($post->post_content);
                echo "</div><!--/.content-->";
                echo "</article>";
                echo "</div><!--/.expander-->";
                echo "</section><!--/.cat.$cat_class-->";
            }
        }
        loadCategories($children, $level+1);
        if ($level > 1) {
            echo "</div><!--/.expander-->";
        }
        echo "</section><!--/.cat.$cat_class-->";
    }
}
$categories = get_terms("product-category");
if ($categories && !is_wp_error($categories)) {
    loadCategories($categories, 1);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM