Forma adecuada de obtener contenido de la página


Tengo que obtener contenido específico de la página(me gusta la página (12))

Usé eso:

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

Work nice execpt para la compatibilidad con qtranslate devuelve texto en francés e inglés

Pero el bucle está bien, devuelve solo la versión en buen idioma

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

Así que la pregunta.... CÓMO obtener un contenido específico de la página insite el bucle...

 48
Author: menardmam, 2011-03-15

8 answers

He respondido a mi propia pregunta. Llama apply_filter y ahí lo tienes.

<?php 
$id=47; 
$post = get_post($id); 
$content = apply_filters('the_content', $post->post_content); 
echo $content;  
?>
 131
Author: menardmam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-18 17:02:45

Obtener el contenido de la página por nombre de página:

<?php
$page = get_page_by_title( 'page-name' );
$content = apply_filters('the_content', $page->post_content); 
echo $content;
?>
 33
Author: Alex,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-06-03 10:51:37

Usé esto:

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

Y esto aún más conciso:

<?= get_post_field('post_content', $post->ID) ?>
 7
Author: Fred K,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-20 14:29:13

Una forma sencilla y rápida de obtener el contenido por id:

echo get_post_field('post_content', $id);

Y si quieres formatear el contenido:

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

Funciona con páginas, publicaciones y publicaciones personalizadas.

 6
Author: Creaforge,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-06 22:07:37

Solo copie y pegue este código para obtener el contenido de su página.

<?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;
                ?>
 4
Author: naveen kumar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-24 07:28:09

La función wp_trim_words también puede limitar los caracteres cambiando la variable num num_words. Para cualquiera que pueda encontrar útil.

<?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;  
?>
 3
Author: keithshaw,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-24 18:13:36
$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;

/ / si desea una página específica de contenido, escriba en bucle

  if(get_the_ID=='11')//make sure to use get_the_ID instead the_ID
    {
    echo get_the_ID();
    the_title();
    the_content();
    }
 1
Author: Dinesh Bhimani,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-24 10:10:52

Un trazador de líneas:

<? if (have_posts()):while(have_posts()): the_post(); the_content(); endwhile; endif; ?>
 1
Author: Etienne Dupuis,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-12-12 20:21:29