質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

Q&A

解決済

1回答

3291閲覧

WordPressで投稿記事内のaタグが他の場所に生成されないようにしたい

raidomaru

総合スコア106

WordPress

WordPressは、PHPで開発されているオープンソースのブログソフトウェアです。データベース管理システムにはMySQLを用いています。フリーのブログソフトウェアの中では最も人気が高く、PHPとHTMLを使って簡単にテンプレートをカスタマイズすることができます。

0グッド

0クリップ

投稿2016/06/11 06:40

編集2016/06/11 16:04

###前提・実現したいこと
category.phpで投稿一覧を作っております。

###発生している問題・エラーメッセージ
投稿記事内のaタグが別の場所にも自動で生成されてしまいます。
これを止めるにはどうしたらいいでしょうか。

###記述コード
category.php

php

1<?php get_header();?> 2 3<div id="content"> 4<div id="newslist" class="clrFix"><!-- #ページ名 --> 5 <ul id="pankuzu" class="clrFix"> 6 <li><a href="#"><img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/pankuzu_home.png"></a></li> 7 <li>&gt;</li> 8 <li>お知らせ</li> 9 </ul> 10 <h2><img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/newslist_title_01.png" alt="News"></h2> 11 12 <?php 13$category = get_the_category(); 14$cat_name = $category[0]->cat_name; //カテゴリー名 15$cat_slug = $category[0]->category_nicename; //スラッグ名 16?> 17 18 19 <div id="main"> 20 21 <div class="tabwrap"> 22 <div class="tabinner"> 23 <ul id="tab"> 24 <li id="newstab"><h3><img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/newslist_ico_01_on.png" class="ico on"> 25 <img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/newslist_ico_01_off.png" class="ico off" style="display:none;">News一覧</h3></li> 26 <li id="schooltab" class="center"><h3><img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/newslist_ico_02_on.png" class="ico on" style="display:none;"> 27 <img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/newslist_ico_02_off.png" class="ico off">スクール情報</h3></li> 28 <li id="salontab"><h3><img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/newslist_ico_03_on.png" class="ico on" style="display:none;"> 29 <img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/newslist_ico_03_off.png" class="ico off">サロン情報</h3></li> 30 </ul> 31 <!-- /#tab --> 32 33 <!-- #wrapper --> 34 <div id="wrapper"> 35 36 <!-- .contents --> 37 <div class="contents"> 38<?php if(have_posts()): ?> 39 <?php while(have_posts()): ?> 40 <?php the_post(); ?> 41 42 <div class="content"> 43 <img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/top_news_0<?php $category=get_the_category(); $cat_slug=$category[0]->category_nicename; if($cat_slug=='news'){echo "2";}elseif($cat_slug=='nailschool'){echo "3";}elseif($cat_slug=='nailsalon'){echo "1";}?>.png" class="newstag"> 44 <p class="date"><?php the_time('Y/m/d'); ?></p> 45 <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> 46 <p class="text"><a href="<?php the_permalink(); ?>"><?php echo mb_substr($post->post_content,0, 100).'...';?></a></p> 47 </div> 48 49 <?php endwhile; ?> 50<?php endif; ?> 51 </div> 52 <!-- /.contents --> 53 54 <!-- .contents --> 55 <div class="contents"> 56<?php query_posts("cat=3"); ?> 57<?php if(have_posts()): ?> 58 <?php while(have_posts()): ?> 59 <?php the_post(); ?> 60 61 <div class="content"> 62 <img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/top_news_03.png" class="newstag"> 63 <p class="date"><?php the_time('Y/m/d'); ?></p> 64 <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> 65 <p class="text"><a href="<?php the_permalink();?>"><?php echo mb_substr($post->post_content,0, 100).'...';?></a></p> 66 </div> 67 68 <?php endwhile; ?> 69<?php endif; ?> 70<?php wp_reset_query(); ?> 71 72 73 </div> 74 <!-- /.contents --> 75 76 <!-- .contents --> 77 <div class="contents"> 78<?php query_posts("cat=4"); ?> 79<?php if(have_posts()): ?> 80 <?php while(have_posts()): ?> 81 <?php the_post(); ?> 82 83 <div class="content"> 84 <img src="<?php echo esc_url(get_template_directory_uri());?>/shared/images/top_news_01.png" class="newstag"> 85 <p class="date"><?php the_time('Y/m/d'); ?></p> 86 <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4> 87 <p class="text"><a href="<?php the_permalink();?>"><?php echo mb_substr($post->post_content,0, 100).'...';?></a></p> 88 </div> 89 90 <?php endwhile; ?> 91<?php endif; ?> 92<?php wp_reset_query(); ?> 93 </div> 94 <!-- /.contents --> 95 96 </div><!-- /#wrapper --> 97</div><!-- /#tabinner --> 98</div><!-- /#tabwrapper --> 99 </div><!-- #main --> 100 101</div><!-- #ページ名 --> 102 103</div><!-- #content --> 104 105<?php get_footer();?>

###該当部分のソース

html

1 <div class="contents"> 2 3 <div class="content"> 4 <img src="http://xxxx/wp-content/themes/xxxx/shared/images/top_news_02.png" class="newstag"> 5 <p class="date">2016/05/29</p> 6 <h4><a href="http://xxxx/news/%e4%bd%b5%e8%a8%ad%e3%83%8d%e3%82%a4%e3%83%ab%e3%82%b5%e3%83%ad%e3%83%b3%e5%90%8d%e5%a4%89%e6%9b%b4%e3%81%ae%e3%81%94%e6%a1%88%e5%86%85/">タイトル</a></h4> 7 <p class="text"><a href="http://xxxx/news/%e4%bd%b5%e8%a8%ad%e3%83%8d%e3%82%a4%e3%83%ab%e3%82%b5%e3%83%ad%e3%83%b3%e5%90%8d%e5%a4%89%e6%9b%b4%e3%81%ae%e3%81%94%e6%a1%88%e5%86%85/"><p>本文</a></p> 8 </div> 9 10 11 <div class="content"> 12 <img src="http://xxxx/wp-content/themes/xxxx/shared/images/top_news_02.png" class="newstag"> 13 <p class="date">2016/05/18</p> 14 <h4><a href="http://xxxx/news/%e6%a0%a1%e8%88%8e%e7%a7%bb%e8%bb%a2%e3%81%ae%e3%81%94%e6%a1%88%e5%86%85/">タイトル</a></h4> 15 <p class="text"><a href="http://xxxx/news/%e6%a0%a1%e8%88%8e%e7%a7%bb%e8%bb%a2%e3%81%ae%e3%81%94%e6%a1%88%e5%86%85/"><p>本文</a></p> 16 </div> 17 18 19 <div class="content"> 20 <img src="http://xxxx/wp-content/themes/xxxx/shared/images/top_news_02.png" class="newstag"> 21 <p class="date">2016/05/02</p> 22 <h4><a href="http://xxxx/news/%e5%88%9d%e5%bf%83%e8%80%85%e3%81%a7%e3%82%82%e5%a4%a7%e4%b8%88%e5%a4%ab%e2%98%86-%ef%bc%91day%e3%80%90%e3%82%bb%e3%83%ab%e3%83%95%e3%83%8d%e3%82%a4%e3%83%ab%e4%bd%93%e9%a8%93%e3%83%ac%e3%83%83/">タイトル</a></h4> 23 <p class="text"><a href="http://xxxx/news/%e5%88%9d%e5%bf%83%e8%80%85%e3%81%a7%e3%82%82%e5%a4%a7%e4%b8%88%e5%a4%ab%e2%98%86-%ef%bc%91day%e3%80%90%e3%82%bb%e3%83%ab%e3%83%95%e3%83%8d%e3%82%a4%e3%83%ab%e4%bd%93%e9%a8%93%e3%83%ac%e3%83%83/"><p><a href="http://ameblo.jp/xxxxtry-12153897623.html">本文</a></p> 24 </div> 25 26 27 <div class="content"> 28 <img src="http://xxxx/wp-content/themes/xxxx/shared/images/top_news_02.png" class="newstag"> 29 <p class="date">2016/05/02</p> 30 <h4><a href="http://xxxx/news/%e3%80%90%e5%b9%b3%e6%97%a5%e6%98%bc%e5%89%b2%e3%80%91%e3%83%bb%e3%80%90%e3%81%a8%e3%82%82%e5%89%b2%e3%80%91%e3%81%ae%e3%81%94%e6%a1%88%e5%86%85/">タイトル</a></h4> 31 <p class="text"><a href="http://xxxx/news/%e3%80%90%e5%b9%b3%e6%97%a5%e6%98%bc%e5%89%b2%e3%80%91%e3%83%bb%e3%80%90%e3%81%a8%e3%82%82%e5%89%b2%e3%80%91%e3%81%ae%e3%81%94%e6%a1%88%e5%86%85/"><p>本文<a href="http://ameblo.jp/xxxxtry-12146682084.html">本文リンクテキスト</a></p...</a></p> 32 </div> 33 34 35 <div class="content"> 36 <img src="http://xxxx/wp-content/themes/xxxx/shared/images/top_news_02.png" class="newstag"> 37 <p class="date">2016/05/02</p> 38 <h4><a href="http://xxxx/news/%ef%bd%87%ef%bd%97%e3%82%ad%e3%83%a3%e3%83%b3%e3%83%9a%e3%83%bc%e3%83%b3%e5%ae%9f%e6%96%bd%e4%b8%ad%ef%bc%81/">タイトル</a></h4> 39 <p class="text"><a href="http://xxxx/news/%ef%bd%87%ef%bd%97%e3%82%ad%e3%83%a3%e3%83%b3%e3%83%9a%e3%83%bc%e3%83%b3%e5%ae%9f%e6%96%bd%e4%b8%ad%ef%bc%81/"><p>本文<a href="http://ameblo.jp/xxxxtry-12154802979.html">本文リンクテキスト</a...</a></p> 40 </div> 41 42 43 </div> 44 <!-- /.contents -->

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kei344

2016/06/11 06:48

「別の場所」とはどこでしょうか。出力されたHTMLとスクリーンショット、category.php全文を追記されたほうが回答を得られやすいと思います。
raidomaru

2016/06/11 09:11

アドバイスありがとうございます。追記いたしました。
kei344

2016/06/11 11:32

出力されたHTML(ブラウザで「ページのソースを表示」)を質問文に追記できますか?
raidomaru

2016/06/11 16:05

文字数の関係で一部分になってます。
guest

回答1

0

ベストアンサー

HTML

1<p class="text"><a href="http://xxxx/news/(略)/"><p>本文</a></p> 2```この部分が問題ですね。 3```PHP 4<?php echo mb_substr($post->post_content,0, 100).'...';?> 5// を 6<?php echo esc_html( mb_substr( $post->post_content, 0, 100 ) ).'...';?> 7```多分ですが、こうしたら良いと思います。

投稿2016/06/11 16:13

kei344

総合スコア69407

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

raidomaru

2016/06/11 16:19

自動生成はされなくなりましたが、記事内のpタグなどが表示されてしまいました。
kei344

2016/06/11 16:24

あぁ、pタグとか入っているんですね。 <?php echo esc_html( strip_tags( mb_substr( $post->post_content, 0, 100 ) ) )).'...';?>
raidomaru

2016/06/11 16:34

できました!ありがとうございます! 申し訳ございませんが、なぜか一覧の記事が5記事しか表示されません。全部で7記事あるはずなのですが・・・
kei344

2016/06/11 17:26 編集

さっきのこうかな。 <?php echo esc_html( mb_substr( strip_tags( $post->post_content ), 0, 100 ) ).'...';?> > なぜか一覧の記事が5記事しか 質問内容が変わっていますよ。 【1ページに表示する最大投稿数を変更 - posts_per_page - WordPressの使い方と設定 - Webkaru】 http://webkaru.net/wordpress/posts-per-page/ 【WordPressでコンテンツごとにアーカイブページの表示件数を変更するベストな方法 | それからデザイン スタッフブログ】 http://sole-color-blog.com/blog/php/222/
raidomaru

2016/06/12 03:56

ありがとうございます!求めていたものができました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問