Related Posts Code Problem

Community Forums Forums Design Tips and Tricks Related Posts Code Problem

This topic is: not resolved

This topic contains 5 replies, has 4 voices, and was last updated by  Rednasil 1 year, 4 months ago.

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1349

    jaxy
    Participant

    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.

    #1352

    sahdow
    Participant
    Post count: 4

    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

    #1354

    jaxy
    Participant

    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.

     

    #1359

    SoZo
    Moderator
    Post count: 1573

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


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

    #1370

    jaxy
    Participant

    Thanks, that worked. Easy if you know :)

    #40599

    Rednasil
    Participant
    Post count: 28

    Thanks for this one, helped me a lot.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.