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 has 6 replies, 2 voices, and was last updated 11 years, 5 months ago by
David Chu.
-
AuthorPosts
-
September 14, 2013 at 10:24 am #62533
pjeaje
MemberI 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 #62535David Chu
ParticipantIf 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 #62615pjeaje
MemberThanks 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 #62632David Chu
ParticipantGenerally 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 #62650David Chu
ParticipantJust 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 #62819pjeaje
MemberThanks 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,
PeteSeptember 16, 2013 at 12:34 pm #62857David Chu
ParticipantPete,
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
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.