Forums

show category post, and then all sub-category posts in category template (13 posts)

  1. ingero
    Member
    Posted 2 years ago #

    I need help with something I'm trying to do in WordPress. I've found some other posts here on the forums that I thought would do what I wanted, but come to find that either I had no idea how to implement them or it wasn't doing quite what I wanted. Any help to do this would be much appreciated.

    I am creating a website that functions similarly to onthemedia.org - essentially, an online public radio program. Each program has many shows released on a periodic basis. The initial post about the show will be the entire program, and then underneath it will be excerpts from the show.

    My category set up is as follows:

    Program Name
    > Show Date
    >> Show Date Excerpts
    > Show Date
    >> Show Date Excerpts

    So, I'm thinking that I need the category.php template to have the following:

    { Show Date loop // parent }
    { Show Date Excerpts loop // child }

    I am pretty sure I could hard code the Show Date template to do all of this properly (category-4.php, category-6.php, etc), but I need something where I can just use 1 template and it automatically finds the ID's. Also note that each loop will have it's own design/css.

    Maybe this is what I need?
    http://wordpress.org/support/topic/252257?replies=4
    http://codex.wordpress.org/Function_Reference/get_the_category

    If so, how do I implement it?

  2. dor
    Member
    Posted 2 years ago #

    Surely, you can obtain requested category id from within the [single] template, and get all sub-categories.

    You will need to nest Loops, that should not be a problem...

    Probably you will need to preserve original query, limiting categories at the same time (also can be found in the docs).

    Sorry, i am not sure that i understand what is the problem. Sorry if i don't.

    Oh well... I'll be back tomorrow.

  3. ingero
    Member
    Posted 2 years ago #

    dor,

    Thanks for your response. I'm not really sure if I am following you. I tried nesting the two loops, but I'm still at a loss at how to let the loop know which category to pull from without specifying actual ID's of categories. If I left it as it is, it will continue to display the same posts forever and never end.

    <!--loop1-->
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
            <div class="program" id="post-<?php the_ID(); ?>"><!--program-->
                	<div class="program-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
                    <div class="program-date">July 27, 2009</div>
                    <br class="clear" />
                    <div class="metadata"><!--metadata-->
                    	<ul>
                        <li><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></li>
                        <li><a href="#">Share</a></li>
                        <li><a href="audio/Doc-Athens.mp3">Download</a></li>
                        </ul>
                    </div><!--program-tools-->
                    <div class="entry"><!--entry-->
                      <?php the_content('Read the rest of this entry &raquo;'); ?>
                    </div><!--entry-->
                </div><!--program-->
                <!--loop1 break-->
    
                <!--loop2-->
    			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
                         <div class="post" id="post-<?php the_ID(); ?>"><!--post-->
                            <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                            <div class="entry"><!--entry-->
    
                            <?php the_content('Read the rest of this entry &raquo;'); ?>
    
                            </div><!--entry-->
    
                            <div class="metadata"><!--metadata-->
                                <ul>
                                    <li><a href="#"><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></a></li>
                                    <li><a href="#">Share</a></li>
                                    <li><a href="audio/F1-Athens.mp3">Download</a></li>
                                </ul>
                            </div><!--metadata-->
                        </div><!--post-->     
    
                    <?php endwhile; ?>
    
                    <div class="navigation">
                        <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
                        <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
                    </div>
    
                <?php else : ?>
    
                    <h2 class="center">Not Found</h2>
                    <p class="center">Sorry, but you are looking for something that isn't here.</p>
                    <?php get_search_form(); ?>
    
                <?php endif; ?>
        		<!--loop2-->
    
            <!--rest of loop1-->
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>
    	<!--loop1-->
  4. ingero
    Member
    Posted 2 years ago #

    The following code does exactly what I want it to, however, I need it to automatically find the categories to include/exclude, so I'm thinking maybe I need to write a function that contains that logic, however I am not really sure where to start with that.

    For those of you who would like to see what this produces visually, please let me know your email and I'll send you the dev site. I'd post the link here, but it's a project that the client would like kept password protected until it's launched.

    Here's the code:

    <!--loop1-->
     <? query_posts('cat=5,-6'); ?>
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
            <div class="program" id="post-<?php the_ID(); ?>"><!--program-->
                	<div class="program-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
                    <div class="program-date">July 27, 2009</div>
                    <br class="clear" />
                    <div class="metadata"><!--metadata-->
                    	<ul>
                        <li><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></li>
                        <li><a href="#">Share</a></li>
                        <li><a href="audio/Doc-Athens.mp3">Download</a></li>
                        </ul>
                    </div><!--program-tools-->
                    <div class="entry"><!--entry-->
                      <?php the_content('Read the rest of this entry &raquo;'); ?>
                    </div><!--entry-->
                </div><!--program-->
    		<?php endwhile; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>
    	<!--loop1-->
    
     			<!--loop2-->
                <? query_posts('cat=6&order=ASC'); ?>
    			<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
                         <div class="post" id="post-<?php the_ID(); ?>"><!--post-->
                            <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
                            <div class="entry"><!--entry-->
    
                            <?php the_content('Read the rest of this entry &raquo;'); ?>
    
                            </div><!--entry-->
    
                            <div class="metadata"><!--metadata-->
                                <ul>
                                    <li><a href="#"><?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></a></li>
                                    <li><a href="#">Share</a></li>
                                    <li><a href="audio/F1-Athens.mp3">Download</a></li>
                                </ul>
                            </div><!--metadata-->
                        </div><!--post-->     
    
                    <?php endwhile; ?>
    
                    <div class="navigation">
                        <div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
                        <div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
                    </div>
    
                <?php else : ?>
    
                    <h2 class="center">Not Found</h2>
                    <p class="center">Sorry, but you are looking for something that isn't here.</p>
                    <?php get_search_form(); ?>
    
                <?php endif; ?>
        		<!--loop2-->
  5. ingero
    Member
    Posted 2 years ago #

    Found some more links, not quite sure what to do with it or if it'll work but I'm going to play with them...

    http://wordpress.org/support/topic/234874
    http://txfx.net/code/wordpress/force-category-template/

  6. dor
    Member
    Posted 2 years ago #

    Sorry, i wanted to test a bit, but really have no time now.

    But here is a piece of one of my templates, may be this helps.

    <?php foreach ( $cat_list as $cat => $subcats ) { ?>
      <?php query_posts(array('category__and'=>array($cat,$cat_requested))); ?>
      <?php if (have_posts()): ?>
        <div class="arch-cat">
          <div class="arch-cat-name"><?php echo get_cat_name($cat); ?></div>
            <?php foreach ( $subcats as $singlecat ) { ?>
              <?php
                # query posts both in "$cat_requested" and "$singlecat" categories:
                query_posts(array('category__and'=>array($singlecat,$cat_requested),'orderby' => 'title','order' => 'ASC',)); ?>
                <?php if (have_posts()): ?>
                  <div class="arch-subcat-name"><?php echo get_cat_name($singlecat); ?></div>
                  <?php while (have_posts()) : the_post(); ?>
                    <div class="post">
    // ....

    This is not exactly what you mean, sorrrry.

  7. ingero
    Member
    Posted 2 years ago #

    Thanks dor, I'll see if I can use this!

  8. ingero
    Member
    Posted 2 years ago #

    Dor,

    I'm having a little trouble with your code. I am not quite sure how to end it? If you could post the ending, that would be helpful. I've tried a few things and I just keep getting errors.

  9. ingero
    Member
    Posted 2 years ago #

    I'm pretty sure that I need to write a function, but I don't have the faintest idea of how to write it.

    I need to be able to grab the ID of the current category, and then the ID of the current category's child, and then display those variables in the query_posts line.

    The first loop needs to include parent, and exclude the child.

    <? query_posts('cat=*parent*,-*child*'); ?>

    The second loop needs to just include the child.

    <? query_posts('cat=*child*&order=ASC'); ?>

    It seems simple to me, but I just don't know php at all. I'm sure this would be very useful to others trying to achieve the same thing.

    I'm also trying to think of how the front page will work. I just want to be able to display 1 category page ( parent post and then all child posts ), and automatically update when a new one is posted.

  10. dor
    Member
    Posted 2 years ago #

    > I need to be able to grab the ID of the current category
    Use

    $cat_requested_title = single_cat_title('', false);
    $cat_requested = get_cat_ID($cat_requested_title);

    > and then the ID of the current category's child
    Mmm... Do you have any problems with this?

    Btw, for archives you may need smth like this:

    global $wp_query;
    query_posts(
      array_merge(
        array(
          'cat'=> <Your cat and other restrictions here>),
          $wp_query->query
      )
    );

    So... It seems you are planning you code right... And i'm not sure that i know what it is the main trouble for you (besides you php knowledge)... And i am sorry that i can not try to code you task these days, your should try it yourself.

    Try with something simple and ask :-(

  11. ingero
    Member
    Posted 2 years ago #

    I just wanted to say that I appreciate your help in this matter so far and whatever I end up using to solve this I will post up here so others can benefit from my learning experience, but you should probably assume that I know absolutely nothing. I'm a designer that knows html/css/some javascript... php is foreign to me for the most part.

    For example, I am not really sure how to use the code "$cat_requested_title = single_cat_title('', false);"

    Here's what I have for the first loop, and although it is not producing any errors, it's not quite doing what I want anyway.

    `<?
    global $wp_query;
    query_posts(
    array_merge(
    array(
    'cat'=> '$cat_requested = get_cat_ID($cat_requested_title);' ),
    $wp_query->query
    )
    );
    ?>`

    If I use this on the index.php template which I have been using for testing, it simply shows all posts no matter what category they are in.

    But actually, it seems to work on category.php.

    I'll send you a message with the URL and password so you can see.

    Now I just need to figure out how to show the children without manually inputting the child category for the second loop.

    -Rakel

  12. ingero
    Member
    Posted 2 years ago #

    Oh, hmm. I guess this forum doesn't have messaging. Can you send me an email? rakel@kmdg.com, or let me know what your email is and I'll send it over!

  13. ingero
    Member
    Posted 2 years ago #

    Also, I found this code to use for the first loop that display the parent category (so there is parent > child > another child):

    <?php
    					   foreach((get_the_category()) as $childcat) {
                            if (cat_is_ancestor_of(4, $childcat)) {
                            echo $childcat->cat_name;
                            }}
                        ?>

    But as you can see I have to input the parent category there as well. Is there a way I can use the code you provided to display the category name in that fashion?

    In case this is hard to understand, here's an outline:

    Program Name (parent)
    > Show Date (child)
    > > Show Date - Excerpts (sub-child)

Topic Closed

This topic has been closed to new replies.

About this Topic