[Resolved] Where to paste the code for related posts

Home Forums Support [Resolved] Where to paste the code for related posts

Home Forums Support Where to paste the code for related posts

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1459879
    Sanu Kumar

    Hey there

    I found PHP code for related posts but I am afraid where to paste because it can break my whole site so could you take a look at this and helps me to paste the code given below

    <?php
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
     
    $args=array(
    'tag__in' => $tag_ids,
    'post__not_in' => array($post->ID),
    'showposts'=>4,  // Number of related posts that will be shown.
    'caller_get_posts'=>1
    );
     
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ) {
    echo '<div id="relatedposts"><h3>Related Posts</h3><ul>';
    while ($my_query->have_posts()) {
    $my_query->the_post();
    ?>
      
    <?php
    if ( has_post_thumbnail() ) { ?>
    <li><div class="relatedthumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?><?php the_title(); ?></a></div></li>
    <?php } else { ?>
    <li><div class="relatedthumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><img src="<?php echo get_post_meta($post->ID, 'Image',true) ?>" width="196" height="110" alt="<?php the_title_attribute(); ?>" /><?php the_title(); ?></a></div></li>
    <?php }
    ?>
      
    <?php
    }
    echo '</ul>';
    }
    }
    $post = $backup;
    wp_reset_query();
    ?>

    I want to show related posts just after the content

    #1460072
    Leo
    Staff
    Customer Support

    Hi there,

    We can’t gaurantee that the code will work and won’t cause any issues as it’s not coming from us.

    You can try to insert it using the generate_after_content hook using a hook element:
    https://docs.generatepress.com/article/hooks-visual-guide/#single-post
    https://docs.generatepress.com/article/hooks-element-overview/

    #1484460
    Eaydman

    Do you have a related post code snippet @leo ?

    #1484771
    David
    Staff
    Customer Support

    Hi there,

    no we do not have a specific code for Related Posts.
    You would be better off looking for a plugin that provides the specific kind of relationship you require.

    #1498009
    Sanu Kumar

    Hey there

    Can I know – Is this website is using “WP Show Post” plugin to show their “Related Post”. If not, could you tell me the name of that plugin or it is custom coded?

    #1498144
    David
    Staff
    Customer Support

    It looks like it is custom developed.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.