2016-07-06 20 views
1

私はこのWordPressのページテンプレートを特定の投稿からの抜粋を表示させようとしています。投稿が作成されたら、私は自分が望む場所にリンクを挿入することに確信していました。私はタイトル、サムネイル、パーマリンクなどをつかむことができますが、何らかの理由で私は抜粋を得ることができません。私は試しました:WordPressで投稿を取得する

the_excerpt(); 
get_the_excerpt(); 
the_content('',FALSE); 
get_the_content('', FALSE, ''); 
get_the_content('', TRUE); 

とりわけ、私が試してみたらget_the_content('', TRUE)はリンクの後にあるすべてのコンテンツを私に渡しますが、リンクの前に何が必要なのですか?

アイデア? ?

<?php 
     $query = 'cat=23&posts_per_page=1'; 
     $queryObject = new WP_Query($query); 
    ?> 

    <?php if($queryObject->have_posts()) : ?> 

     <div> 

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

       <div> 

        <h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> 

        <br> 

        <?php the_post_thumbnail() ?> 

        <?php #the_excerpt(); ?> 

        <div> 

         <a href="<?php the_permalink(); ?>">Read More</a> 

        </div> 

       </div> 

      <?php endwhile ?> 

     </div> 

    <?php endif; wp_reset_query(); 

>

答えて

1

てみてください、あなたのfunctions.phpにこれを追加し、ポストIDで抜粋を呼び出す:

get_excerpt_by_id($post->ID); 

//get excerpt by id 
function get_excerpt_by_id($post_id){ 
    $the_post = get_post($post_id); //Gets post ID 
    $the_excerpt = ($the_post ? $the_post->post_content : null); //Gets post_content to be used as a basis for the excerpt 
    $excerpt_length = 35; //Sets excerpt length by word count 
    $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images 
    $words = explode(' ', $the_excerpt, $excerpt_length + 1); 

    if(count($words) > $excerpt_length) : 
     array_pop($words); 
     array_push($words, '…'); 
     $the_excerpt = implode(' ', $words); 
    endif; 

    return $the_excerpt; 
} 

次に、このようなあなたのテンプレートでそれを呼びます

+0

ありがとうございました。これは、指定された単語数で抜粋したものを除いて動作します。私は、ユーザがその投稿のCMSを介してコンテンツ本体に ""リンクを手動で含めることができるようにしたいと考えています。このテンプレートは、そのタグを置くところで終了する抜粋を表示します。 – user2623706

1

これは私が思いついたものです。おそらくより良い解決策が、それは動作します!

function get_excerpt(){ 

    $page_object = get_page($post->ID); 

    $content = explode('<!--more-->', $page_object->post_content); 

    return $content[0]; 

} 

そして、このようにそれを呼び出す:

<?php echo get_excerpt(); ?> 
1

はここであなたのためのトリックを行いますかなりきちんとしたソリューションです!

<div class="post"> 
     <h3 class="title"><?php echo $post->post_title ?></h3> 
     <? 
     // Making an excerpt of the blog post content 
     $excerpt = strip_tags($post->post_content); 
     if (strlen($excerpt) > 100) { 
      $excerpt = substr($excerpt, 0, 100); 
      $excerpt = substr($excerpt, 0, strrpos($excerpt, ' ')); 
      $excerpt .= '...'; 
     } 
     ?> 
     <p class="excerpt"><?php echo $excerpt ?></p> 
     <a class="more-link" href="<?php echo get_post_permalink($post->ID); ?>">Read more</a> 
    </div> 
関連する問題