ThemeShaper Forums » Thematic

[closed]

a loop that runs only on one category archive

(14 posts)
  • Started 1 year ago by colfelt
  • Latest reply from colfelt
  • This topic is resolved
  1. colfelt
    Member

    Hi all,

    I am trying to call a function/loop specific to one category archive page. What I've done, is to remove the standard loop within my child-theme's functions.php, then replaced it with my 'special' loop.

    So far, I have cracked getting that 'special' loop to work nicely. But what I want to do now, is to only run the function which rips out the normal loop when we are on the category archive of a particular category ('menus').

    Here's my code:

    // remove the standard loop
    function remove_thematic_categoryloop() {
    	remove_action('thematic_categoryloop', 'thematic_category_loop');
    }
    add_action('init', 'remove_thematic_categoryloop');
    
    // get the name of the category and store it in the variable $catslug
    function add_handmade_intro_categoryloop() {
    if (is_category( )) {
      $cat = get_query_var('cat');
      $yourcat = get_category ($cat);
      $catslug = $yourcat->slug;
    
    // loop for the content within the current category,
    // which has a custom field 'intro_article' which is equal to 1
    $handmade_query = new WP_Query('category_name='.$catslug.'&meta_key=intro_article&meta_value=1');
    while ($handmade_query->have_posts()) : $handmade_query->the_post();  ?>
    
    <h1 class="entry-title"><?php the_title(); ?></h1>
    <div class="entry-content"><?php the_content(); ?></div>
    
    <?php endwhile;
    wp_reset_postdata(); ?>
    
    <?php
    // now loop to create an unordered list of the other posts in the category
    // but which have a custom field 'intro_article' which is equal to 0
    ?>
    <ul id="sub-nav-ul">
    <?php
     query_posts('category_name='.$catslug.'&meta_key=intro_article&meta_value=0');
     if (have_posts()) : while (have_posts()) : the_post();  ?>
    
     <li id="sub-nav" class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php the_post_thumbnail(array(70,70));?><span><?php the_title(); ?></span></a></li>
    
    <?php endwhile; endif; ?></ul> <?php
    }
    }
    add_action('thematic_categoryloop', 'add_handmade_intro_categoryloop');

    So, I've tried to put in some "if (is_category('menus'))" stuff in before the function, which didn't work... and I have subsequently hit a wall and need some inspiration. I only want to call these functions if the category is 'menus'...

    Can anybody help me get over this hump? it would save me some hair. Thanks!

    Posted 1 year ago #
  2. colfelt
    Member

    OK... I think I have it. Just in case someone else may find this helpful I'm pasting the code below.

    // remove the standard loop
    
    function remove_thematic_categoryloop() {
    	remove_action('thematic_categoryloop', 'thematic_category_loop');
    }
    add_action('init', 'remove_thematic_categoryloop');
    
    // get the name of the category and store it in the variable $catslug
    function add_handmade_intro_categoryloop() {
    if (is_category( )) {
      $cat = get_query_var('cat');
      $yourcat = get_category ($cat);
      $catslug = $yourcat->slug;
    
    // if this isn't in the menus category, do a normal loop
    if (!is_category('menus')){
     query_posts('category_name='.$catslug);
     if (have_posts()) : while (have_posts()) : the_post();  ?>
    
     <h1 class="entry-title"><?php the_title(); ?></h1>
    <div class="entry-content"><?php the_content(); ?></div>
    
    <?php endwhile; endif;
    wp_reset_postdata();
    }
    
    // if it is in the category 'menus' then do these special loops
    if (is_category('menus')) {
    
    // loop for the content within the current category,
    // which has a custom field 'intro_article' which is equal to 1
    $handmade_query = new WP_Query('category_name='.$catslug.'&meta_key=intro_article&meta_value=1');
    while ($handmade_query->have_posts()) : $handmade_query->the_post();  ?>
    
    <h1 class="entry-title"><?php the_title(); ?></h1>
    <div class="entry-content"><?php the_content(); ?></div>
    
    <?php endwhile;
    wp_reset_postdata(); ?>
    
    <?php
    // now loop to create an unordered list of the other posts in the category
    // but which have a custom field 'intro_article' which is equal to 0
    ?>
    <ul id="sub-nav-ul">
    <?php
     query_posts('category_name='.$catslug.'&meta_key=intro_article&meta_value=0');
     if (have_posts()) : while (have_posts()) : the_post();  ?>
    
     <li id="sub-nav" class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php the_post_thumbnail(array(70,70));?><span><?php the_title(); ?></span></a></li>
    
    <?php endwhile; endif; ?></ul> <?php
    }
    }
    }
    add_action('thematic_categoryloop', 'add_handmade_intro_categoryloop');
    Posted 1 year ago #
  3. as a general set up what about:

    function childtheme_override_category_loop(){
      if(is_category('bacon')){
           //put your special bacon loop here
      } else {
          thematic_category_loop();
      }
    }
    Posted 1 year ago #
  4. colfelt
    Member

    Well, clearly that's much more elegant. And anything that has bacon in it has to be good, right?

    So, the function childtheme_override_whateverfunction. That can be used to? Let me guess, override any function you can find that says thematic_whateverfunction?

    Posted 1 year ago #
  5. you got it. everything is better with bacon. yes, look through the extensions folder. you'll see plenty of places where it is set up to run childtheme_override_whatever in place of thematic_whatever. the overrides are far more intuitive than filters, but not always the best solution. usually depends on how much you need to change... or if there is a filter available.

    Posted 1 year ago #
  6. you got it. everything is better with bacon. yes, look through the extensions folder. you'll see plenty of places where it is set up to run childtheme_override_whatever in place of thematic_whatever. the overrides are far more intuitive than filters, but not always the best solution. usually depends on how much you need to change... or if there is a filter available.

    Posted 1 year ago #
  7. colfelt
    Member

    Cool beans. Thanks Helga!

    Posted 1 year ago #
  8. colfelt
    Member

    OK... so I've been working with Helga's cleaner code. But, It's not playing nicely with me. I find that the two loops I have defined in it work fine. But then the normal thematic_category_loop doesn't seem to work. The categories that should run that loop don't seem to run any loop at all. So, it made me wonder, doesn't specifying childtheme_override_category_loop tell the original thematic_category_loop to go use childtheme_override_category_loop instead? Therefore, flying up it's own backside? Maybe it doesn't work that way. But it was the best explanation I could come up with to explain why the code below didn't work. Can you spot anything that looks like it would prevent the thematic_category_loop from working?

    // remove the standard loop
    
    function childtheme_override_category_loop(){
    
    // get the name of the category and store it in the variable $catslug
      $cat = get_query_var('cat');
      $yourcat = get_category ($cat);
      $catslug = $yourcat->slug;
    
    if (is_category('testimonials')){
     query_posts('category_name='.$catslug);
     if (have_posts()) : while (have_posts()) : the_post();  ?>
    
    <h1 class="entry-title"><?php the_title(); ?></h1>
    <div class="entry-content"><?php the_content(); ?></div>
    
    <?php endwhile; endif;
    }
    elseif (is_category('menus')) {
    // loop for the content within the current category,
    // which has a custom field 'intro_article' which is equal to 1
    $handmade_query = new WP_Query('category_name='.$catslug.'&meta_key=intro_article&meta_value=1');
    while ($handmade_query->have_posts()) : $handmade_query->the_post();  ?>
    
    <h1 class="entry-title"><?php the_title(); ?></h1>
    <div class="entry-content"><?php the_content(); ?></div>
    
    <?php endwhile;
    wp_reset_postdata(); ?>
    
    <?php
    // now loop to create an unordered list of the other posts in the category
    // but which have a custom field 'intro_article' which is equal to 0
    ?>
    <ul id="sub-nav-ul">
    <?php
     query_posts('category_name='.$catslug.'&meta_key=intro_article&meta_value=0');
     if (have_posts()) : while (have_posts()) : the_post();  ?>
    
     <li id="sub-nav" class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> <?php the_post_thumbnail(array(70,70));?><span><?php the_title(); ?></span></a></li>
    
    <?php endwhile; endif; ?></ul> <?php
    wp_reset_postdata();
    
    } else {
          thematic_category_loop();
      }
    }
    Posted 1 year ago #
  9. can't see anything in your code, but first off make sure you are using this version of thematic:
    developing.thematic4you.com/thematic-development-release/

    as there were some problems w/ the add_action that got resolved.

    Posted 1 year ago #
  10. colfelt
    Member

    Yeah I'm using 0.9.8, but no joy. Ah well, the less streamlined version is working OK. It just bugs me that something that *should* work, doesn't. It's just not bacon.

    Posted 1 year ago #
  11. not bacon! :) well i have to apologize b/c i think i messed up.

    basically thematic does this:

    if (function_exists('childtheme_override_category_loop'))  {
    	function thematic_category_loop() {
    		childtheme_override_category_loop();
    	}
    }

    so if the override is defined then thematic's default becomes the override. which means the original thematic_category_loop doesn't exist anymore and you just end up in a loop of loops.

    my else statement

    else { thematic_category_loop() }

    is just pointing to childtheme_override_category_loop which points to thematic_category_loop and on and on.

    so the override should be

    function childtheme_override_category_loop(){
      if(is_category('bacon')){
           //put your special bacon loop here
      } else {
          //put default category loop here
      }
    }
    Posted 1 year ago #
  12. colfelt
    Member

    Ah, yes. That's better. I figured there must've been a walking donut there. That loop had completely disappeared up it's own rear. And it didn't have ANY bacon in it, which is, well, unsatisfactory.

    As ever, Helga, thank you. You're awesome. :)

    Posted 1 year ago #
  13. no bacon is very unsatisfactory. always more than 1 way to skin the cat b/c we could probably go back to what you started with

    function remove_thematic_categoryloop() {
    	remove_action('thematic_categoryloop', 'thematic_category_loop');
    }
    add_action('init', 'remove_thematic_categoryloop');
    
    function kia_categoy_loop(){
       if(is_category('bacon')){
           //bacon loop here
       } else {
           thematic_category_loop();
       }
    add_action('thematic_categoryloop','kia_category_loop');
    Posted 1 year ago #
  14. colfelt
    Member

    Ahh, that old chestnut...

    Posted 1 year ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.