• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

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

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived 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: hook, loop, outside

  • This topic has 6 replies, 2 voices, and was last updated 12 years, 7 months ago by David Chu.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • September 14, 2013 at 10:24 am #62533
    pjeaje
    Member

    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())
    ?>
    September 14, 2013 at 10:46 am #62535
    David Chu
    Participant

    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.

    remove_action( 'genesis_loop', 'genesis_do_loop' );
    

    Dave


    Dave Chu ยท Custom WordPress Developer – likes collaborating with Designers

    September 15, 2013 at 5:05 am #62615
    pjeaje
    Member

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

    September 15, 2013 at 9:23 am #62632
    David Chu
    Participant

    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:

    add_action( 'genesis_before_content', 'your_example_function');
    function your_example_function() {
       if ( is_single() ) {
          remove_action( 'genesis_loop', 'genesis_do_loop' );
       }
    }
    

    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:

    genesis();
    

    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

    September 15, 2013 at 12:28 pm #62650
    David Chu
    Participant

    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

    September 16, 2013 at 8:24 am #62819
    pjeaje
    Member

    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

    September 16, 2013 at 12:34 pm #62857
    David Chu
    Participant

    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

  • Author
    Posts
Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Design Tips and Tricks’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2026 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble