webサイト制作の勉強|2023年5月クラス用ブログ

ファリカテクニカルアカデミーのwebサイト制作科の授業解説ブログです。フェリカテクニカルアカデミーは池袋にある求職者支援訓練の学校です。

wordpressで特定のカテゴリー記事を表示させる

wordpressで特定のカテゴリー記事を表示させる為には「 get_posts( );」を使って表示させます。カテゴリーに指定したスラッグを使う事で特定のカテゴリー記事のみを表示する事が出来ます。また、「foreach」を使う事で配列に入っている値を全て出力させます。
以前は「query_posts」で出力するケースもありましたが、現在は非推奨なので注意が必要です。


「get_posts」で出力

出力する記事のフォーマット指定
<?php
$arg = array(
'posts_per_page' => 4, // 表示する件数
'orderby' => 'date', // 日付でソート
'order' => 'DESC', // DESCで最新から表示、ASCで最古から表示
'category_name' => 'news', // 表示したいカテゴリーのスラッグを指定
//'tag' => 'post'//表示したいタグをスラッグ指定
);
$posts = get_posts( $arg );
if( $posts ): ?>

「foreach」で条件に当てはまる記事を表示

<!-- ループ開始 -->
<?php
foreach ( $posts as $post ) :
setup_postdata( $post );
?>
<dt><?php the_time('Y年m月d日'); ?></dt>
<dd>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</dd>
<?php endforeach; ?>
<!-- ループ終了 -->


<?php
/// 必ずクエリをリセット
  endif;
  wp_reset_postdata();
?>

9月28日の作業データ(ブログ)

index.php

<?php
/* Template Name:ブログページ */
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TRAVEL SKETCH</title>
<link rel="stylesheet" href="<?php echo get_stylesheet_uri(); ?>">
</head>
<body>
<header>
<h1>TRAVEL SKETCH</h1>
</header>
<div class="wrapper">
<main>
<div class="post-wrapper">
<h2><a href="#">投稿のタイトル</a></h2>
<p class="cate-name"><a href="#">カテゴリー名</a></p>
<p class="post-text">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Ipsa vero quod architecto at corrupti.</p>
<p class="date">2023年09月28日</p>
</div><!-- /.post-wrapper -->

<div class="post-wrapper">
<h2><a href="#">投稿のタイトル</a></h2>
<p class="cate-name"><a href="#">カテゴリー名</a></p>
<p class="post-text">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Ipsa vero quod architecto at corrupti.</p>
<p class="date">2023年09月28日</p>
</div><!-- /.post-wrapper -->

<div class="post-wrapper">
<h2><a href="#">投稿のタイトル</a></h2>
<p class="cate-name"><a href="#">カテゴリー名</a></p>
<p class="post-text">Lorem ipsum dolor sit, amet consectetur adipisicing elit. Ipsa vero quod architecto at corrupti.</p>
<p class="date">2023年09月28日</p>
</div><!-- /.post-wrapper -->
</main>
<aside></aside>
</div><!-- /.wrapper -->
<footer>

</footer>
</body>
</html>

style.css

@charset "UTF-8";
/*
Theme Name:ブログ
Aother:yachin29
Description:投稿機能を色々と使ってみるテーマ
*/
html{box-sizing:border-box;-webkit-text-size-adjust:100%}*,:after,:before{background-repeat:no-repeat;box-sizing:inherit}:after,:before{text-decoration:inherit;vertical-align:inherit}*{padding:0;margin:0;box-sizing:border-box;}audio:not([controls]){display:none;height:0}hr{overflow:visible}article,aside,details,figcaption,figure,footer,header,main,menu,nav,section,summary{display:block}summary{display:list-item}small{font-size:80%}[hidden],template{display:none}abbr[title]{border-bottom:1px dotted;text-decoration:none}a{background-color:transparent;-webkit-text-decoration-skip:objects}a:active,a:hover{outline-width:0}code,kbd,pre,samp{font-family:monospace,monospace}b,strong{font-weight:bolder}dfn{font-style:italic}mark{background-color:#ff0;color:#000}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sub{bottom:-.25em}sup{top:-.5em}input{border-radius:0}[role=button],[type=button],[type=reset],[type=submit],button{cursor:pointer}[disabled]{cursor:default}[type=number]{width:auto}[type=search][type=search]::-webkit-search-cancel-button,[type=search]::-webkit-search-decoration{-webkit-appearance:none}textarea{overflow:auto;resize:vertical}button,input,optgroup,select,textarea{font:inherit}optgroup{font-weight:700}button{overflow:visible}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button::-moz-focus-inner{border-style:0;padding:0}[type=button]::-moz-focus-inner,[type=reset]::-moz-focus-inner,[type=submit]::-moz-focus-inner,button:-moz-focusring{outline:1px dotted}[type=reset],[type=submit],button,html [type=button]button,select{text-transform:none}button,input,select,textarea{background-color:transparent;border-style:none;color:inherit}select::-ms-expand{display:none}select::-ms-value{color:currentColor}legend{border:0;color:inherit;display:table;max-width:100%;white-space:normal}::-webkit-file-upload-button{-webkit-appearance:button;font:inherit}[type=search]img{border-style:none;vertical-align: bottom}progress{vertical-align:baseline}svg:not(:root){overflow:hidden}audio,canvas,progress,video{display:inline-block}@media screen{[hidden~=screen]{display:inherit}[hidden~=screen]:not(:active):not(:focus):not(:target){position:absolute!important;clip:rect(0 0 0 0)!important}}[aria-busy=true]{cursor:progress}[aria-controls]{cursor:pointer}[aria-disabled]{cursor:default}::-moz-selection{background-color:#b3d4fc;color:#000;text-shadow:none}::selection{background-color:#b3d4fc;color:#000;text-shadow:none}ul,ol{list-style:none;}a{text-decoration:none;}img{max-width: 100%;vertical-align: bottom;}

header{
width: 100%;
background-color: beige;
}
h1{
text-align: center;
padding: 30px 0;
}
.wrapper{
width: 90%;
margin: 140px auto;
display: flex;
justify-content: space-between;
}
main{
width: 65%;
padding: 10px;
}
aside{
width: 28%;
padding: 10px;
height: 1000px;
background-color: crimson;
}
.post-wrapper{
margin-bottom: 100px;
}
.post-wrapper>h2{
font-size: 32px;
margin-bottom: 20px;
}
.post-wrapper>h2>a{
color: #222;
}
.cate-name>a{
color: #1b3c91;
text-decoration: underline;
}
.post-text{
margin: 20px 0;
}
.date{
text-align: right;
}

wordpressに記事データをインポートする

コンテンツのインポート

まずはサンプルデータをダウンロードしましょう。
www.shoeisha.co.jp





もちろん自分で投稿記事などを入力する事も可能ですが、ここではサンプルデータをダッシュボードからインポートします。
「ツール」→「インポート」→「WordPress


ダウンロードした「posts.xml」ファイルを選んでインポートしましょう。


ユーザー名を選択して「Submit」ボタンを押します(画像などの関連ファイルはないので、Attachmentsのチェックは不要)


ダッシュボードの「投稿」から「投稿一覧」を選び、投稿データがインポートされているか、必ず確認しましょう。



投稿記事のサンプルデータ
yachin29.jp





インポートした記事をループで呼び出す

<div class="post-wrapper">
<?php if(have_posts()): ?>
<?php while (have_posts()): ?>
<?php the_post(); ?>

<!-- ここから投稿 -->
<a href="<?php the_permalink(); ?>">
<div class="post-box">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<p class="date"><?php the_time('Y年n月j日'); ?></p>
</div><!-- /.post-box -->
</a>
<!-- ここまで投稿 -->

<?php endwhile; //投稿ループ終了 ?>
<!-- ページネーション開始 -->
<?php the_posts_pagination(); ?>
<!-- ページネーション終了 -->
<?php else: ?>
<?php endif; //条件分岐終了 ?>
</div><!-- /.post-wrapper -->

time関数とstrtotime関数

UNIXタイムスタンプとは

UNIXタイムスタンプとは、コンピューターのシステム時刻の一種で、協定世界時UTC)での1970年1月1日(午前0時0分0秒)からの経過時間を指します。

time関数

現在のタイムスタンプを取得

<?php
$now = time();//基準時間から現在までを秒数換算した値

$min = 60;
$hour = 60 * $min;
$day = 24 * $hour;


$now = $now / $day;
$now = floor($now);//小数点以下を切り捨て
echo "基準時間から今日まで{$now}日";
?>

strtotime関数

指定した日時のタイムスタンプを取得

<?php
$t = strtotime('2024-01-1');
echo $t;
?>

指定日までのカウントダウン

現在のタイムスタンプと指定した日時のタイムスタンプを使って、指定日までのカウントダウンを作ってみましょう。
1秒毎にファイルを更新させる方法は色々ありますが一番簡単なrefreshを使ってみましょう。

<meta http-equiv="refresh" content="秒数">

PHPでの日付や時間取得

基本的にはJavascriptと同じですが、PHP独自のフォーマットをしっかりと覚えましょう。


1、「どのような形式で」を指定できる関数:date関数(フォーマット文字列)
2、「いつの日付を」を指定できる関数:mktime関数(タイムスタンプ)

timezoneの設定

本来は「php.ini」ファイル上で設定しますが、設定していない場合は、phpファイルに直接、以下のように記述します。

<?php
date_default_timezone_set ( 'Asia/Tokyo' );
echo '現在は ' . date ( 'G時 i分 s秒' ) . ' です';
現在の日付を取得
echo date('Y年m月d日');
現在の時間を取得
echo date('H時i分s秒');
echo date('Y年m月d日 H時i分s秒');

様々なフォーマットでの表示例


echo date('Y-m-d');
echo date('Y/n/d');
echo date('Y年');
echo date('n月');
echo date('M d, Y');
echo date('F d, Y');
echo date('l F d, Y');
echo date('g:i A');
echo date('G:i');

曜日を日本語で表示

Javascriptと同様、曜日の日本語表記は用意されていないので、配列を使って日本語の曜日を表示させてみましょう。

<?php 
$week = ['日', '月', '火', '水', '木', '金', '土'];

// date関数
$w = date('w');

echo $week[$w];
?>


上記のフォーマット一覧表にもあるように('w')で曜日を数字で取得します。
日曜0 → 土曜6
これを応用して、以下のように曜日を日本語表記させます。
日曜=0~土曜=6 

  1. $weekに日〜土の値を順番に格納した配列を用意する
  2. 変数$wに今日の曜日の番号を格納する(月曜は1)
  3. 変数$weekは0から始まるキーが連番で振られているので、$week[$w]とすると$week[1]と同等となる
  4. $weekの[1]は月曜なので、月と表示される