I created a function in my functions.php to show related posts for a custom posttype. Because the original developer used a themebuilder, I need to use a shortcode to display this function on the front-end.

A part of the code I use is

         while($related_cats_post->have_posts()): $related_cats_post->the_post();
            $postsList .= '<li><a href="' . the_permalink() . '">' .  the_title() . '</a></li>';
        return '<ul>' . $postsList . '</ul>';

It gets the job almost done. The only problem is that because I use the_permalink() and the_title(), the link and the title shows up on top of the page and not in the unordered list as they supposed to do. I don't know why this happens and how to fix this. Is it maybe because I call the function within a shortcode?

Is there a way to fix this?


You need to use the equivalent functions that return their value. Those are get_the_permalink() and get_the_title().

Most WordPress template functions that begin with the_ and echo their values have equivalent functions that return their values that begin with get_the_,

  • A thanks a LOT! Works perfect!
    – Ricardio
    Sep 17 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.