2

How can I get the current url in a wordpress page? I have enabled clean urls, but I want the url without the clean urls. For example

http://example.com/?page_id=1

instead of

http://example.com/my-page
2
  • what for do you need such url? Mar 8, 2013 at 10:32
  • Have you thought changing your permalinks? You can find the permalink settings in your admin area
    – Ken Gitahi
    Feb 22, 2014 at 19:26

2 Answers 2

1

Add the following custom function in functions.php file of your theme to get the page url without the clean urls

function get_page_custom_link() {
    global $post;
    $link = '';        
   if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
        $link = home_url('/');
    else
        $link = home_url( '?page_id=' . $post->ID );    
    return $link;
}

And call it in your template as following

<?php echo get_page_custom_link(); ?>

Note : It will not work for other urls( attachments, tags, taxonomy, post etc.)

0

You might be interested in url_to_postid function

Used as follows (example is from singular theme part eg. single.php, page.php):

$url = get_permalink();
$postid = url_to_postid( $url );
$post_type = get_post_type( $post_id );
switch ( $post_type ){
    case 'post':
        $not_nice_permalink = home_url( '?p=' . $post->ID );
        break;
    case 'page':
        $not_nice_permalink = home_url( '?page_id=' . $post->ID );
        break;
}

Note that this does not return the post id for custom post types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.