I need to extract a url path number to a shortcode... Here is an example below...
I need the number "1" to be shown with a shortcode...
[url_path_number]
I need to extract a url path number to a shortcode... Here is an example below...
https://example.com/page/c/1
I need the number "1" to be shown with a shortcode...
[url_path_number]
You can find that with somthing like the following
add_shortcode( 'url_path_number', function ( $atts ) {
// use an attribute or the current URL
$a = shortcode_atts( array(
'url' => get_permalink(),
), $atts );
// get the path from the URL
$path = parse_url($a['url'],PHP_URL_PATH);
$parts = array_filter(explode('/', $path),function($v) { return $v !== ''; });
// get the last dir of path
return end($parts);
} );
Using the shortcode will return current URL
[url_path_number]
or you can specify the URL
[url_path_number url="https://example.com/page/c/1"]
to go just after the path, and not use get_permalink()
add_shortcode( 'url_path_number', function ( $atts ) {
$a = shortcode_atts( array(
'path' => $_SERVER['REQUEST_URI'],
), $atts );
$parts = array_filter(explode('/',$a['path']),function($v) { return $v !== ''; });
return end($parts);
} );
$_REQUEST
or$_GET
. However I think this value integer is the ID of a post, page or similar in your WP install, so that you can use the WP core function likeget_the_ID
. – bueltge Commented Jan 3, 2018 at 7:37