2016-11-04 6 views
0

ページコンテンツに抜粋を追加するには?ページコンテンツWordPressに抜粋を追加するには?

私は、親ページの子ページを呼び出してショートコードを使用して別のページに表示するカスタムページテンプレートを用意しています。しかし、私はすべてのページ(25単語)に表示される単語を制限し、 "もっと読む"ボタンを入れたいと思います。

これを行う方法?これにより

<?php 
    $ids = array(); 
    $pages = get_pages("child_of=".$post->ID); 
     if ($pages){ 
      foreach ($pages as $page){ 
       $ids[] = $page->ID; 
      } 
     } 
    $paged = (get_query_var("paged")) ? get_query_var("paged") : 1; 
    $args = array(
      "paged" => $paged, 
      "post__in" => $ids, 
      "posts_per_page" => 3, 
      "post_type" => "page" 
    ); 
    query_posts($args); 
    if (have_posts()) : while (have_posts()) : the_post(); 
?> 
<div class="news-box"> 
    <div style="margin: 1em 0 0 0;"><?php the_post_thumbnail(); ?></div> 
    <div class="news-date"><?php the_date(); ?></div> 
    <div><?php the_content(); ?></div> 
</div> 

<?php endwhile; ?> 
    <?php endif; 
      paging_nav(); 
      wp_reset_query(); 
    ?> 

答えて

0
<?php echo wp_trim_words(get_the_content(), 20);?> 
<a href="<?php the_permalink(); ?>">Read More</a> 

単語の制限が20になり、読んでより多くのリンクがあるでしょう。代わりに

<?php the_content(); ?> 

を使用できます。

完了。

0

get_the_except()を使用して、投稿IDを渡して、ループ内の投稿の抜粋を取得できます。

<?php $postid = get_the_ID(); ?> 
<div class="news-box"> 
    <div style="margin: 1em 0 0 0;"><?php the_post_thumbnail(); ?></div> 
    <div class="news-date"><?php the_date(); ?></div> 
    <div> 
     <?php echo get_the_excerpt($postid); ?> 
     <a href="<?php echo get_the_permalink($postid); ?>" class="button">Read More</a> 
    </div> 
</div> 

次に、functions.phpファイルの抜粋をフィルタに追加します。

<?php 
add_filter('excerpt_length', 'custom_excerpt_length', 999); 
function custom_excerpt_length($length) { 
    return 25; 
} 
?> 
関連する問題