Search code examples
phpblogsexpressionengine

ExpressionEngine: Looping Through all the Entries in One Category


I'm using expressionengine to create a documentation site, using the weblog module. I have a number of categories, which contain subcategories. Categories and subcategories contain entries.

I want to create a page for each category that outputs a nested list of all the child entries and subcategories within in that parent category. There should be a breadcrumb at the top that shows the category hierarchy with links to the parent categories.

Here is my code:

<!-- url /docs/category/category_id -->

<!-- Breadcrumb -->
<!-- This works on the page template, but on the category template it shows all the categories -->
{exp:weblog:entries weblog="docs" }
    {categories}
        <a href="{path='/category'}?category_id={category_id}&category_name={category_name}&category_description={category_description}">{category_name}</a> >
    {/categories}
    {title}
{/exp:weblog:entries}

<!-- List of Categories -->
<!-- This shows ALL of the categories. I want it to only show the parent category and its children -->

{exp:weblog:categories style="nested"}
    <h1><a href="{path='weblog/category'}"{category_name}</a></h1> 
    {exp:weblog:entries category="{category_id}"}
        <a href="{path='weblog/page'}">{title}</a>
    {/exp:weblog:entries}
{/exp:weblog:categories}

Solution

  • ---EDIT---

    I have since written a plugin that solves this problem:
    https://github.com/adambom/Category-Inheritance-Plugin-for-ExpressionEngine

    -------------

    Here's how I ended up doing it.

    To get a list of subcategories, I pass a URL querystring parameter category_id, and run this:

    <ul>
        {exp:query sql="SELECT cat_id as child_category_id, cat_name AS child_category_name FROM exp_categories WHERE parent_id = '<?php echo addslashes($_GET['category_id']) ?>' ORDER BY category_name ASC"}
            <li><a href="{path=/category/}?category_id={child_category_id}">{child_category_name}</a></li>
        {/exp:query}
    </ul>
    

    It only goes one level deep, but that's the best I could get it to do.

    This code outputs all the entries (had to hard code the url):

    {exp:weblog:entries category="<?php echo $_GET['category_id'] ?>"}
        <p><a href="/simulate/docs2/index.php/page/{entry_id}">{title}</a></p>
    {/exp:weblog:entries}
    

    As a side note, I would not recommend using expressionengine for much of anything. For a blog, use Wordpress. For a documentation site, use a wiki. We are going to be switching to a rails site, I think.