アプリ版:「スタンプのみでお礼する」機能のリリースについて

下記のコードでアーカイブを実装したいのですが、エラーが表示されており、調べても構文の間違えが分かりませんでした。構文でおかしな部分はありますでしょうか?

//エラー箇所
if ($the_year === $this_year): // 今年だったら
$out .= '<li class="year acv_open current">' . $the_year;
$out .= '<ul class="month-archive-list">';
else: // それ以外の年の場合
$out .= '<li class="year">' . $the_year;
$out .= '<ul class="month-archive-list">';
endif;


//エラー文
syntax error, unexpected '$out' (T_VARIABLE)


<div class="widget">
<h4 class="monthly-archive-title">月別アーカイブ</h4>

<?php
// 月別アーカイブリストを取得する
$monthly_archives = wp_get_archives(
$args = array(
'type' => 'monthly',
'show_post_count' => true,
'before' => '',
'after' => ',',
'echo' => 0,
));
$monthly_archives = explode(',', $monthly_archives); //配列化
array_pop($monthly_archives); //末尾の空白要素を削除

// 年別アーカイブリストを取得する
$yearly_archives = wp_get_archives(
$args = array(
'type' => 'yearly',
'format' => 'custom',
'before' => '',
'after' => ',',
'echo' => 0,
));
$yearly_archives = explode(',', $yearly_archives); //配列化
array_pop($yearly_archives); //末尾の空白要素を削除

$this_year = (string)idate('Y'); //現在の年を、4桁の文字列で取得

// HTMLとして出力するコードの記述部分
// ここから
$out = '<ul class="archive-list">';

foreach ($yearly_archives as $year) {
$the_year = substr($year,-8,4); // 「年」を表す部分のみ抽出する

if ($the_year === $this_year): // 今年だったら
$out .= '<li class="year acv_open current">' . $the_year;
$out .= '<ul class="month-archive-list">';
else: // それ以外の年の場合
$out .= '<li class="year">' . $the_year;
$out .= '<ul class="month-archive-list">';
endif;

foreach ($monthly_archives as $month) {
//月毎アーカイブの文字列中に、ターゲットとなる年が含まれているか
$pos = strpos($month, $the_year);

// 含まれている限り、li要素を出力
if ($pos !== false): // 該当する文字列が含まれているときは、その位置が返ってくるので、!==falseという判定文を使用
$out .= $month;
endif;
}
$out .= '</ul>'; // 閉じる <ul class="month-archive-list">
}
$out .= '</li>'; // 閉じる <li class="year">
$out .= '</ul>'; // 閉じる <ul class="archive-list">
// ここまで

// HTMLの出力
echo $out;
?>
</div>

A 回答 (5件)

https://www.php.net/manual/ja/control-structures …

コロンを使った構文はこれで正しいと思います。

> syntax error, unexpected '$out' (T_VARIABLE)
$outという変数が、本来あるべきではない箇所に書いてある、というエラーです。
こういう場合
・$outがあるのが間違い
・$outの周辺に間違いが原因で、 $out が間違いかのようにエラーになっている
の2つが考えられます。



質問のスクリプトをコピペしたところ、
if ($the_year === $this_year): // 今年だったら

: //
ここが「全角スペース」になっています。
元のファイルもそうなってませんか?

コードを書くときは、(色が変わる、□で書かれるなどの)空白が見えるようなエディタを使うことをお勧めします。
    • good
    • 3
この回答へのお礼

全角スペースが原因でした。ありがとうございます。

お礼日時:2022/07/12 01:21

マニュアルに


「同じブロック内で別の構文を混ぜて使うことはできません。」
と書かれているから、それに該当するのでは?
https://www.php.net/manual/ja/control-structures …
    • good
    • 2
この回答へのお礼

回答ありがとうございます。

お礼日時:2022/07/12 01:22

if then elseの構文は以下



if (xxxxxxxx) { // もし〇〇なら
真の場合の処理を記述
} else { // もし〇〇でなければ
偽の場合の処理を記述
}
    • good
    • 1
この回答へのお礼

回答ありがとうございます。

お礼日時:2022/07/12 01:21

すみません


旧式PHPしか知りませんでした
var_dump($変数)で所々止めて確認するしかないですね
    • good
    • 1
この回答へのお礼

再度考えてみます。回答ありがとうございます。

お礼日時:2022/07/11 18:05

if ($the_year === $this_year){ // 今年だったら


$out .= '<li class="year acv_open current">' . $the_year;
$out .= '<ul class="month-archive-list">';
}else{ // それ以外の年の場合
$out .= '<li class="year">' . $the_year;
$out .= '<ul class="month-archive-list">';
}
    • good
    • 1

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!