Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End WordPress loop organized by categories

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #162639
    Mike Dorr
    Participant

    I’m trying to use WP for a database of teaching resources, like flashcards, worksheets, games, etc. Each one of these resources will be a separate wordpress post, and each resource goes along with one (or more) of our audio CDs (broken down by song).

    I’m am using multiple categories per resource to specify where it belongs. Like a set flashcards for numbers 1-100 will be categorized in “Flashcards”, “CD1”, and “The Counting Song”. Likewise, a worksheet for learning animals will be categorized in “Worksheets”, “CD4”, and “Old McDonald Had A Farm”.

    From the navigation menu, if someone clicks on “CD1”, I would like for the page to display all the resources that go with CD1. This is easy, and pretty much built in – no problem.

    I would also like the page to be organized by resource type, grouping all the flashcards for CD1 together in a div on the page with a label “Flashcards”. Below that div, there would be another div labeled “Worksheets” that all the worksheets for CD1 go in. But, if there are no worksheets for CD1, I do not want the section to show up on the page.

    I’ve tried using query_posts and specified a category name like “Flashcards”, but that seems to query all posts, not just the posts for CD1 (if I were on the CD1 page).

    So, on the CD1 page (category=cd1), here is the code I’d want to display:

    <div class="resourceType">
         <h2>Flashcards</h2>
         <div class="result">Flashcard Set 1</div>
         <div class="result">Flashcard Set 2</div>
         <div class="result">Flashcard Set 3</div>
    </div>
    
    <div class="resourceType">
         <h2>Worksheets</h2>
         <div class="result">Worksheet 1</div>
         <div class="result">Worksheet 2</div>
         <div class="result">Worksheet 3</div>
    </div>
    

    I hope this makes sense, and if anyone can shine any light on this, it would be greatly appreciated.

    -Mike

    #163138
    Mike Dorr
    Participant

    Hi shaneisme,

    Thanks for your reply. I had found that article before posting to the forum, but it seems that using that will query all my posts…am I doing something wrong? If I visit the CD1 page, I want it to query the posts under CD1, and show the flashcards and worksheets that go with it separately.

    Any idea how to do this?

    -Mike

    #163251
    Mike Dorr
    Participant

    Hi shaneisme,

    Here is what I’m using now. It seems to be working, but when there are no worksheets for something, it’s still printing that h2 for worksheets. I just started trying to figure out how to use pre_get_posts, but I can’t seem to figure that out either.

    <?php if(have_posts()) : ?>
    
    <div class="wrapCat">
        <h2 class="catTitle">Worksheets</h2>
        <?php while(have_posts()) : the_post(); global $post; ?>
                <?php if (in_category('5') ): ?>
                    Show worksheets
                <?php endif; ?>
        <?php endwhile; ?>
    </div>
    
    <div class="wrapCat">
        <h2 class="catTitle">Flashcards</h2>
        <?php while(have_posts()) : the_post(); global $post; ?>
                <?php if (in_category('6') ): ?>
                    Show flashcards
                <?php endif; ?>
        <?php endwhile; ?>
    </div>
    
    #163257
    Mike Dorr
    Participant

    Thanks for your help!

    It seems as though the if statement you gave me (get_category(‘6’)->category_count > 0) is querying the entire database (not just the posts in the current loop), and so always coming out as true (?).

    So, when I am viewing the page for CD1 (which is say, category 1), the main loop is automatically getting posts from only this category. But, some of the posts that have category 1 assigned to them, also might have category 5 (worksheets) or category 6 (flashcards) assigned as well.

    Should I structure my posts some other way? Perhaps not sharing so many categories per post?

    Thank you for all your help!

    -Mike

    #163275
    Mike Dorr
    Participant

    Hi,

    Thanks for all the help. I tried your snippet, and it was full of tons of content. I’m sure we could have kept digging with that and figured something out.

    I went another way, and was able to come up with something that works! Here’s what I did:

        <div class="wrapCat">
            <?php $cat_id = get_query_var('cat');
            $thequery = new WP_Query( array( 'category__and' => array( $cat_id, 5 )) );
            if($thequery->have_posts()) : ?>
                <h2 class="catTitle">Worksheets</h2>
                <?php while($thequery->have_posts()) : $thequery->the_post(); global $post; ?>
                    Show Worksheets
                <?php endwhile; ?>
            <?php endif; ?>
        </div>
    

    Thanks again for all your help!

    -Mike

Viewing 5 posts - 1 through 5 (of 5 total)
  • The forum ‘Back End’ is closed to new topics and replies.