Archived topic
wp_get_recent_posts in element after_header_content wrong permalink
16 replies · Started by Niklas on November 9, 2020
I want to display the latest post in the header. For this purpose I have created an element with the position after_header_content. There I call the latest post with the function wp_get_recent_posts(). This also works. However, the permalink of the post always refers to the page itself and not to the post?
Hi there,
This doesn't sound like a theme related issue.
Perhaps a plugin like WP Show Posts (also developed by Tom) would help?
https://en-ca.wordpress.org/plugins/wp-show-posts/
Hi Leo,
thanks for the quick answer. The wrong permalink only occurs when I place the latest post in a GP element, if I place the function in the content it works. In the GP element I call the function like this
$recent_posts = wp_get_recent_posts(
array(
'post_type' => 'post',
'numberposts' => 1,
'post_status' => 'publish',
)
);
foreach($recent_posts as $post) {
echo get_the_excerpt($post['ID']);
echo get_permalink($post['ID']);
}
This permalink of the get_the_excerpt() always refers to the page itself. get_permalink() works. This is strange.
That's strange. What if you add this in the loop?:
var_dump($post['ID']);
Does it output the correct ID?
This shows the correct ID.
Hi there,
do you have any functions that may be affecting the post loop or the excerpt ?
I have disabled several functions and plugins, the result is always the same. Maybe a similar problem is described here, unfortunately there is no solution:
https://wordpress.org/support/topic/read-more-buttons-below-posts-link-to-main-page/
Any chance you can link us to a page where we can see the 'broken' post ?
I can unlock the site for about 1 hour, I hope that's enough:
Its requesting a login ? You can share login details in the Private Information field in your reply.
Sorry there was a short delay, now the page can be reached without login.
I am seeing 2 x Read More Links - 1 of which is incorrect ( current page ) and the other which has the correct link ? What changed ?
the first link comes from: get_the_excerpt($post['ID']);
the second from: get_permalink($post['ID']);
This likely means the read more link that is filtered into the get_the_excerpt() function isn't working correctly.
You could try this:
function tu_disable_more_link() {
return '';
}
add_filter( 'excerpt_more', 'tu_disable_more_link', 999 );
get_the_excerpt($post['ID']);
remove_filter( 'excerpt_more', 'tu_disable_more_link', 999 );
This is one way around the problem. I would just like to know why the links have different targets. But some things may remain mysterious.