2011-03-15 12 views
41

devo ottenere contenuti pagina specifica (come la pagina (12))modo corretto per ottenere il contenuto della pagina

Ho usato che:

<?php $id=47; $post = get_page($id); echo $post->post_content; ?> 

lavoro piacevole execpt per la compatibilità con qtranslate esso tornare francese e inglese testo

Ma il ciclo va bene, restituire solo la buona versione in lingua

<?php if(have_posts()) : while(have_posts()) : the_post(); ?> 
<div id="post"> 
<?php the_content(); ?> 
</div> <!-- .post --> 

Quindi la domanda .... Come ottenere un contenuto di pagina specifico insite il ciclo ...

risposta

120

Ho risposto alla mia stessa domanda. Chiama il numero apply_filter e via.

<?php 
$id=47; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 
echo $content; 
?> 
+0

veloce ed elegante. – Mina

+5

get_page è stato deprecato: http://codex.wordpress.org/Function_Reference/get_page. Usa invece get_post. – manishie

+0

+1 Grazie! Questo è utile anche quando si ha wp_mail in un ciclo. 'Get_the_content();' non era fuori mettendo il formato. Questo mi ha aiutato alla grande! – Davis

30

ottenere contenuti pagina nome della pagina:

<?php 
$page = get_page_by_title('page-name'); 
$content = apply_filters('the_content', $page->post_content); 
echo $content; 
?> 
+0

anche 'get_page_by_path' potrebbe essere utile. – Guss

5

Un modo veloce semplice per ottenere il contenuto da ID:

echo get_post_field('post_content', $id); 

E se si desidera ottenere il contenuto formattato:

echo apply_filters('the_content', get_post_field('post_content', $id)); 

Works with pages, post s post personalizzati &.

1
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
$args = array('prev_text' >' Previous','post_type' => 'page', 'posts_per_page' => 5, 'paged' => $paged); 
$wp_query = new WP_Query($args); 
while (have_posts()) : the_post(); 
//get all pages 
the_ID(); 
the_title(); 

//if you want specific page of content then write 
if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID 
{ 
echo get_the_ID(); 
the_title(); 
the_content(); 
} 

endwhile; 

// se si desidera che la pagina specifica del contenuto poi scrivere in loop

if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID 
    { 
    echo get_the_ID(); 
    the_title(); 
    the_content(); 
    } 
+0

Questo non risponde alla domanda; 'get_the_ID' ottiene l'ID del post corrente - la domanda sta cercando una * specifica * pagina all'interno del ciclo. – vicvicvic

+0

Se è possibile modificare la risposta e spiegare cosa fa il codice che si sta visualizzando, e perché/come quel codice risponde alla domanda, potrebbe davvero essere d'aiuto. I blocchi di codice non rappresentano di solito risposte utili. –

5

ho usato questo:

<?php echo get_post_field('post_content', $post->ID); ?> 

e questo ancora più conciso:

<?= get_post_field('post_content', $post->ID) ?> 
3

La funzione wp_trim_words può anche limitare i caratteri cambiando la variabile $ num_words. Per chiunque possa trovare utile.

<?php 
$id=58; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 

$customExcerpt = wp_trim_words($content, $num_words = 26, $more = ''); 
echo $customExcerpt; 
?> 
4

Basta copiare e incollare questo codice per ottenere il contenuto della pagina.

<?php 
       $pageid = get_the_id(); 
       $content_post = get_post($pageid); 
       $content = $content_post->post_content; 
       $content = apply_filters('the_content', $content); 
       $content = str_replace(']]>', ']]&gt;', $content); 
       echo $content; 
       ?> 
1

uno di linea:

<? if (have_posts()):while(have_posts()): the_post(); the_content(); endwhile; endif; ?> 
Problemi correlati