2017-03-08 7 views
0

WordPressテーマをスタイリングしていますが、投稿のタイトルが60文字を超える場合は、最初の6文字の3点(.. 。)最後にネイティブPHPでWordPress:ポストタイトルの最初の60文字をエコーする

希望:あなたがコードに見ることができるよう

<?php  
    if (strlen($title) <= 60) { 
     echo %title 
    } else { 
     echo (substr($title, 60) . "..." 
    }       
?> 

を私の問題は、%タイトルのWordPress内での変数の構文は$タイトルではないということですが、

<?php previous_post_link('%link', '%title '); ?> 

私の質問は以下のとおりです。

  1. ワードプレス
  2. 内部のIF(三)形式の場合/他の速記になりますどのように最終的なものだろうか?

おかげ

+0

1ページを除くすべてのタイトルを切り捨てたいですか? –

+0

いいえ、これは各ポストの下部にあるポストナビゲーションにあります。だから、次の投稿と前の投稿のタイトルはそれほど大きくはありません。これは携帯電話版の@media(max-width:360px)にあります。 –

答えて

1

あなたは

<div class="prev-posts pull-left"> 
    <?php 
    $prev_post = get_previous_post(); 
    if ($prev_post) 
    { 
     $prev_title = strip_tags(str_replace('"', '', $prev_post->post_title)); 
     if (strlen($prev_title) >= 60) //<-- here is your custom checking 
     { 
      $prev_title = (substr($prev_title, 0, 60)) . "..."; 
     } 
     echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title . '" class=" "><strong><<< &quot;' . $prev_title . '&quot;</strong></a>' . "\n"; 
    } 

    ?> 
</div> 
<div class="next-posts pull-right"> 
    <?php 
    $next_post = get_next_post(); 
    if ($next_post) 
    { 
     $next_title = strip_tags(str_replace('"', '', $next_post->post_title)); 
     if (strlen($next_title) >= 60) //<-- here is your custom checking 
     { 
      $next_title = (substr($next_title, 0, 60)) . "..."; 
     } 
     echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title . '" class=" "><strong>&quot;' . $next_title . '&quot; >>></strong></a>' . "\n"; 
    } 

    ?> 
</div> 

カスタムpost_nav関数を作成することによって、これを達成、この情報がお役に立てば幸い!

+0

はい、素晴らしいです –

+0

素晴らしいですが、私の答えをupvoteすることを忘れないでください! –

+0

が完了しました。ありがとうございます –

関連する問題