Can't work out where (hook) this piece of code goes (outside the loop)?

Community Forums Forums Design Tips and Tricks Can't work out where (hook) this piece of code goes (outside the loop)?

This topic is: not resolved

Tagged: , ,

This topic contains 6 replies, has 2 voices, and was last updated by  David Chu 10 months, 1 week ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #62533

    pjeaje
    Participant
    Post count: 2

    I have this piece of code that has worked very well in every other theme I’ve tried, except genesis. The code goes on single.php and displays/lists all the OTHER posts in the same category as the current single post. It is placed OUTSIDE the loop.

    Whenever I try to hook (with the simple hooks plugin) it where i think it should go it strangely lists the current post as as well as the other posts in the category of the current post… very weird as it works like a dream in every other non-genesis theme I’ve tried.

    Any help greatly appreciated

    <?php
    if ( is_single()) {
      $categories = get_the_category();
      if ($categories) {
        foreach ($categories as $category) {
          // echo "<pre>"; print_r($category); echo "</pre>";
          $cat = $category->cat_ID;
          $args=array(
            'cat' => $cat,	
    		'order' =>DESC,
    		'orderby' => rand,
            'post__not_in' => array($post->ID),
            'posts_per_page'=>55,
            'caller_get_posts'=>1
          );
          $my_query = null;
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
            echo '<h3 class="otherh2">Other students who have completed this activity (or parts of it) - '. $category->cat_nameXXX . ' </h3>';	
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              		<div class="other">
    				<p class="otherh3" style="clear:both;"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a><br /><?php the_time('Y'); ?></p>
    				<?php if ( function_exists( 'get_the_image' ) ) get_the_image(array('the_post_thumbnail' => true, 'order_of_image' => 1, 'size' => 'thumbnail', 'image_class' => 'thumb4', 'link_to_post' => true, 'default_image' => 'http://www.perthmetro.net/asdan/wp-content/uploads/no_photo.jpg' )); ?>
    				</div>
    			
             <?php
            endwhile;
          } //if ($my_query)
        } //foreach ($categories
      } //if ($categories)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
    #62535

    David Chu
    Participant
    Post count: 1314

    If I follow you, you’re sort of trying to show everything in a category minus “the current Post”?
    My gut reaction: I wonder if you might just need to run this before running your code. It pulls out the regular Genesis loop.

    [php]
    remove_action( ‘genesis_loop’, ‘genesis_do_loop’ );
    [/php]

    Dave


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

    #62615

    pjeaje
    Participant
    Post count: 2

    Thanks Dave you’re right in knowing what the code does… Can you elaborate exactly where I should run the above code?

    #62632

    David Chu
    Participant
    Post count: 1314

    Generally hook procedures are run in your child theme’s functions.php, and that’s where I tend to stick everything, or in a separate functions file in my child theme.

    But you don’t want to run that command “bare” in there, because then none of your pages/posts will have content, and that’s maybe a little too minimalist. ;)

    So for instance, in functions.php you could wrap it up like so:
    [php]
    add_action( ‘genesis_before_content’, ‘your_example_function’);
    function your_example_function() {
    if ( is_single() ) {
    remove_action( ‘genesis_loop’, ‘genesis_do_loop’ );
    }
    }
    [/php]

    I’m sure you’ll grasp what that’s doing. And if you want Genesis functions to work with your template, you’ll need this command before the last line of your template:
    [php]
    genesis();
    [/php]

    But you already knew that, because you read up on how Genesis works, right? ;)

    btw, if you’re using that code in a template called single.php, you won’t need to check is_single at all, I’m pretty sure, it’s already weeding everything else out. :-)

    If you’re going to be hanging with Genesis often, you’ll want to get to know these hooks:

    http://my.studiopress.com/docs/hook-reference/

    Wordpress coders who come to Genesis late in the game have a familiar progression – they want to code tons of template files just like Twenty Thirteen or what-have-you. That works, as it’s still WordPress, and so they think “what’s so great about Genesis?” Which is a reasonable question. But then they start getting comfy with its hooks, and gradually realize, OMG, using Genesis hooks and filters I can code child themes with just 2 or 3 files! And then they get excited. :-)

    The only downside to that (that I can think of) is that if they decide later that they don’t want Genesis anymore, they’ll have more code to re-write to adapt their theme.

    Dave


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

    #62650

    David Chu
    Participant
    Post count: 1314

    Just for giggles, I tested again using one of my page templates… you could actually do the one-liner remove command right in your template instead of sticking all that code into functions.php.

    Again, you’d just want to have the genesis command at the very bottom.

    As is usually the case, there are many solutions to this problem. And if you stick with Genesis, I predict that you’ll embrace putting hook and filter commands in your functions.php. ;)

    Dave


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

    #62819

    pjeaje
    Participant
    Post count: 2

    Thanks heaps for that David. I’ve got a story for you…

    Once upon a time this guy had a bike. The bike had 12 speeds a few reflectors, a seat, pedals and brakes. He rode his bike 7km to work and back each day. If the tire got a flat he fixed it, if the chain needed replacing he fixed it, if the brakes needed adjusting, he fixed it. He loved his bike! Everyone knew him as the ‘bike guy’. This guy’s mate has a 21 speed bike with light for riding at night, disc brakes, a gel seat and carbon fibre wheels. The guy’s mate said “You need to come aboard and get this bike… it’ll make you ride faster, longer and at night… these bikes are the future!”.

    “But I don’t want to ride faster or longer or at night even… I want to be able to get to work and back by myself without having to rely on others to help me fix my bike.”

    The guy got on his bike and kept on riding to work… every day for the rest of his working life. The guy’s mate got on his bike and rode to work, until a newer, faster model came out… then he rode that new bike to work.

    Thanks again for your help David but WordPress to me is about template tags inside templates, ones that I can fix directly. Let me get bored with these first and I might look at buying that new bike :)

    Regards,
    Pete

    #62857

    David Chu
    Participant
    Post count: 1314

    Pete,
    I hear you. Not everyone likes the kind of approach that I do. Although Genesis runs quite briskly, any framework will take some overhead. If you like raw code templates, you may get a pinch of extra speed, and this could be augmented by one of those more stripped-down themes. I wonder if you might like something like underscores, some talented people are involved with that one.

    Dave


    Dave Chu · Custom WordPress Developer – likes collaborating with Designers

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.