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
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
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.)
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.