• ashtonfitzgerald

    (@ashtonfitzgerald)


    I am building a theme that has built into the sidebar a call to display a set of random posts from a given category using the get_posts function. It is configured to display the post title, excerpt, and read more link.

    It works perfectly on my main page (index.php). When I am viewing a post or page other than the index page it displays the post title and excerpt properly, but the permalink points to the current active page/post, rather than the post the title and excerpt are pulled from.

    This is the code I am using:

    <?php
     $postslist = get_posts('numberposts=3&orderby=rand&category_name=dog-training-videos');
     foreach ($postslist as $post) :
        setup_postdata($post); ?>
    
     <div style="border-bottom:1px solid #f0f0f0;">
     <br />
     <strong><?php the_title(); ?></strong>
     <?php the_excerpt(); ?>
    <em><a href="<?php the_permalink(); ?>">watch the dog training video...</a></em><br /><br />
     </div>
     <?php endforeach; ?>

    I have a feeling that this has something to do with being on the same page as the loop, but I’m relatively new to template tags still.

    This theme is currently active at: https://sitmeanssit.com (the main page displays and functions properly). https://sitmeanssit.com/contact/ is an example of how the permalinks go funny.

    Any help or direction on how to tweak this is already appreciated.

    Thanks,
    Ashton

Viewing 3 replies - 1 through 3 (of 3 total)
  • huggie

    (@huggie)

    Yeah, you’re probably right. I’d only use get_posts() on the main loop and wp_query() on the sidebar.

    Thread Starter ashtonfitzgerald

    (@ashtonfitzgerald)

    Thank you. I’m going to try using wp_query() and see how that works out.

    -Ashton

    Thread Starter ashtonfitzgerald

    (@ashtonfitzgerald)

    It turns out that the order of how the php tags are placed affects the outcome dramatically.

    Using my original code, if I place the_permalink before the_excerpt, the permalink points to the proper post. If it’s placed after the_excerpt, it displays the active page permalink.

    This occurs with wp_query as well.

    This is the original get_posts code, but with the ‘watch the dog training video’ link duplicated above the_excerpt. It displays properly above, but not below the_excerpt.

    <?php
     $postslist = get_posts('numberposts=3&orderby=rand&category_name=dog-training-videos');
     foreach ($postslist as $post) :
        setup_postdata($post); ?>
    
    	<em><a href="<?php the_permalink(); ?>">watch the dog training video...</a></em><br /><br />
    
     <div style="border-bottom:1px solid #f0f0f0;">
     <br />
     <strong><?php the_title(); ?></strong>
     <?php the_excerpt(); ?>
    <em><a href="<?php the_permalink(); ?>">watch the dog training video...</a></em><br /><br />
     </div>
     <?php endforeach; ?>

    Can anyone shed some light on this? I don’t mind just permalinking the post title, but now I’m curious how this is working.

    -Ashton

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using get_posts in Sidebar’ is closed to new replies.