[Resolved] wp_get_recent_posts in element after_header_content wrong permalink

Home Forums Support [Resolved] wp_get_recent_posts in element after_header_content wrong permalink

Home Forums Support wp_get_recent_posts in element after_header_content wrong permalink

Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
    Posts
  • #1524204
    Niklas

    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?

    #1524305
    Leo
    Staff
    Customer Support

    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/

    #1524372
    Niklas

    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.

    #1524494
    Tom
    Lead Developer
    Lead Developer

    That’s strange. What if you add this in the loop?:

    var_dump($post['ID']);

    Does it output the correct ID?

    #1524974
    Niklas

    This shows the correct ID.

    #1525072
    David
    Staff
    Customer Support

    Hi there,

    do you have any functions that may be affecting the post loop or the excerpt ?

    #1525141
    Niklas

    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/

    #1525149
    David
    Staff
    Customer Support

    Any chance you can link us to a page where we can see the ‘broken’ post ?

    #1525163
    Niklas

    I can unlock the site for about 1 hour, I hope that’s enough:

    #1525172
    David
    Staff
    Customer Support

    Its requesting a login ? You can share login details in the Private Information field in your reply.

    #1525209
    Niklas

    Sorry there was a short delay, now the page can be reached without login.

    #1525237
    David
    Staff
    Customer Support

    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 ?

    #1525243
    Niklas

    the first link comes from: get_the_excerpt($post[‘ID’]);
    the second from: get_permalink($post[‘ID’]);

    #1525737
    Tom
    Lead Developer
    Lead Developer

    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 );
    #1527213
    Niklas

    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.

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