ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Category / Child Categories, am I over engineering this?

October 30, 2008 8:18am

Subscribe [2]
  • #1 / Oct 30, 2008 8:18am

    haydenp

    46 posts

    I’m concerned I am over engineering this as a result of overlooking something small and hope someone can point me in the right direction.

    BRIEF:
    I have a weblog “directory”. It has a category group with Parent categories and Child categories in the format:

    Accommodation
      - Hotel
      - B&B
    Eating Out
      - Restaurant
      - Café

    I have a sidebar menu with these parent & child categories listed.

    A) If I click on a URL linking to a Child category all the blogs under that child category are displayed correctly. Example URL:

    http://www.mywebsite.com/directory/category/hotel/

    This is using the standard {weblog:entries} {/exp:weblog:entries} tag pair.

    B) If I click on a URL linking to a Parent category I would LIKE all the blogs under any of its Child categories to display … BUT this is not working. Example URL

    http://www.mywebsite.com/directory/category/eating-out/

    I tried using a combination of tag pairs to get (B) above to work but nothing seems to work. My latest attempt was as per this code below.

    {exp:weblog:category_heading weblog="directory"}
    
    {exp:child_categories parent="{category_id}" show_empty="yes"}
    
    {exp:weblog:entries weblog="directory" category="{child_category_start}{child_category_id}|{child_category_end}" orderby="entry_date" sort="desc" limit="5" paginate="bottom"}
    
    {bd_summary}
    
    {paginate}
    Page {current_page} of {total_pages} pages {pagination_links}
    {/paginate}
    
    {/exp:weblog:entries}
    
    {/exp:child_categories}    
    
    {/exp:weblog:category_heading}

    I did have a LITTLE success with the code above when removing the {weblog:entries} {/exp:weblog:entries} tag pair into a template of its own and then embedding it for each {child_category_id} for example

    {exp:weblog:category_heading weblog="directory"}
    
    {exp:child_categories parent="{category_id}" show_empty="yes"}
    
    {child_category_start}
    
    {embed="directory/.dir_list" this_id="{child_category_id}"}
    
    {child_category_end}
    
    {/exp:child_categories}    
    
    {/exp:weblog:category_heading}

    BUT … this does not help with PAGINATION as the {entries} tag is repeated for each {child_category_id} resulting in pagination for each.

    Am I over engineering this or is it impossible using standard tags and plugins, forcing me to have to resort to QUERIES and PHP?

    Thanks,

    Hayden

  • #2 / Oct 30, 2008 12:06pm

    PhireGuys

    525 posts

    Maybe I am under thinking this but I believe I did something similar with very little work.

    What I did was put in the URL the category ID (EE can do it for you in your links if you create the links with the weblog:categories tag).  So the URL looked like: http://www.mywebsite.com/directory/C25/  where 25 was the ID of my category.

    Then I simply did a regular weblog:entries tag and it pulled everything I needed.  For my categories, it pulled the subcategories, and for the subcategories it pulled just the subcategories.

    Is this what you’re trying to accomplish?

  • #3 / Oct 30, 2008 1:51pm

    haydenp

    46 posts

    “Is this what you’re trying to accomplish?” ... yes, 100%

    BUT ... I’m, not overly keen on using the Category ID method you mention. I would prefer to use the Category Title in the URL as apposed to the Category ID.

    For example:
    mywebsite .com/directory/category/hotel/ … as apposed to … mywebsite .com/directory/category/C25/

  • #4 / Oct 30, 2008 2:10pm

    PhireGuys

    525 posts

    I would think you’d need to use SQL statements along with PHP to accomplish this.

    If your url is mywebsite.com/index.php/directory/category/hotel/ then {segment_4} will hold your category title.  {segment_4} is an actual variable that you can use.

    What you’ll want to do is connect to your SQL database, setup a query that looks for this title and gives you the category ID.  Then you take this category ID and put it into your weblog:entries tag, so it would be category=”<?php echo $thevariable; ?>.

    Make sure to set PHP to INPUT when doing this.  Here is a basic framework but if you copy and paste it won’t work since I probably don’t have the table names right.

    <pre><code><?php
    $iscategory = false; //let us know if category is in URL

    if ({segment_3} == “category”)
    {
    $category_title = "{segment_4}”;
        $iscategory = true;

        $sql = “SELECT id FROM thecorrecttable WHERE title_url = $category_title”;
        $result = mysql_query($sql);
        $mycategory = mysql_fetch_array($result, MYSQL_ASSOC);

        $currentcat = $mycategory[‘id’];  //store the id of the category
    }
    ?>


    <?php

    if ($iscategory) //check if it is true
    {
    ?>
    {exp:weblog:entries category="<?php echo $currentcat; ?>"}
        ...
        {/exp:weblog:entries}

    <?php
    }
    else
    {
    //do something else
    }
    ?></code></pre>

    I did this really quick so there might be errors but this is essentially what you want.  I’m making the assumption that if you put a parent category there you will get anything in the sub categories.  (There is a setting in the Admin panel to disable this, but default is that it is on)

  • #5 / Oct 30, 2008 2:23pm

    haydenp

    46 posts

    Ok Ok!

    Phire_SK thanks for your reply. I played with a few things and what I realised is when I create a blog I have ONLY allocated it to it’s Child category and not to BOTH the Parent category and its Child category.

    For example:
    A blog allocated to “Hotels” and not to “Accommodation” (parent) and “Hotels” (child) will only appear via the Child category link eg. /directory/category/hotels/ ... and NOT via its parent category link eg./directory/category/accommodation/

    Surely this is incorrect in the {entries} tag? Would this mean that if I have a category tree 10 deep I would have to allocate an entry to every Parent category UP its tree order for it to work correctly?

    For example:

    Acommodation
      - Hotels
          - 5 Star
            - Close to airport

    If I added a 5 star hotel, close to the airport I would have to allocate it to “Acommodation” and to “Hotels” and to “5 Star” and to “Close to airport” for it to show up if the user were to click on any of the above categories? Should I not simply have to allocate it to “Close to airport” and then the {entries} tag take care of the rest?

  • #6 / Oct 30, 2008 3:46pm

    PhireGuys

    525 posts

    Under ADMIN > WEBLOG ADMINISTRATOR > GLOBAL WEBLOG PREFERENCES

    There is an option called: Auto-Assign Category Parents

    Make sure this is set to Yes.  Basically, it should auto assign anything that is given a subcategory to the category itself.  I don’t know how deep it will go, I assume all the way to the top.

  • #7 / Oct 30, 2008 5:18pm

    haydenp

    46 posts

    BINGO!

    Like I said ... “am I over engineering this?”, and the answer to my question is ... YES!

    Phire_SK thanks for the input. I changes that setting under Global Weblog Preferences as suggested and all problems solved.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases