Risposte:
Usa get_posts()e il parametro nameche è la lumaca:
$page = get_posts( array( 'name' => 'your-slug' ) );
if ( $page )
{
echo $page[0]->post_content;
}
Tieni presente che il tipo di post get_posts()predefinito è 'post'. Se vuoi una pagina usa ...
$page = get_posts(
array(
'name' => 'your-slug',
'post_type' => 'page'
)
);
Se desideri tutti i tipi di post pubblici (tranne gli allegati), imposta l'argomento del tipo di post su 'any'. Quindi potresti ottenere più di un risultato perché le lumache non sono univoche tra diversi tipi di post.
Puoi ottenere una pagina in base al titolo usando la get_page_by_title()funzione.
Puoi usarlo in questo modo (supponendo che tu voglia mostrare il contenuto):
$page = get_page_by_title('Your Title');
$content = apply_filters('the_content', $page->post_content);
echo $content;
A proposito, per ottenere la pagina usando slug:
function get_page_id_by_slug($slug){
global $wpdb;
$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$slug."'AND post_type = 'page'");
return $id;
}
$page = get_post(get_page_id_by_slug('my-slug'));
Leggi sui tag condizionali : is_page()prende anche la lumaca come argomento.
Quindi,
if( is_page( 'your-slug' ) ) {
// fetch content
}
farà quello che vuoi.
Se sei interessato a come recuperare il contenuto di post / pagina basato su una lumaca quando non su quella pagina, puoi anche alimentare get_postsuna lumaca. Questo non è documentato nel codice.
Quanto segue recupererà l'id da una lumaca:
$args = array(
'name' => 'your-slug'
);
$posts_from_slug = get_posts( $args );
// echo fetched content
echo $posts_from_slug[0]->post_content;
Usa get_page_by_path.
Sintassi
<?php get_page_by_path( $page_path, $output, $post_type ); ?>
Esempio:
//Returns the animal with the slug 'cat'
get_page_by_path('cat', OBJECT, 'animal');
per ulteriori informazioni, consultare la Guida alle funzioni di WordPress
Uso questo codice per popolare un modello di tema da una pagina,
$about = get_page_by_path('about');
$content = apply_filters( 'the_content', $about->post_content );
echo $content;