LoginSignup
0
0

More than 3 years have passed since last update.

【WordPress】ACFを使用し、チェックリストとテキストエリアの内容を足した文章を、特定の文字数以降は省略して表示する

Last updated at Posted at 2019-11-01

なにこれ?

個人的なメモ。
ACFで作った花名をチェックリストで選択し、リストにない花名が合った場合は「、」の後に「その他の花名テキスト」を表示。
PCの時のみ、2行を超えたら「…」で省略。
しっかり自分で書いたの初めてかもしれないので、見ちゃった人は生暖かい目で見て・・・でも間違ってたら教えて下さい。

コード

ファイル名.php
<?php
    $flowers_used_name = get_field('flowers_used_name');
    $flowers_used_name = implode('、', $flowers_used_name);
    $flowers_other_name = get_field('flowers_other_name');

    //文字列
    $flowers_used_and_other = $flowers_used_name.'、'.$flowers_other_name;
    $flowers_used = $flowers_used_name;
    $flowers_other = $flowers_other_name;
    //文字数の上限
    $limit = 41;

    if(!wp_is_mobile() && get_field('flowers_other_name') && mb_strlen($flowers_used_and_other) > $limit){
        //PCで、その他の花名の入力があって、文字数が上限を超える場合 
        $txt = mb_substr($flowers_used_and_other,0,$limit);
        echo $txt."…";
    }elseif(!wp_is_mobile() && !get_field('flowers_other_name') && mb_strlen($flowers_used) > $limit){
        //PCで、その他の花名の入力がなくて、文字数が上限を超える場合 
        $txt = mb_substr($flowers_used,0,$limit);
        echo $txt."…";
    }elseif(!wp_is_mobile() && get_field('flowers_other_name') && mb_strlen($flowers_used_and_other) <= $limit){
        //PCで、その他の花名の入力があって、文字数が上限を超えない場合 
        echo $flowers_used_and_other;
    }elseif(!wp_is_mobile() && !get_field('flowers_other_name') && mb_strlen($flowers_used_and_other) <= $limit){
        //PCで、その他の花名の入力がなくて、文字数が上限を超えない場合 
        echo $flowers_used;
    }else{
        echo $flowers_used_and_other;
    }
?>

リンク

参考サイト

PHPで文字数を制限し、超過分を『…』に置き換えるコード | Webコーダーdaimaの備忘録
[Wordpress/php] implodeで連結した配列要素の区切り文字(連結文字)を指定する | KERENOR { ケレンオール }
PHP入門:echoの使い方 | サービス | プロエンジニア

ちなみに昨日プロゲートやってif周りを復習してた。
シンプルなWebサイトを作ろう PHPコース | プログラミングの入門なら基礎から学べるProgate[プロゲート]

Codex

関数リファレンス/wp is mobile - WordPress Codex 日本語版

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0