• 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

Secondary Genesis Loop for Related Posts in single.php

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 › Secondary Genesis Loop for Related Posts in single.php

This topic is: not resolved

Tagged: related posts

  • This topic has 4 replies, 2 voices, and was last updated 9 years, 2 months ago by preahkumpii.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • May 8, 2016 at 11:49 am #185211
    preahkumpii
    Member

    I 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.com
    May 8, 2016 at 5:47 pm #185228
    Brad Dalton
    Participant

    Try this https://wordpress.org/plugins/same-category-posts/


    Tutorials for StudioPress Themes.

    May 8, 2016 at 6:19 pm #185236
    preahkumpii
    Member

    Thanks. 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 #185249
    Brad Dalton
    Participant

    I spent 3 hours working on this for my members.

    If you have any questions, please use the contact form on my site.


    Tutorials for StudioPress Themes.

    May 10, 2016 at 9:00 am #185358
    preahkumpii
    Member

    I 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 &rarr;</div></a>";
                echo "</li>";
                $count++;
                if ( $count == 6 ) {  //this is the number of posts to show
                    break;
                }
            }
            echo "</ul>";
            echo "</div>";
        }
    }
  • Author
    Posts
Viewing 5 posts - 1 through 5 (of 5 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

© 2025 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