<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>任意の場所 &#8211; ケケンタの独学ITブログ</title>
	<atom:link href="https://kekenta-it-blog.com/tag/%E4%BB%BB%E6%84%8F%E3%81%AE%E5%A0%B4%E6%89%80/feed/" rel="self" type="application/rss+xml" />
	<link>https://kekenta-it-blog.com</link>
	<description>プログラミングやWeb制作の情報を発信</description>
	<lastBuildDate>Tue, 18 Mar 2025 02:18:27 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://kekenta-it-blog.com/wp-content/uploads/2023/07/cropped-KEKENTA-BLOG-32x32.png</url>
	<title>任意の場所 &#8211; ケケンタの独学ITブログ</title>
	<link>https://kekenta-it-blog.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【SWELL】関連記事を任意の場所に表示する方法【ショートコード】</title>
		<link>https://kekenta-it-blog.com/swell-relation-post-shcode/</link>
					<comments>https://kekenta-it-blog.com/swell-relation-post-shcode/#respond</comments>
		
		<dc:creator><![CDATA[ケケンタ]]></dc:creator>
		<pubDate>Thu, 20 Jun 2024 07:27:17 +0000</pubDate>
				<category><![CDATA[SWELL]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[ショートコード]]></category>
		<category><![CDATA[任意の場所]]></category>
		<category><![CDATA[関連記事]]></category>
		<guid isPermaLink="false">https://kekenta-it-blog.com/?p=7956</guid>

					<description><![CDATA[<p><img src="https://kekenta-it-blog.com/wp-content/uploads/2024/06/eye-catch__swell-relation-post-shcode__7956-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>この記事ではSWELLで ショートコードを使用して「関連記事」を任意の場所に表示する方法 をご紹介します。 SWELLの「関連記事」はデフォルトだと決められた場所に表示されますが、 今回ご紹介する方法を使用することで自分 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://kekenta-it-blog.com/wp-content/uploads/2024/06/eye-catch__swell-relation-post-shcode__7956-1024x576.jpg" class="webfeedsFeaturedVisual" /></p>
<p class="wp-block-paragraph">この記事ではSWELLで</p>



<p class="has-text-align-center is-style-big_kakko_box wp-block-paragraph"><strong><span class="swl-marker mark_yellow">ショートコードを使用して「関連記事」を任意の場所に表示する方法</span></strong></p>



<p class="wp-block-paragraph">をご紹介します。</p>



<p class="wp-block-paragraph">SWELLの「関連記事」はデフォルトだと決められた場所に表示されますが、</p>



<p class="wp-block-paragraph">今回ご紹介する方法を使用することで<strong>自分の好きな場所に「関連記事」を表示できる</strong>ため、上手く活用することで<strong>回遊率を高めることが可能</strong>です。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="gray"><div class="c-balloon__icon -circle"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">ケケンタ</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>本記事の内容がお役に立てば何よりです！</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>

<div class="swell-block-postLink">			<a href="https://kekenta-it-blog.com/wp-edit-functions/" class="c-blogLink -internal" data-style="text">
				<!-- <i class="c-blogLink__icon icon-link" role="presentation"></i> -->
				<span class="c-blogLink__icon">
					<svg xmlns="http://www.w3.org/2000/svg" class="swl-svg-link __svg" width="1em" height="1em" viewBox="0 0 48 48" role="img" aria-hidden="true" focusable="false"><path d="M21.2 30.2c-.5 0-1-.2-1.4-.6l-.7-.7c-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l7.1-7.1c2.3-2.3 5.3-3.5 8.5-3.5s6.2 1.2 8.5 3.5c4.7 4.7 4.7 12.3 0 17l-3.5 3.5c-.8.8-2 .8-2.8 0-.8-.8-.8-2 0-2.8l3.5-3.5c3.1-3.1 3.1-8.2 0-11.3-1.5-1.5-3.5-2.3-5.7-2.3-2.1 0-4.2.8-5.7 2.3l-7.1 7.1c-1.5 1.5-2.3 3.5-2.3 5.7s.8 4.2 2.3 5.7l.7.7c.8.8.8 2 0 2.8-.4.3-.9.5-1.4.5z" /><path d="M13.4 46.6c-3.1 0-6.1-1.2-8.5-3.5-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l3.5-3.5c.8-.8 2-.8 2.8 0 .8.8.8 2 0 2.8l-3.5 3.5c-1.5 1.5-2.3 3.5-2.3 5.7 0 2.1.8 4.2 2.3 5.7 3.1 3.1 8.2 3.1 11.3 0l7.1-7.1c1.5-1.5 2.3-3.5 2.3-5.7 0-2.1-.8-4.2-2.3-5.7l-.7-.7c-.8-.8-.8-2 0-2.8.8-.8 2-.8 2.8 0l.7.7c2.3 2.3 3.5 5.3 3.5 8.5s-1.2 6.2-3.5 8.5l-7.1 7.1c-2.3 2.3-5.3 3.5-8.4 3.5z" /></svg>				</span>
				<span class="c-blogLink__text">【WordPress】functions.phpを編集する「３つ」の方法と編集時の注意点</span>
			</a>
		</div>

<div class="swell-block-postLink">			<a href="https://kekenta-it-blog.com/swell-title-eyecatch-re/" class="c-blogLink -internal" data-style="text">
				<!-- <i class="c-blogLink__icon icon-link" role="presentation"></i> -->
				<span class="c-blogLink__icon">
					<svg xmlns="http://www.w3.org/2000/svg" class="swl-svg-link __svg" width="1em" height="1em" viewBox="0 0 48 48" role="img" aria-hidden="true" focusable="false"><path d="M21.2 30.2c-.5 0-1-.2-1.4-.6l-.7-.7c-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l7.1-7.1c2.3-2.3 5.3-3.5 8.5-3.5s6.2 1.2 8.5 3.5c4.7 4.7 4.7 12.3 0 17l-3.5 3.5c-.8.8-2 .8-2.8 0-.8-.8-.8-2 0-2.8l3.5-3.5c3.1-3.1 3.1-8.2 0-11.3-1.5-1.5-3.5-2.3-5.7-2.3-2.1 0-4.2.8-5.7 2.3l-7.1 7.1c-1.5 1.5-2.3 3.5-2.3 5.7s.8 4.2 2.3 5.7l.7.7c.8.8.8 2 0 2.8-.4.3-.9.5-1.4.5z" /><path d="M13.4 46.6c-3.1 0-6.1-1.2-8.5-3.5-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l3.5-3.5c.8-.8 2-.8 2.8 0 .8.8.8 2 0 2.8l-3.5 3.5c-1.5 1.5-2.3 3.5-2.3 5.7 0 2.1.8 4.2 2.3 5.7 3.1 3.1 8.2 3.1 11.3 0l7.1-7.1c1.5-1.5 2.3-3.5 2.3-5.7 0-2.1-.8-4.2-2.3-5.7l-.7-.7c-.8-.8-.8-2 0-2.8.8-.8 2-.8 2.8 0l.7.7c2.3 2.3 3.5 5.3 3.5 8.5s-1.2 6.2-3.5 8.5l-7.1 7.1c-2.3 2.3-5.3 3.5-8.4 3.5z" /></svg>				</span>
				<span class="c-blogLink__text">【SWELL】タイトルとアイキャッチ画像の表示を上下逆にする方法</span>
			</a>
		</div>

<div class="swell-block-postLink">			<a href="https://kekenta-it-blog.com/swell-f-cr-link/" class="c-blogLink -internal" data-style="text">
				<!-- <i class="c-blogLink__icon icon-link" role="presentation"></i> -->
				<span class="c-blogLink__icon">
					<svg xmlns="http://www.w3.org/2000/svg" class="swl-svg-link __svg" width="1em" height="1em" viewBox="0 0 48 48" role="img" aria-hidden="true" focusable="false"><path d="M21.2 30.2c-.5 0-1-.2-1.4-.6l-.7-.7c-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l7.1-7.1c2.3-2.3 5.3-3.5 8.5-3.5s6.2 1.2 8.5 3.5c4.7 4.7 4.7 12.3 0 17l-3.5 3.5c-.8.8-2 .8-2.8 0-.8-.8-.8-2 0-2.8l3.5-3.5c3.1-3.1 3.1-8.2 0-11.3-1.5-1.5-3.5-2.3-5.7-2.3-2.1 0-4.2.8-5.7 2.3l-7.1 7.1c-1.5 1.5-2.3 3.5-2.3 5.7s.8 4.2 2.3 5.7l.7.7c.8.8.8 2 0 2.8-.4.3-.9.5-1.4.5z" /><path d="M13.4 46.6c-3.1 0-6.1-1.2-8.5-3.5-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l3.5-3.5c.8-.8 2-.8 2.8 0 .8.8.8 2 0 2.8l-3.5 3.5c-1.5 1.5-2.3 3.5-2.3 5.7 0 2.1.8 4.2 2.3 5.7 3.1 3.1 8.2 3.1 11.3 0l7.1-7.1c1.5-1.5 2.3-3.5 2.3-5.7 0-2.1-.8-4.2-2.3-5.7l-.7-.7c-.8-.8-.8-2 0-2.8.8-.8 2-.8 2.8 0l.7.7c2.3 2.3 3.5 5.3 3.5 8.5s-1.2 6.2-3.5 8.5l-7.1 7.1c-2.3 2.3-5.3 3.5-8.4 3.5z" /></svg>				</span>
				<span class="c-blogLink__text">【SWELL】誰でも簡単！フッターのコピーライトをリンクにする方法</span>
			</a>
		</div>

<div class="swell-block-postLink">			<a href="https://kekenta-it-blog.com/change-to-swell/" class="c-blogLink -internal" data-style="text">
				<!-- <i class="c-blogLink__icon icon-link" role="presentation"></i> -->
				<span class="c-blogLink__icon">
					<svg xmlns="http://www.w3.org/2000/svg" class="swl-svg-link __svg" width="1em" height="1em" viewBox="0 0 48 48" role="img" aria-hidden="true" focusable="false"><path d="M21.2 30.2c-.5 0-1-.2-1.4-.6l-.7-.7c-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l7.1-7.1c2.3-2.3 5.3-3.5 8.5-3.5s6.2 1.2 8.5 3.5c4.7 4.7 4.7 12.3 0 17l-3.5 3.5c-.8.8-2 .8-2.8 0-.8-.8-.8-2 0-2.8l3.5-3.5c3.1-3.1 3.1-8.2 0-11.3-1.5-1.5-3.5-2.3-5.7-2.3-2.1 0-4.2.8-5.7 2.3l-7.1 7.1c-1.5 1.5-2.3 3.5-2.3 5.7s.8 4.2 2.3 5.7l.7.7c.8.8.8 2 0 2.8-.4.3-.9.5-1.4.5z" /><path d="M13.4 46.6c-3.1 0-6.1-1.2-8.5-3.5-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l3.5-3.5c.8-.8 2-.8 2.8 0 .8.8.8 2 0 2.8l-3.5 3.5c-1.5 1.5-2.3 3.5-2.3 5.7 0 2.1.8 4.2 2.3 5.7 3.1 3.1 8.2 3.1 11.3 0l7.1-7.1c1.5-1.5 2.3-3.5 2.3-5.7 0-2.1-.8-4.2-2.3-5.7l-.7-.7c-.8-.8-.8-2 0-2.8.8-.8 2-.8 2.8 0l.7.7c2.3 2.3 3.5 5.3 3.5 8.5s-1.2 6.2-3.5 8.5l-7.1 7.1c-2.3 2.3-5.3 3.5-8.4 3.5z" /></svg>				</span>
				<span class="c-blogLink__text">無料テーマCocoonからSWELLに乗り換えた７つの理由【WordPress】</span>
			</a>
		</div>

<div class="swell-block-postLink">			<div class="p-blogCard -internal" data-type="type2" data-onclick="clickLink">
				<div class="p-blogCard__inner">
					<span class="p-blogCard__caption">あわせて読みたい</span>
					<div class="p-blogCard__thumb c-postThumb"><figure class="c-postThumb__figure"><img decoding="async" src="https://kekenta-it-blog.com/wp-content/uploads/2025/03/eye-catch__school-php-and-wp__14099-300x169.jpg" alt="" class="c-postThumb__img u-obf-cover" width="320" height="180"></figure></div>					<div class="p-blogCard__body">
						<a class="p-blogCard__title" href="https://kekenta-it-blog.com/school-php-and-wp/">PHPやWordPressを学ぶのにおすすめのプログラミングスクール【７選】</a>
						<span class="p-blogCard__excerpt">僕はこれまで独学でPHPを学習してきました。結論から言えば、それでもPHPを習得することはできました。 しかし、それに費やした累計学習期間は1年ほど。モチベーション&#8230;</span>					</div>
				</div>
			</div>
		</div>


<h2 class="wp-block-heading">【全体の流れ】ショートコードで「関連記事」を任意の場所に表示する</h2>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="gray"><div class="c-balloon__icon -circle"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">ケケンタ</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>この記事でご紹介する内容の流れをザックリまとめると以下のようになります。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<div class="swell-block-capbox cap_box"><div class="cap_box_ttl"><span>ショートコードを使用して「関連記事」を任意の場所に表示する流れ</span></div><div class="cap_box_content">
<ul class="wp-block-list is-style-num_circle -list-under-dashed">
<li>SWELLのデフォルト機能で出力されている「関連記事」のHTML構造に基づき、記事ごとの出力用HTMLを用意する</li>



<li>表示中の投稿の「関連記事」をWP_Queryで取得する</li>



<li>①で書いた処理と②で取得したデータをループで回して「関連記事」を出力する</li>



<li>ここまでの処理①～③をショートコードに登録して任意の場所で表示できるようにする</li>
</ul>
</div></div>



<h2 class="wp-block-heading">ソースコード</h2>



<p class="wp-block-paragraph"><strong>ソースコード全体</strong>は以下の通りです。</p>



<p class="is-style-big_icon_check wp-block-paragraph">以下<strong>２つのソースコード</strong>をfunctions.phpに追記し、ショートコード<strong>「my_short_create_related_post_list」</strong>を任意の場所に記述するだけで「関連記事」を出力できます。</p>



<p class="is-style-bg_stripe wp-block-paragraph">※子テーマのfunctoins.phpにソースコードを追加する手順は<a href="#add_functinons">こちら</a>（この記事内の別項目に移動します）</p>



<p class="has-text-align-center is-style-balloon_box wp-block-paragraph"><strong>記事ごとの出力用HTML</strong></p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-php" data-lang="PHP"><code>function create_related_post() {
  // サムネイル（サムネイルの有無によって表示する画像をサムネイル or NO IMAGE画像を設定）
  $thumb_url = has_post_thumbnail() 
                  ? get_the_post_thumbnail_url(get_the_ID(), &#39;medium&#39;) 
                  : &#39;https://サイトドメイン（例：kekenta-it-blog.com）/wp-content/themes/swell/assets/img/no_img.png&#39;;
                     // ↑このURL＝NO IMAGE画像は任意のものでOK
  $permalink = get_the_permalink();   // 記事リンク
  $title     = get_the_title();       // タイトル
  $date      = get_the_date(&#39;Y-m-d&#39;); // 公開日

  // 関連記事のHTML
  $html  = &lt;&lt;&lt;CURRENT_POST
    &lt;li class=&quot;p-postList__item&quot;&gt;
      &lt;a href=&quot;{$permalink}&quot; class=&quot;p-postList__link&quot;&gt;
        &lt;div class=&quot;p-postList__thumb c-postThumb&quot;&gt;
          &lt;figure class=&quot;c-postThumb__figure&quot;&gt;
            &lt;img width=&quot;300&quot; height=&quot;169&quot; src=&quot;{$thumb_url}&quot; alt=&quot;{$title}&quot; class=&quot;c-postThumb__img u-obf-cover&quot;&gt;
          &lt;/figure&gt;
        &lt;/div&gt;
        &lt;div class=&quot;p-postList__body&quot;&gt;
          &lt;div class=&quot;p-postList__title&quot;&gt;{$title}&lt;/div&gt;
          &lt;div class=&quot;p-postList__meta&quot;&gt;
            &lt;div class=&quot;p-postList__times c-postTimes u-thin&quot;&gt;
              &lt;time class=&quot;c-postTimes__posted icon-posted&quot; datetime=&quot;{$date}&quot; aria-label=&quot;公開日&quot;&gt;{$date}&lt;/time&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;/a&gt;
    &lt;/li&gt;
    CURRENT_POST;

  return $html;
}</code></pre></div>



<p class="has-text-align-center is-style-balloon_box wp-block-paragraph"><strong>「WP_Queryで取得した関連記事をループで取得する処理」をショートコードに登録</strong></p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-php" data-lang="PHP"><code>function create_related_post_list() {
  $html  = &#39;&lt;section class=&quot;l-articleBottom__section -related&quot;&gt;&#39;;
  $html .= &#39;&lt;h2 class=&quot;l-articleBottom__title c-secTitle&quot;&gt;関連記事&lt;/h2&gt;&#39;;
  $html .= &#39;&lt;ul class=&quot;p-postList p-relatedPosts -type-card&quot;&gt;&#39;;

  // 関連記事の呼び出し
  $args = array(
    &#39;post_type&#39;       =&gt; &#39;post&#39;,                      // 「投稿」の記事を取得
    &#39;category_name&#39;   =&gt; get_the_category()[0]-&gt;slug, // 表示中の記事のカテゴリーを指定
    &quot;post__not_in&quot;    =&gt; [get_the_ID()],              // 表示中の記事は「関連記事」に含めない
    &#39;posts_per_page&#39;  =&gt; 8,                           // 8件の記事を取得する（※調整可能）
  );
  $the_query = new WP_Query( $args );

  if ( $the_query-&gt;have_posts() ) :  
    while ( $the_query-&gt;have_posts() ) : $the_query-&gt;the_post();

      // 関連記事をひとつずつ取得
      $html .= create_related_post();
    
    endwhile;
  endif;
  wp_reset_postdata();

  $html .= &#39;&lt;/ul&gt;&lt;/section&gt;&#39;;

  echo $html;
}
add_shortcode(&#39;my_short_create_related_post_list&#39;, &#39;create_related_post_list&#39;);</code></pre></div>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="gray"><div class="c-balloon__icon -circle"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">ケケンタ</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>ご興味のある方に向けて、以下より簡単に解説をしていきます。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<h2 class="wp-block-heading">解説</h2>



<h3 class="wp-block-heading">①<strong> 記事ごとの出力用HTML</strong></h3>



<p class="is-style-bg_stripe wp-block-paragraph">今回、「関連記事」のHTML構造をあえて<strong><span class="swl-marker mark_yellow">SWELLで使用されているものと同じもの</span></strong>にしています。</p>



<p class="is-style-balloon_box2 wp-block-paragraph"><strong>理由</strong></p>



<ul class="wp-block-list -list-under-dashed is-style-num_circle">
<li>構造をまったく同じにすればSWELLでデフォルトで使用している「関連記事」と同じ見た目になる</li>



<li>SWELLで設定した内容が今回作成する「関連記事」の見た目にも反映されるようになる</li>
</ul>



<p class="is-style-icon_pen wp-block-paragraph">ここで言っている構造とは、<strong>HTMLタグだけでなく、classや属性などすべてを含めたもの</strong>を指しています。<br>実際のSWELLデフォルトの関連記事のHTML構造はGoogle chromeの<strong>検証ツール</strong>で確認できます。</p>


<div class="swell-block-postLink">			<a href="https://kekenta-it-blog.com/css-veri-g-tool/" class="c-blogLink -internal" data-style="text">
				<!-- <i class="c-blogLink__icon icon-link" role="presentation"></i> -->
				<span class="c-blogLink__icon">
					<svg xmlns="http://www.w3.org/2000/svg" class="swl-svg-link __svg" width="1em" height="1em" viewBox="0 0 48 48" role="img" aria-hidden="true" focusable="false"><path d="M21.2 30.2c-.5 0-1-.2-1.4-.6l-.7-.7c-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l7.1-7.1c2.3-2.3 5.3-3.5 8.5-3.5s6.2 1.2 8.5 3.5c4.7 4.7 4.7 12.3 0 17l-3.5 3.5c-.8.8-2 .8-2.8 0-.8-.8-.8-2 0-2.8l3.5-3.5c3.1-3.1 3.1-8.2 0-11.3-1.5-1.5-3.5-2.3-5.7-2.3-2.1 0-4.2.8-5.7 2.3l-7.1 7.1c-1.5 1.5-2.3 3.5-2.3 5.7s.8 4.2 2.3 5.7l.7.7c.8.8.8 2 0 2.8-.4.3-.9.5-1.4.5z" /><path d="M13.4 46.6c-3.1 0-6.1-1.2-8.5-3.5-2.3-2.3-3.5-5.3-3.5-8.5s1.2-6.2 3.5-8.5l3.5-3.5c.8-.8 2-.8 2.8 0 .8.8.8 2 0 2.8l-3.5 3.5c-1.5 1.5-2.3 3.5-2.3 5.7 0 2.1.8 4.2 2.3 5.7 3.1 3.1 8.2 3.1 11.3 0l7.1-7.1c1.5-1.5 2.3-3.5 2.3-5.7 0-2.1-.8-4.2-2.3-5.7l-.7-.7c-.8-.8-.8-2 0-2.8.8-.8 2-.8 2.8 0l.7.7c2.3 2.3 3.5 5.3 3.5 8.5s-1.2 6.2-3.5 8.5l-7.1 7.1c-2.3 2.3-5.3 3.5-8.4 3.5z" /></svg>				</span>
				<span class="c-blogLink__text">検証ツールでCSSの反映状況を確認して対処する方法【デベロッパーツール】</span>
			</a>
		</div>


<p class="wp-block-paragraph">デフォルトの「関連記事」の構造を引用して組み立てたのが以下のソースコードです。</p>



<p class="is-style-balloon_box2 wp-block-paragraph">デフォルトの「関連記事」の構造を引用して組み立てたソースコード</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-php" data-lang="PHP" data-line="3-9"><code>function create_related_post() {
  // サムネイル（サムネイルの有無によって表示する画像をサムネイル or NO IMAGE画像を設定）
  $thumb_url = has_post_thumbnail() 
                  ? get_the_post_thumbnail_url(get_the_ID(), &#39;medium&#39;) 
                  : &#39;https://サイトドメイン（例：kekenta-it-blog.com）/wp-content/themes/swell/assets/img/no_img.png&#39;;
                     // ↑このURL＝NO IMAGE画像は任意のものでOK
  $permalink = get_the_permalink();   // 記事リンク
  $title     = get_the_title();       // タイトル
  $date      = get_the_date(&#39;Y-m-d&#39;); // 公開日

  // 関連記事のHTML
  $html  = &lt;&lt;&lt;CURRENT_POST
    &lt;li class=&quot;p-postList__item&quot;&gt;
      &lt;a href=&quot;{$permalink}&quot; class=&quot;p-postList__link&quot;&gt;
        &lt;div class=&quot;p-postList__thumb c-postThumb&quot;&gt;
          &lt;figure class=&quot;c-postThumb__figure&quot;&gt;
            &lt;img width=&quot;300&quot; height=&quot;169&quot; src=&quot;{$thumb_url}&quot; alt=&quot;{$title}&quot; class=&quot;c-postThumb__img u-obf-cover&quot;&gt;
          &lt;/figure&gt;
        &lt;/div&gt;
        &lt;div class=&quot;p-postList__body&quot;&gt;
          &lt;div class=&quot;p-postList__title&quot;&gt;{$title}&lt;/div&gt;
          &lt;div class=&quot;p-postList__meta&quot;&gt;
            &lt;div class=&quot;p-postList__times c-postTimes u-thin&quot;&gt;
              &lt;time class=&quot;c-postTimes__posted icon-posted&quot; datetime=&quot;{$date}&quot; aria-label=&quot;公開日&quot;&gt;{$date}&lt;/time&gt;
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;/a&gt;
    &lt;/li&gt;
    CURRENT_POST;

  return $html;
}</code></pre></div>



<p class="is-style-bg_stripe wp-block-paragraph">３～９行目では記事ひとつひとつの<strong>アイキャッチ画像</strong>や<strong>個別ページへのリンク</strong>、<strong>タイトル</strong>、<strong>日付</strong>を取得しています。</p>



<p class="wp-block-paragraph">ちなみに以下の部分で設定している<strong>URL</strong>は「<strong>アイキャッチ画像が未設定だった場合に出力される画像のURL</strong>」です。</p>



<p class="is-style-balloon_box2 wp-block-paragraph">アイキャッチ画像が未設定だった場合に出力される画像のURL</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-php" data-lang="PHP" data-line="3"><code>$thumb_url = has_post_thumbnail() 
                  ? get_the_post_thumbnail_url(get_the_ID(), &#39;medium&#39;) 
                  : &#39;https://サイトドメイン（例：kekenta-it-blog.com）/wp-content/themes/swell/assets/img/no_img.png&#39;;
                     // ↑このURL＝NO IMAGE画像は任意のものでOK</code></pre></div>



<p class="is-style-icon_pen wp-block-paragraph">上記のソースコードではSWELLでデフォルトで用意されている<strong>no_img.png</strong>を指定しています。</p>



<p class="wp-block-paragraph">そして以下の部分に各情報を当て込み、このあと改めてご紹介するループ処理で記事ひとつひとつが正しく出力されるように構造を整えています。</p>



<p class="is-style-balloon_box2 wp-block-paragraph">HTML内に取得済みの各情報を当て込んでいる</p>



<div class="hcb_wrap"><pre class="prism line-numbers lang-php" data-lang="PHP"><code>// 関連記事のHTML
$html  = &lt;&lt;&lt;CURRENT_POST
  &lt;li class=&quot;p-postList__item&quot;&gt;
    &lt;a href=&quot;{$permalink}&quot; class=&quot;p-postList__link&quot;&gt;
      &lt;div class=&quot;p-postList__thumb c-postThumb&quot;&gt;
        &lt;figure class=&quot;c-postThumb__figure&quot;&gt;
          &lt;img width=&quot;300&quot; height=&quot;169&quot; src=&quot;{$thumb_url}&quot; alt=&quot;{$title}&quot; class=&quot;c-postThumb__img u-obf-cover&quot;&gt;
        &lt;/figure&gt;
      &lt;/div&gt;
      &lt;div class=&quot;p-postList__body&quot;&gt;
        &lt;div class=&quot;p-postList__title&quot;&gt;{$title}&lt;/div&gt;
        &lt;div class=&quot;p-postList__meta&quot;&gt;
          &lt;div class=&quot;p-postList__times c-postTimes u-thin&quot;&gt;
            &lt;time class=&quot;c-postTimes__posted icon-posted&quot; datetime=&quot;{$date}&quot; aria-label=&quot;公開日&quot;&gt;{$date}&lt;/time&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;/a&gt;
  &lt;/li&gt;
  CURRENT_POST;</code></pre></div>



<h3 class="wp-block-heading">②「WP_Queryで取得した関連記事をループで取得する処理」をショートコードに登録</h3>



<p class="wp-block-paragraph">２つ目にご紹介したソースコードでは</p>



<ul class="wp-block-list is-style-num_circle -list-under-dashed">
<li>まずWP_Queryで<strong>現在表示中の投稿の「関連記事」</strong>（ここでは同じカテゴリーを８記事）を取得し</li>



<li>それを「<strong>①記事ごとの出力用HTML</strong>」でご紹介した処理を用いてループで出力しています</li>
</ul>



<div class="hcb_wrap"><pre class="prism line-numbers lang-plain"><code>function create_related_post_list() {
  $html  = &#39;&lt;section class=&quot;l-articleBottom__section -related&quot;&gt;&#39;;
  $html .= &#39;&lt;h2 class=&quot;l-articleBottom__title c-secTitle&quot;&gt;関連記事&lt;/h2&gt;&#39;;
  $html .= &#39;&lt;ul class=&quot;p-postList p-relatedPosts -type-card&quot;&gt;&#39;;

  // 関連記事の呼び出し
  $args = array(
    &#39;post_type&#39;       =&gt; &#39;post&#39;,                      // 「投稿」の記事を取得
    &#39;category_name&#39;   =&gt; get_the_category()[0]-&gt;slug, // 表示中の記事のカテゴリーを指定
    &quot;post__not_in&quot;    =&gt; [get_the_ID()],              // 表示中の記事は「関連記事」に含めない
    &#39;posts_per_page&#39;  =&gt; 8,                           // 8件の記事を取得する（※調整可能）
  );
  $the_query = new WP_Query( $args );

  // ここからループを回している
  if ( $the_query-&gt;have_posts() ) :  
    while ( $the_query-&gt;have_posts() ) : $the_query-&gt;the_post();

      // 関連記事をひとつずつ取得
      $html .= create_related_post();
    
    endwhile;
  endif;
  wp_reset_postdata();
  // ループ処理ここまで

  $html .= &#39;&lt;/ul&gt;&lt;/section&gt;&#39;;

  echo $html;
}
add_shortcode(&#39;my_short_create_related_post_list&#39;, &#39;create_related_post_list&#39;);</code></pre></div>



<p class="is-style-bg_stripe wp-block-paragraph">ここで登場しているHTML構造もSWELLのデフォルトの「関連記事」を引用したものとなっています。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="gray"><div class="c-balloon__icon -circle"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">ケケンタ</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>最終行のadd_shortcodeで上記の処理をショートコード名「my_short_create_related_post_list」として登録をしています。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<p class="wp-block-paragraph">以上で任意の場所に「関連記事」を出力するショートコードの用意ができました。</p>



<p class="wp-block-paragraph">あとはこれを好きな場所（例えばウィジェットの「記事下部」）に貼り付ければ、「関連記事」が出力されます。</p>



<p class="has-text-align-center is-style-balloon_box2 wp-block-paragraph">使用例</p>


<div class="wp-block-image is-style-shadow size_s">
<figure class="aligncenter size-full"><img decoding="async" width="463" height="305" src="https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-51.png" alt="記事下部に「関連記事」を出力するショートコードを設定する" class="wp-image-8477" srcset="https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-51.png 463w, https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-51-300x198.png 300w" sizes="(max-width: 463px) 100vw, 463px" /><figcaption class="wp-element-caption">（例）記事下部に「関連記事」を出力するショートコードを設定</figcaption></figure>
</div>


<p class="is-style-icon_info wp-block-paragraph">もしデフォルトの関連記事を使用している場合は表示が重複することになるため適宜非表示にしましょう。</p>



<h2 class="wp-block-heading" id="add_functinons">SWELLの子テーマのfunctions.phpにソースコードを追加する方法</h2>



<p class="wp-block-paragraph">まずは管理画面の「外観」→「テーマファイルエディタ」をクリックします。</p>



<figure class="wp-block-image size-full is-style-shadow"><img decoding="async" width="403" height="249" src="https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-8.png" alt="外観→テーマファイルエディタ" class="wp-image-7694" srcset="https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-8.png 403w, https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-8-300x185.png 300w" sizes="(max-width: 403px) 100vw, 403px" /><figcaption class="wp-element-caption">「テーマファイルエディタ」の場所</figcaption></figure>



<p class="wp-block-paragraph">すると以下のような画面が表示されるので、右側にある「<strong>Theme Functions（functions.php）</strong>」をクリックします。</p>



<figure class="wp-block-image size-large size_s is-style-shadow"><img decoding="async" width="1024" height="565" src="https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-9-1024x565.png" alt="SWELLで外観→テーマファイルエディタをクリックした直後の様子" class="wp-image-7695" srcset="https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-9-1024x565.png 1024w, https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-9-300x166.png 300w, https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-9-768x424.png 768w, https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-9.png 1159w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">functions.phpの場所</figcaption></figure>



<p class="is-style-icon_info wp-block-paragraph">このとき、念のため右上の「編集するテーマを選択」が「SWELL CHILD」になっていることをご確認下さい。<br>※もしなっていなければ、「SWELL CHILD」を選び、「選択」ボタンをクリックして下さい。</p>



<p class="wp-block-paragraph">続いて、<strong>エディタの末尾にソースコードを追加</strong>します。</p>



<figure class="wp-block-image size-full size_s is-style-shadow"><img decoding="async" width="957" height="802" src="https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-11.png" alt="SWELLでのfunctions.phpの編集個所" class="wp-image-7697" srcset="https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-11.png 957w, https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-11-300x251.png 300w, https://kekenta-it-blog.com/wp-content/uploads/2024/06/image-11-768x644.png 768w" sizes="(max-width: 957px) 100vw, 957px" /><figcaption class="wp-element-caption">functions.phpの編集内容</figcaption></figure>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="gray"><div class="c-balloon__icon -circle"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">ケケンタ</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>ソースコードを追加したら、最後に画面左下の「ファイルを更新」をクリックして完了です。</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<h2 class="wp-block-heading">まとめ</h2>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="gray"><div class="c-balloon__icon -circle"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://kekenta-it-blog.com/wp-content/uploads/2023/07/キリッ-150x150.jpg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">ケケンタ</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>以上が<strong><span class="swl-marker mark_yellow">SWELLで「関連記事」を任意の場所に出力する方法</span></strong>でした！</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div>


<p class="wp-block-paragraph">SWELLのデフォルト機能では関連記事は元々決められた場所にしか出力することができません。</p>



<p class="wp-block-paragraph">しかし、今回ご紹介した方法を使用すれば自分が好きな場所にショートコードを設置＝「関連記事」を出力することが可能です。</p>



<p class="wp-block-paragraph">オリジナリティを出したり回遊率を高めるために上手く活用していただければ何よりです！</p>



<p class="wp-block-paragraph">このブログでは、他にもWordPressやWeb制作、PHPについて様々な情報を発信していますので、色々見ていただけると嬉しいです。</p>


<div class="swell-block-balloon"><div class="c-balloon -bln-left" data-col="gray"><div class="c-balloon__icon -circle"><img decoding="async" src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="https://kekenta-it-blog.com/wp-content/uploads/2023/11/kekenta-icon-150x150.jpg" alt="" class="lazyload c-balloon__iconImg" width="80px" height="80px"><noscript><img decoding="async" src="https://kekenta-it-blog.com/wp-content/uploads/2023/11/kekenta-icon-150x150.jpg" alt="" class="c-balloon__iconImg" width="80px" height="80px"></noscript><span class="c-balloon__iconName">ケケンタ</span></div><div class="c-balloon__body -speaking -border-on"><div class="c-balloon__text">
<p>それでは最後までご覧いただきありがとうございました！</p>
<span class="c-balloon__shapes"><span class="c-balloon__before"></span><span class="c-balloon__after"></span></span></div></div></div></div><div class="p-blogParts post_content" data-partsID="7607">
<p class="has-text-align-center is-style-balloon_box wp-block-paragraph">SWELLのその他の記事はこちら</p>



<p class="has-text-align-center wp-block-paragraph"><span data-icon="LsSwellLogo" data-id="0" style="--the-icon-svg: url(data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjFlbSIgd2lkdGg9IjFlbSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBhcmlhLWhpZGRlbj0idHJ1ZSIgdmlld0JveD0iMCAwIDQwIDQwIj48Y2lyY2xlIGN4PSI0LjIiIGN5PSIxNS42IiByPSIyLjEiPjwvY2lyY2xlPjxwYXRoIGQ9Ik0zNi43IDExLjZjLTEtMi4yLTMuNy0zLjktNi45LTMuMy0zLjYuNy01LjQgMi45LTcuOCA1LjktMi43IDMuNC01LjUgNy05LjQgOC41LTEuNC42LTIuOC44LTQuMy44LTEuMSAwLTIuMy0uMi0zLjUtLjUtLjctLjItMS41LS41LTIuMy0uOCAyIDcuOSA5LjIgMTMuOCAxNy44IDEzLjYgOS44LS4yIDE3LjctOC4zIDE3LjctMTgtLjEtMi4xLS40LTQuMy0xLjMtNi4yeiI+PC9wYXRoPjwvc3ZnPg==)" aria-hidden="true" class="swl-inline-icon"> </span><a href="https://kekenta-it-blog.com/category/wordpress/swell/">SWELL記事一覧</a></p>



<div class="swell-block-fullWide pc-py-60 sp-py-40 alignfull" style="background-color:#f7f7f7"><div class="swell-block-fullWide__inner l-article">
<h2 class="wp-block-heading is-style-section_ttl my-is-style-section_ttl--none"></h2>



<p class="has-text-align-center wp-block-paragraph"><span class="swl-marker mark_green"><strong><span class="swl-fz u-fz-l">今回ご紹介した内容のテーマ</span></strong></span><span style="--the-icon-svg: url(data:image/svg+xml;base64,PHN2ZyBoZWlnaHQ9IjFlbSIgd2lkdGg9IjFlbSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiBhcmlhLWhpZGRlbj0idHJ1ZSIgdmlld0JveD0iMCAwIDQwIDQwIj48Y2lyY2xlIGN4PSI0LjIiIGN5PSIxNS42IiByPSIyLjEiPjwvY2lyY2xlPjxwYXRoIGQ9Ik0zNi43IDExLjZjLTEtMi4yLTMuNy0zLjktNi45LTMuMy0zLjYuNy01LjQgMi45LTcuOCA1LjktMi43IDMuNC01LjUgNy05LjQgOC41LTEuNC42LTIuOC44LTQuMy44LTEuMSAwLTIuMy0uMi0zLjUtLjUtLjctLjItMS41LS41LTIuMy0uOCAyIDcuOSA5LjIgMTMuOCAxNy44IDEzLjYgOS44LS4yIDE3LjctOC4zIDE3LjctMTgtLjEtMi4xLS40LTQuMy0xLjMtNi4yeiI+PC9wYXRoPjwvc3ZnPg==)" data-icon="LsSwellLogo" data-id="13" aria-hidden="true" class="swl-inline-icon"><span class="swl-fz u-fz-l"> </span></span></p>


<p><div class="p-blogParts post_content" data-partsID="7603">
<div style="text-align: center;">



<a href="//af.moshimo.com/af/c/click?a_id=4500848&p_id=3885&pc_id=9646&pl_id=53838" rel="nofollow" referrerpolicy="no-referrer-when-downgrade"><img decoding="async" src="//image.moshimo.com/af-img/3130/000000053838.jpg" width="420" height="350" style="border:none;"></a><img decoding="async" src="//i.moshimo.com/af/i/impression?a_id=4500848&p_id=3885&pc_id=9646&pl_id=53838" width="1" height="1" style="border:none;">



</div>
</div></p>



<p class="has-text-align-center u-mb-ctrl u-mb-0 wp-block-paragraph"><span class="swl-fz u-fz-s"><strong>＼　19,800円・買い切り　／</strong></span></p>



<div class="swell-block-button -html green_ -size-l is-style-btn_shiny"><a href="//af.moshimo.com/af/c/click?a_id=4500848&amp;p_id=3885&amp;pc_id=9646&amp;pl_id=53798&amp;url=https%3A%2F%2Fswell-theme.com%2F" rel="nofollow" referrerpolicy="no-referrer-when-downgrade" attributionsrc="">今すぐSWELLを購入する　&gt;&gt;</a><img decoding="async" src="//i.moshimo.com/af/i/impression?a_id=4500848&amp;p_id=3885&amp;pc_id=9646&amp;pl_id=53798" width="1" height="1" style="border:none;" alt="" loading="lazy"></div>
</div></div>
</div>]]></content:encoded>
					
					<wfw:commentRss>https://kekenta-it-blog.com/swell-relation-post-shcode/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
