web-dev-qa-db-ja.com

特定のページのコンテンツを(IDで)取得する

以下のフロントページテンプレートを作成しました。

enter image description here

これらの大きな Lorem Ipsum ブロックの代わりに、私はそのボックス(特定の文字数)を埋めるために特定のページからの「抜粋」を示す必要があります。

ページのコンテンツを文字列形式で取得して、エコーアウトして特定の文字数に切り捨てる方法を教えてください。

13
Samuel Stiles
<?php

// would echo post 7's content up until the <!--more--> tag
$post_7 = get_post(7); 
$excerpt = $post_7->post_excerpt;
echo $excerpt;

// would get post 12's entire content after which you
// can manipulate it with your own trimming preferences
$post_12 = get_post(12); 
$trim_me = $post_12->post_content;
my_trim_function( $trim_me );

?>
19
Marc Dingena

どうぞ !

<?php
$my_id = 5369;
$post_id_5369 = get_post($my_id);
$content = $post_id_5369->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
?>
19
ameer hamza

あなたはこのコードを使用することができますそれはあなたのページ番号でpage_id = 19を細かく変更して動作します:

<?php $the_query = new WP_Query( 'page_id=19' ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post();  ?>

                       <?php the_excerpt(); ?>


     <?php endwhile;?>
2
Haitham Shehata
$post   = get_post( 42 );

$output =  apply_filters( 'the_content', $post->post_content );

echo $output;

からhttps://developer.wordpress.org/reference/functions/get_post/

あなたがループにいるならば、これをしてください:

<?php
$my_excerpt = get_the_excerpt();
if ( $my_excerpt != '' ) {
    // Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page

IDをお持ちの場合は、投稿を取得してpost_excerptメンバーvarを訴える

例えば.

$post = get_post( $post_id );
echo $post->post_excerpt;
0
Tom J Nowell

このコードを試して、あなたのpage_idを変更してください。

<?php $my_query = new WP_Query('page_id=20');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;?>
 <h3><?php the_title(); ?></h3>
    <div class="text">

        <?php echo wp_trim_words( get_the_content(), 15, '...' ); ?>
 <a href="<?php echo get_page_link(); ?>" class="read-more">Read More</a>
    </div>

 <?php endwhile; ?>
0