admin管理员组

文章数量:1287633

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

if($related_cats_post->have_posts()):
         while($related_cats_post->have_posts()): $related_cats_post->the_post();
            $postsList .= '<li><a href="' . the_permalink() . '">' .  the_title() . '</a></li>';
        endwhile;
    
        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?

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

if($related_cats_post->have_posts()):
         while($related_cats_post->have_posts()): $related_cats_post->the_post();
            $postsList .= '<li><a href="' . the_permalink() . '">' .  the_title() . '</a></li>';
        endwhile;
    
        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?

Share Improve this question edited Sep 17, 2021 at 12:07 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Sep 17, 2021 at 9:42 RicardioRicardio 381 silver badge8 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

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_,

本文标签: permalinksWordpress basic functions show on top in custom shortcode