2015-11-21 19 views

risposta

1

È possibile recuperare dall'oggetto posta in questo modo:

global $post; 
$post->post_name; 
10

Se si desidera ottenere slug del post dal loop quindi utilizzare:

global $post; 
echo $post->post_name; 

Se si vuole ottenere slug del post fuori dal ciclo quindi utilizzare:

$post_id = 45; //specify post id here 
$post = get_post($post_id); 
$slug = $post->post_name; 
4

È possibile fare questo è per molti versi come:

1- È possibile utilizzare Wordpress variabile globale $post:

<?php 
global $post; 
$post_slug=$post->post_name; 
?> 

2- Oppure si può ottenere l'uso:

$slug = get_post_field('post_name', get_post()); 

3- Oppure ottieni l'url completo e quindi utilizza la funzione 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); 

Spero che i metodi ti aiuteranno.

20

È possibile ottenere che utilizzando i seguenti metodi:

<?php $post_slug = get_post_field('post_name', get_post()); ?> 

oppure è possibile utilizzare questo codice facile:

<?php 
    global $post; 
    $post_slug = $post->post_name; 
?> 
2

Wordpress: Get Articolo/Pagina 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(); } ?> 
Problemi correlati