ThemeShaper Forums » Thematic

Can I replace the loop with a filter?

(21 posts)
  • Started 9 years ago by travismallen
  • Latest reply from Seijun
  • This topic is resolved
  1. Hey all,

    Is the right way (in terms of child themes) to replace/change the loop with a filter, or am I supposed to copy the index and modify?

    I haven't seen an action for it so I wasn't sure, but want to do it right.

    Thanks,

    Travis

    Posted 9 years ago #
  2. I'm going to go with copying the index, but is anyone aware of a way to do it with a function?

    Posted 9 years ago #
  3. Hi,

    you can copy the index.php to your child theme's directory or you can:

    function remove_index_loop() {
        remove_action('thematic_indexloop', 'thematic_index_loop');
    }
    add_action('init', 'remove_index_loop');
    
    function my_index_loop() {
        <!-- Your code -->
    }
    add_action('thematic_indexloop', 'my_index_loop');

    in your child theme's functions.php

    Chris

    Posted 9 years ago #
  4. Thanks a lot, Chris! I'll do it that way now.

    Posted 9 years ago #
  5. Would this also apply to <?php thematic_categoryloop() ?> ?

    I am trying to add the call to the get_the_image plugin to the category loop.

    Posted 9 years ago #
  6. Yes .. Naming scheme should be the same

    Chris

    Posted 9 years ago #
  7. It's not working for me, but I read elsewhere that I might have to be working with the latest Thematic version via SVN. I am working with 0.9.5.1 currently. This is the function I'm using:

    function remove_categoryloop() {
        remove_action('thematic_categoryloop', 'thematic_categoryloop');
    }
    add_action('init', 'remove_categoryloop');
    
    function my_categoryloop() {
        echo 'Hello World';
    }
    add_action('thematic_categoryloop', 'my_categoryloop');

    When viewing a category archive page I am expecting to see only "Hello World" and no posts listed, but I am seeing "Hello World" at the top of my posts.

    As a test, I replaced the index loop using the function above, and I am only seeing "Hello World" as expected on the index page.

    Posted 9 years ago #
  8. ...sorry, got it now. I had a typo 'thematic_categoryloop' should be 'thematic_category_loop'

    Now to figure out how to put a new category loop in there that includes the call to <?php get_the_image(); ?> for each post in the category listing:)

    Posted 9 years ago #
  9. amygail
    Member

    This worked for me

    function remove_categoryloop() {
        remove_action('thematic_categoryloop', 'thematic_categoryloop');
    }
    add_action('init', 'remove_categoryloop');
    
    function my_categoryloop() { ?>
        <?php get_the_image(); ?>
    <?php
    }
    add_action('thematic_categoryloop', 'my_categoryloop');
    Posted 9 years ago #
  10. amygail
    Member

    Actually - that's working for single posts (in the category archives) but only giving me the image from the first post on a category page ...

    Posted 9 years ago #
  11. Hi amygail,

    I think the trick is going to be to put the actual category loop in this function and add the <?php get_the_image(); ?> just before the post content (or wherever). I'm going to give it a whirl, but probably not until tomorrow.

    Posted 9 years ago #
  12. I wouldn't have this without the help of a friend:)

    function remove_categoryloop() {
        remove_action('thematic_categoryloop', 'thematic_category_loop');
    }
    add_action('init', 'remove_categoryloop');
    
    function my_categoryloop() {
    while (have_posts()) : the_post(); ?>
    
              <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
                  <?php thematic_postheader(); ?>
                  <div class="entry-content">
    <?php get_the_image(); ?>
    <?php thematic_content(); ?>
    				</div>
                  <?php thematic_postfooter(); ?>
              </div><!-- .post -->
    
          <?php endwhile;
    }
    add_action('thematic_categoryloop', 'my_categoryloop');
    Posted 9 years ago #
  13. amygail
    Member

    Right on. That's working for me.
    Thanks for sharing!

    Posted 9 years ago #
  14. Jamie Mitchell
    Member

    Howdy all.. :)

    This is such a better way of filtering the category page instead of copying it to the child theme.

    But how can we have it so it will only filter for certain category/s ?

    Say for a portfolio page that one may want different.

    J

    Posted 9 years ago #
  15. Hi Jamie,

    took the above mentioned code:

    function remove_categoryloop() {
        remove_action('thematic_categoryloop', 'thematic_category_loop');
    }
    add_action('init', 'remove_categoryloop');
    
    function my_categoryloop() {
    while (have_posts()) : the_post();
    
            if
            <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
                  <?php thematic_postheader(); ?>
                  <div class="entry-content">
    <?php get_the_image(); ?>
    <?php thematic_content(); ?>
    				</div>
                  <?php thematic_postfooter(); ?>
              </div><!-- .post -->
    
          <?php endwhile;
    }
    add_action('thematic_categoryloop', 'my_categoryloop');

    Chris

    Posted 9 years ago #
  16. Jamie Mitchell
    Member

    hey Chris, how ya going :)

    i added

    <?php if ( is_category('portfolio') ) query_posts(); ?>

    where you have the if, but then how do i bring it back to the normal category page

    say i want to have the portfolio category page have no postheader or postfooter, as it's only used for images, but every other category will display as normal?

    J

    Posted 9 years ago #
  17. Hi Jamie,

    you don't need to change the loop. Create two filters:

    function my_postheader($content) {
        if (is_category('portfolio') {
            $content = '';
        }
        return $content;
    }
    add_filter('thematic_postheader', 'my_postheader');
    
    function my_postfooter($content) {
        if (is_category('portfolio') {
            $content = '';
        }
        return $content;
    }
    add_filter('thematic_postfooter', 'my_postfooter');

    Chris

    Posted 9 years ago #
  18. Jamie Mitchell
    Member

    Your brain is bigger than mine...

    I still can't get my head around php

    thanks as always Chris...

    :)

    Posted 9 years ago #
  19. function remove_categoryloop() {
        remove_action('thematic_categoryloop', 'thematic_category_loop');
    }
    add_action('init', 'remove_categoryloop');
    
    function my_categoryloop() {
    while (have_posts()) : the_post();
    
            if
            <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">
                  <?php thematic_postheader(); ?>
                  <div class="entry-content">
    <?php get_the_image(); ?>
    <?php thematic_content(); ?>
    				</div>
                  <?php thematic_postfooter(); ?>
              </div><!-- .post -->
    
          <?php endwhile;
    }
    add_action('thematic_categoryloop', 'my_categoryloop');

    gives me a syntax error. The other codes in this topic have no effect. Adding the php directly into the loop like the original instructions say ALSO has no effect. Really not what what I'm doing wrong here. I am running wp 2.9.2 with the latest version of thematic and get-the-image installed. Has something changed since this thread was created?

    EDIT: Found out why the other codes were not working. I needed to have get_the_image set to scan my post for images if none were found as attachments. <?php echo get_the_image( array( 'image_scan' => true ) ); ?>

    Posted 8 years ago #
  20. Sorry guys it looks like the code above in Chris's last post is missing the closing parentheses on the conditional tagged if statements. Happens to the best of us. Here is a version that should parse.

    function my_postheader($content) {
        if (is_category('portfolio')) {
            $content = '';
        }
        return $content;
    }
    add_filter('thematic_postheader', 'my_postheader');
    
    function my_postfooter($content) {
        if (is_category('portfolio')) {
            $content = '';
        }
        return $content;
    }
    add_filter('thematic_postfooter', 'my_postfooter');

    @Seijun in the code you posted above there is an issue with a stranded if here:

    while (have_posts()) : the_post();
    
            if
            <div id="post-<?php the_ID(); ?>" class="<?php thematic_post_class(); ?>">

    -Gene

    Posted 8 years ago #
  21. Gene:
    I dont know much of anything about php, I just copied the block of code from chris's post and it gave me the white screen of death. Do I just need to delete the word 'if'?

    Posted 8 years ago #

RSS feed for this topic

Reply

You must log in to post.