Community Forums › Forums › Archived Forums › Design Tips and Tricks › Secondary Genesis Loop for Related Posts in single.php
Tagged: related posts
- This topic has 4 replies, 2 voices, and was last updated 8 years, 10 months ago by
preahkumpii.
-
AuthorPosts
-
May 8, 2016 at 11:49 am #185211
preahkumpii
MemberI am trying to create a custom grid loop to display related content (related, that is, to the main post) in single.php, after the main content. I want this loop to display a specified number of random posts (each with title, thumbnail, excerpt, date, etc.) that have the same category as the main post. I have some experience in php code, but not enough to do this. Thanks for the help.
Adam
http://pintsizedtreasures.comMay 8, 2016 at 5:47 pm #185228Brad Dalton
ParticipantMay 8, 2016 at 6:19 pm #185236preahkumpii
MemberThanks. I was actually hoping to streamline this process by not using a plugin. We already have dozens of plugins and I don't really want to add to the bloat. So I'm hoping for a code snippet with the basics...
Adam
May 8, 2016 at 9:38 pm #185249Brad Dalton
ParticipantI spent 3 hours working on this for my members.
If you have any questions, please use the contact form on my site.
May 10, 2016 at 9:00 am #185358preahkumpii
MemberI figured it out. Maybe it will help someone else.
All of the code was put into the functions.php file. I did not have to touch the single.php file.
add_image_size( 'pst-related-posts-thumb', 250, 250, array( 'center', 'top' ) ); //for related posts add_action( "genesis_loop", "pst_related_posts" ); function pst_related_posts() { if ( is_single() ) { //make sure related posts only shows on single posts $my_cats = get_the_category(); $my_cat_id = $my_cats[0]->cat_ID; //gets the current post category id; must be done in loop $args = array( //sets arguments for get_posts() function, including get only posts with current post category id 'posts_per_page' => 12, //this number should be several more than the number of posts to show 'category' => $my_cat_id, 'orderby' => 'rand' ); $posts_array = get_posts( $args ); echo "<div class='pst-related-posts'>"; echo "<h3>We think you'll love this momspiration too!</h3>"; echo "<ul>"; $count = 0; foreach ($posts_array as $my_post) { if ( !has_post_thumbnail( $my_post ) ) { continue; } //cycle through each matched post and get needed info (title, url, thumbnail, etc.) $title = $my_post->post_title; $link = get_permalink($my_post); $thumb = get_the_post_thumbnail($my_post, "pst-related-posts-thumb"); echo "<li>"; echo "<h4><a href='" . $link . "' target='_self' title='" . $title . "'>" . $title . "</a></h4>"; echo "<a href='" . $link . "' target='_self' title='" . $title . "'>" . $thumb . "</a>"; echo "<a href='" . $link . "' target='_self' title='" . $title . "'><div class='pst-related-posts-button'>Read it now →</div></a>"; echo "</li>"; $count++; if ( $count == 6 ) { //this is the number of posts to show break; } } echo "</ul>"; echo "</div>"; } }
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.