• 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

Related Posts Code Problem

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 › Related Posts Code Problem

This topic is: not resolved

Tagged: magazine theme

  • This topic has 5 replies, 4 voices, and was last updated 12 years, 1 month ago by Rednasil.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • November 23, 2012 at 9:37 am #1349
    jaxy
    Member

    I've been to Nick's website (http://designsbynickthegeek.com/tutorials/related-posts-genesis) and tried following his tutorial to add related posts/images after the post to my Magazine theme. Iv'e run into problems though.

    I have added

     

    add_image_size( 'related', 90, 70, true );

     

    into the function.php file, and added CSS code to style.css. The problem I have is though with the long piece of code.

    I'm not 100% sure now if it should go to functions.php file or into the Simple Hooks plugin - genesis_after_post_content box.

    Just to make sure I have the right code here it is:

    <span>add_action( 'genesis_after_post_content', 'child_related_posts' );
    /**
    * Outputs related posts with thumbnail
    *
    * @author Nick the Geek
    * @url <a class="linkclass" href="http://designsbynickthegeek.com/tutorials/related-posts-genesis">http://designsbynickthegeek.com/tutorials/related-posts-genesis</a&gt;
    * @global object $post
    */
    function child_related_posts() {

    if ( is_single ( ) ) {

    global $post;

    $count = 0;
    $postIDs = array( $post->ID );
    $related = '';
    $tags = wp_get_post_tags( $post->ID );
    $cats = wp_get_post_categories( $post->ID );

    if ( $tags ) {

    foreach ( $tags as $tag ) {

    $tagID[] = $tag->term_id;

    }

    $args = array(
    'tag__in' => $tagID,
    'post__not_in' => $postIDs,
    'showposts' => 5,
    'ignore_sticky_posts' => 1,
    'tax_query' => array(
    array(
    'taxonomy' => 'post_format',
    'field' => 'slug',
    'terms' => array(
    'post-format-link',
    'post-format-status',
    'post-format-aside',
    'post-format-quote'
    ),
    'operator' => 'NOT IN'
    )
    )
    );

    $tag_query = new WP_Query( $args );

    if ( $tag_query->have_posts() ) {

    while ( $tag_query->have_posts() ) {

    $tag_query->the_post();

    $img = genesis_get_image() ? genesis_get_image( array( 'size' => 'related' ) ) : '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/related.png" alt="' . get_the_title() . '" />';

    $related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . $img . get_the_title() . '</a></li>';

    $postIDs[] = $post->ID;

    $count++;
    }
    }
    }

    if ( $count <= 4 ) {

    $catIDs = array( );

    foreach ( $cats as $cat ) {

    if ( 3 == $cat )
    continue;
    $catIDs[] = $cat;

    }

    $showposts = 5 - $count;

    $args = array(
    'category__in' => $catIDs,
    'post__not_in' => $postIDs,
    'showposts' => $showposts,
    'ignore_sticky_posts' => 1,
    'orderby' => 'rand',
    'tax_query' => array(
    array(
    'taxonomy' => 'post_format',
    'field' => 'slug',
    'terms' => array(
    'post-format-link',
    'post-format-status',
    'post-format-aside',
    'post-format-quote' ),
    'operator' => 'NOT IN'
    )
    )
    );

    $cat_query = new WP_Query( $args );

    if ( $cat_query->have_posts() ) {

    while ( $cat_query->have_posts() ) {

    $cat_query->the_post();

    $img = genesis_get_image() ? genesis_get_image( array( 'size' => 'related' ) ) : '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/related.png" alt="' . get_the_title() . '" />';

    $related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . $img . get_the_title() . '</a></li>';
    }
    }
    }

    if ( $related ) {

    printf( '<div class="related-posts"><h3 class="related-title">Related Posts</h3><ul class="related-list">%s</ul></div>', $related );

    }

    wp_reset_query();

    }
    }
    </span>

     

    When I add it to the functions.php file the whole theme crashes and I get an error that there's a syntax error on line where the code starts:

     

    add_action( 'genesis_after_post_content', 'child_related_posts' );

     

    The only way to get into the theme is to go through FTP and remove the whole code.

    Secondly, I tried adding the long code into Simple Edits box. What happens then is that when I open the post all of the code is after the post. So it's the code and not the images with related posts.

    I have removed the long code at the moment. So if you could check or know what the problem is advice would be very much appreciated.

    Thanks a lot.

    November 23, 2012 at 9:54 am #1352
    sahdow
    Participant

    Go back to nicks site and copy the clean code again, make certain you copy the entire block and aren't cutting anything off. Also make certain when pasting into functions you aren't breaking any existing elements.

    Paste into notepad or simple text to ensure your code is clean

    November 23, 2012 at 10:00 am #1354
    jaxy
    Member

    I've done it several times now, also pasted it straight into DreamWeaver what is also showing there's a syntax error on the 1st and also on the last line.

    As a side note, DW only shows there's an error when I take the existing functions.php code and add Nick's code to the end of it. If I had Nick's code on it's own it's fine.

    And I've made sure I'm pasting it at the very end of the functions.php file.

     

    November 23, 2012 at 10:08 am #1359
    SoZo
    Member

    It goes in functions.php but you don't need the span tags


    John “Nicolas Flamel” Wright | SoZo’s design| John Wright Photography

    November 23, 2012 at 10:24 am #1370
    jaxy
    Member

    Thanks, that worked. Easy if you know 🙂

    May 12, 2013 at 6:19 pm #40599
    Rednasil
    Participant

    Thanks for this one, helped me a lot.

  • Author
    Posts
Viewing 6 posts - 1 through 6 (of 6 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