Display parent page URL for child page

I have this code that is working but is giving me the wrong link

gives me this link example.com/index.php/sample-page/

but I need this link example.com/sample-page/

 ?php global $post;
  if ( $post-post_parent ) { ?
    a href=?php echo get_permalink( $post-post_parent ); ? 
    ?php echo get_the_title( $post-post_parent ); ?
    /a Br?php echo get_permalink( $post-post_parent ); ?
?php } ?

Topic permalinks Wordpress

Category Web


$post->post_parent returns 0 when there's no parent associated. You need to check if the value is greater than 0.

<?php global $post;
  if ( $post->post_parent > 0 ) { ?>
    <a href="<?php echo get_permalink( $post->post_parent ); ?>" >
    <?php echo get_the_title( $post->post_parent ); ?>
    </a> <Br><?php echo get_permalink( $post->post_parent ); ?>
<?php } ?>

Also, check your permalink settings URL base.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.