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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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;
?>
回答2:
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;
回答3:
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.
回答4:
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(); } ?>
回答5:
You can retrieve it from the post object like so:
global $post;
$post->post_name;
回答6:
use global variable $post
<?php
global $post;
$post_slug=$post->post_name;
?>