Home > Back-end >  Wordpress Display parent page URL for child page in a shortcode not working
Wordpress Display parent page URL for child page in a shortcode not working

Time:05-05

WordPress Display parent page URL for child page in a shortcode the problem is I can't manage to show in a simple shortcode

    function page_url_shortcode(){
  global $post;
  if ( $post->post_parent ) { 
    echo get_permalink( $post->post_parent );
 } 
}
add_shortcode('title_url','page_url_shortcode');

short code [title_url]

CodePudding user response:

Shortcodes are expected to return something, rather than echo the output. As per the docs (https://developer.wordpress.org/reference/functions/add_shortcode/):

Note that the function called by the shortcode should never produce an output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode.

Change your permalink call to:

return get_the_permalink($post->post_parent);

  • Related