Reputation: 1067
I want to get "abc_15_11_02_3" from http://example.com/project_name/abc_15_11_02_3/. How can i do this?
Upvotes: 77
Views: 253682
Reputation: 66
You need to set rewrite rule first for it.
rule normally follows the pagename $_GET query param
add_action('init', 'set_project_endpoint');
add_filter('query_vars', 'set_project_query_vars');
add_action('template_redirect', 'set_project_template_redirect');
/**
* This will set rewrite rule readable by wordpress
**/
function set_project_endpoint(){
add_rewrite_rule('^project_name/([A-Za-z0-9]+)/?$', 'index.php?pagename=project_name&project_id=$matches[1]', 'top');
}
/**
* This will set $_GET params to query_variable
**/
function set_project_query_vars($query_vars) {
$query_vars[] = 'project_id';
return $query_vars;
}
/**
* This will global as per your requirement
**/
function set_project_template_redirect() {
global $project_id;
$project_id = get_query_var('project_id');
// now you can use this in the project_name page or template
// just design based on project_id global tha's all
}
If you dont want to set global then you can get it by get_query_var() function
Upvotes: 0
Reputation: 2713
Current Post object.
global $post;
$slug = $post->post_name;
Other Options:
Any Post using ID.
$post = get_post($post_id);
$slug = $post->post_name;
In the loop
while (have_posts()) {
the_post();
$slug = get_post_field( 'post_name' );
}
Outside the loop using ID
$page_slug = get_post_field( 'post_name', $post_id );
Upvotes: 3
Reputation: 19
<?php
$args = array(
'post_type' => 'your_postype',
'orderby' => 'title',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
global $post;
$post_slug=$post->post_name;
echo $post_slug;
endwhile;
wp_reset_query();
endif;
?>
Upvotes: 2
Reputation: 419
Best option to do this according to WP Codex is as follow.
Use the global variable $post:
<?php
global $post;
$post_slug = $post->post_name;
?>
Upvotes: 12
Reputation: 566
this simple code worked for me:
$postId = get_the_ID();
$slug = basename(get_permalink($postId));
echo $slug;
Upvotes: 15
Reputation: 405
I came across this method and I use it to make div IDs the slug name inside the loop:
<?php $slug = basename( get_permalink() ); echo $slug;?>
Upvotes: 2
Reputation: 6668
Here is most advanced and updated version what cover many cases:
if(!function_exists('the_slug')):
function the_slug($post_id=false, $echo=true) {
global $product, $page;
if(is_numeric($post_id) && $post_id == intval($post_id)) {} else {
if(!is_object($post_id)){}else if(property_exists($post_id, 'ID')){
$post_id = $post_id->ID;
}
if(empty($post_id) && property_exists($product, 'ID')) $post_id = $product->ID;
if(empty($post_id)) $post_id = get_the_ID();
if(empty($post_id) && property_exists($page, 'ID')) $post_id = $page->ID;
}
if(!empty($post_id))
$slug = basename(get_permalink($post_id));
else
$slug = basename(get_permalink());
do_action('before_slug', $slug);
$slug = apply_filters('slug_filter', $slug);
if( $echo ) echo $slug;
do_action('after_slug', $slug);
return $slug;
}
endif;
This is collections from the best answers and few my updates.
Upvotes: 2
Reputation: 216
use global variable $post
<?php
global $post;
$post_slug=$post->post_name;
?>
Upvotes: 2
Reputation: 704
Wordpress: Get post/page slug
<?php
// Custom function to return the post slug
function the_slug($echo=true){
$slug = basename(get_permalink());
do_action('before_slug', $slug);
$slug = apply_filters('slug_filter', $slug);
if( $echo ) echo $slug;
do_action('after_slug', $slug);
return $slug;
}
?>
<?php if (function_exists('the_slug')) { the_slug(); } ?>
Upvotes: 6
Reputation: 1590
You can do this is in many ways like:
1- You can use Wordpress global variable $post
:
<?php
global $post;
$post_slug=$post->post_name;
?>
2- Or you can get use:
$slug = get_post_field( 'post_name', get_post() );
3- Or get full url and then use the PHP function parse_url
:
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$url_path = parse_url( $url, PHP_URL_PATH );
$slug = pathinfo( $url_path, PATHINFO_BASENAME );
I hope above methods will help you.
Upvotes: 22
Reputation: 1189
You can get that using the following methods:
<?php $post_slug = get_post_field( 'post_name', get_post() ); ?>
Or You can use this easy code:
<?php
global $post;
$post_slug = $post->post_name;
?>
Upvotes: 98
Reputation: 11808
If you want to get slug of the post from the loop then use:
global $post;
echo $post->post_name;
If you want to get slug of the post outside the loop then use:
$post_id = 45; //specify post id here
$post = get_post($post_id);
$slug = $post->post_name;
Upvotes: 37
Reputation: 4228
You can retrieve it from the post object like so:
global $post;
$post->post_name;
Upvotes: 5