【问题标题】:taxonomy php else if statement not working?分类 php else if 语句不起作用?
【发布时间】:2015-07-21 18:03:07
【问题描述】:

我正在尝试为我创建的自定义分类法创建一个 php else if 语句,但它似乎只显示第一个?我已经回显了变量,它是正确的吗?任何帮助将不胜感激。

<?php
$genre = get_the_term_list( $post->ID, 'genre' );
if ($genre = "Action") {
  echo "Action" ;
} elseif ($genre = "Thriller") {
  echo "Thriller" ;
} elseif ($genre = "Horror") {
  echo "Horror" ;
}
?>

【问题讨论】:

  • 您也可以使用switch 语句,因为您在每个if 条件中都使用$genre

标签: php wordpress if-statement custom-taxonomy


【解决方案1】:

你的条件语句应该是if($variable == "something")

比较运算符有=====!=!==等...see more here

执行$genre = "Action" 就是将“操作”分配给$genre

解决方法是:

$genre = get_the_term_list( $post->ID, 'genre' );
if ($genre == "Action") {
  echo "Action" ;
} elseif ($genre == "Thriller") {
  echo "Thriller" ;
} elseif ($genre == "Horror") {
  echo "Horror" ;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2022-01-16
    • 2014-09-15
    相关资源
    最近更新 更多