初めまして、こんにちは!
前提・実現したいこと
WPでHPの修正を行っています。
トップページに表示されるブログの新着記事のタイトルの下に、また同じタイトルが表示されてしまうので
タイトルの下にはその投稿記事の本文の先頭から50文字位を表示させたいです。
< 該当箇所の画像 >
https://gyazo.com/a250e5d6368a3973113066365da44fdc
発生している問題・エラーメッセージ
functions.phpにされている記述を直せば上記が実現できるかも?と思ったのはいいのですが
調べてみても肝心の修正方法が間違っているみたいです...。
※エラーメッセージは
該当ソースコードの欄の最下部に記載しています。
該当のソースコード
HTML
1 <div class="box"> 2 <div class="info"> 3 <h3 class="mintyo">お知らせ</h3> 4 <div class="scroll"> 5 <ul class="list">[info]</ul> 6 </div> 7 </div>
php
1 2//新着一覧(カテゴリ別)を出力するショートコード 3function getInfoList($atts, $content = null){ 4 extract(shortcode_atts(array( 5 "num" => '5', 6 "cat" => 'info' 7 ), $atts)); 8 9 global $post; 10 $oldpost = $post; 11 //$myposts = get_posts('numberposts='.$num.'&order=DESC&orderby=post_date'); 12 $myposts = get_posts('numberposts='.$num.'&category_name='.$cat.'&order=DESC&orderby=post_date'); 13 $retHtml = ''; 14 15 foreach($myposts as $post): 16 setup_postdata($post); 17/* 18 //カテゴリIDの取得 19 $cat = get_the_category(); 20 $cat = $cat[0]; 21 $cat_name = $cat->cat_name; 22 $cat_slug = $cat->category_nicename; 23 $cat_id = $cat->cat_ID; 24*/ 25 $retHtml.='<li>'; 26 $retHtml.='<a href="' . get_permalink(). '">'; 27 if ( has_post_thumbnail( $post->ID ) ) : 28 $retHtml.= get_the_post_thumbnail($post->ID, 'thumbnail', array( 'class' => 'pic' )); 29 else : 30 $retHtml.= '<img src="' . get_bloginfo('stylesheet_directory') . '/img/home/info_1.png" class="pic">'; 31 endif; 32 33 $retHtml.='<div class="rbox">'; 34 $retHtml.='<span class="date">'.get_the_date('Y/m/d').'</span>'; 35 36 $retHtml.='<span class="title">'; 37 if (mb_strlen($post->post_title) > 15) { 38 $retHtml.=mb_substr($post->post_title, 0, 15).'...'; 39 }else{ 40 $retHtml.= get_the_title(); 41 } 42 $retHtml.='</span>'; 43 44 ★ $retHtml.='<span class="text">'; 45 if (mb_strlen($post->post_content) > 50) { 46 $retHtml.=mb_substr($post->post_content, 0, 50).'...'; 47 }else{ 48 $retHtml.= post_content(); 49 } 50 $retHtml.='</span>'; 51 $retHtml.='</div>'; 52 $retHtml.='</a>'; 53 54 $retHtml.='</li>'; 55 endforeach; 56 57 $post = $oldpost; 58 return $retHtml; 59} 60add_shortcode("info","getInfoList"); 61
この中の
PHP
1 $retHtml.='<span class="text">'; 2 if (mb_strlen($post->post_excerpt) > 50) { 3 $retHtml.=mb_substr($post->post_excerpt, 0, 50).'...'; 4 }else{ 5 $retHtml.= get_the_title(); 6 } 7 $retHtml.='</span>'; 8 $retHtml.='</div>'; 9 $retHtml.='</a>';
試したこと
【WordPress】投稿記事の抜粋文を表示させるthe_excerpt()の使い方メモ
|https://idotdesign.net/blog/web/wordpress/the_excerpt/
こちらの記事を参考にして
PHP
1 2// 字数を100文字に指定する 3function my_excerpt_mblength($length) { 4 return 50; 5} 6add_filter('excerpt_mblength', 'my_excerpt_mblength'); 7 8// 本文からの抜粋末尾の文字列を指定する 9function my_auto_excerpt_more($more) { 10 return '・・・'; 11} 12add_filter('excerpt_more', 'my_auto_excerpt_more'); 13 14// 抜粋末尾に個別投稿ページへのリンクを追加する 15function my_custom_excerpt_more($excerpt) { 16 return $excerpt . '<a href="' . get_permalink($post->ID) . '"> >> 記事を表示する</a>'; 17} 18add_filter('get_the_excerpt', 'my_custom_excerpt_more');;
をfunctions.php最下部に追記してみました。
何も起こりませんでした。
なので、他の正しく(自分がやりたいように)表示されているページのphpの記述を見ながら
下記のように書き換えましたところ
PHP
1 2 $retHtml.='<span class="text">'; 3 if (mb_strlen($post->post_content) > 50) { 4 $retHtml.=mb_substr($post->post_content, 0, 50).'...'; 5 }else{ 6 $retHtml.= post_content(); 7 } 8 $retHtml.='</span>'; 9 $retHtml.='</div>'; 10 $retHtml.='</a>'; 11 12 $retHtml.='</li>'; 13 endforeach; 14 15 $post = $oldpost; 16 return $retHtml; 17}
以下のようなエラーメッセージが出現しました。
wp-content/themes/yawaragien/functions.php ファイルの132行目のエラーのため、PHP コードの変更をロールバックしました。修正し、もう一度保存してください。 Uncaught Error: Call to undefined function post_content() in wp-content/themes/yawaragien/functions.php:132 Stack trace: #0 wp-includes/shortcodes.php(319): getInfoList('', '', 'info') #1 [internal function]: do_shortcode_tag(Array) #2 wp-includes/shortcodes.php(197): preg_replace_callback('/\[(\[?)(url|im...', 'do_shortcode_ta...', '<div class="sec...') #3 wp-includes/class-wp-hook.php(286): do_shortcode('<div class="sec...') #4 wp-includes/plugin.php(203): WP_Hook->apply_filters('<div class="sec...', Array) #5 wp-includes/post-template.php(240): apply_filters('the_content', '<div class="sec...') #6 wp-content/themes/yawaragien/content-page.php(25): the_content() 非表示にする
補足情報(FW/ツールのバージョンなど)
WordPress 4.9.10 (mymemory テーマ)
外観>テーマの編集>テーマのための関数 (functions.php)
不足している情報などありましたらご面倒ではございますが
お教えいただけますと幸いです。
よろしくお願いいたします!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/26 08:49
2019/03/26 08:51