wordpress 如何从WordPress中的帖子中获取帖子?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33842251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get post slug from post in WordPress?
提问by user1990
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?
我想从http://example.com/project_name/abc_15_11_02_3/获取“abc_15_11_02_3” 。我怎样才能做到这一点?
回答by Keyur Patel
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;
?>
回答by WisdmLabs
If you want to get slug of the post from the loopthen use:
如果您想从循环中获取帖子的 slug,请使用:
global $post;
echo $post->post_name;
If you want to get slug of the post outside the loopthen use:
如果您想在循环外获取帖子的 slug,请使用:
$post_id = 45; //specify post id here
$post = get_post($post_id);
$slug = $post->post_name;
回答by Yatin Khullar
You can do this is in many ways like:
您可以通过多种方式执行此操作,例如:
1- You can use Wordpress global variable $post
:
1- 您可以使用 Wordpress 全局变量$post
:
<?php
global $post;
$post_slug=$post->post_name;
?>
2- Or you can get use:
2-或者你可以使用:
$slug = get_post_field( 'post_name', get_post() );
3- Or get full url and then use the PHP function parse_url
:
3- 或者获取完整的 url,然后使用 PHP 函数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.
希望以上方法能帮到你。
回答by theRana
Wordpress: Get post/page slug
Wordpress:获取帖子/页面 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(); } ?>
回答by Igor Yavych
You can retrieve it from the post object like so:
您可以像这样从 post 对象中检索它:
global $post;
$post->post_name;
回答by hossein naghneh
this simple code worked for me:
这个简单的代码对我有用:
$postId = get_the_ID();
$slug = basename(get_permalink($postId));
echo $slug;
回答by faryal rida
Best option to do this according to WP Codex is as follow.
根据 WP Codex 执行此操作的最佳选择如下。
Use the global variable $post:
使用全局变量 $post:
<?php
global $post;
$post_slug = $post->post_name;
?>
回答by Iisrael
I came across this method and I use it to make div IDs the slug name inside the loop:
我遇到了这种方法,并使用它使 div ID 成为循环内的 slug 名称:
<?php $slug = basename( get_permalink() ); echo $slug;?>
回答by ICG DEVS
use global variable $post
使用全局变量 $post
<?php
global $post;
$post_slug=$post->post_name;
?>
回答by Ivijan Stefan Stipi?
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.
这是最佳答案的集合,我的更新很少。