我想在每个帖子中输出一个特定的链接,具体取决于帖子的类别。例如,如果帖子属于“旅行”类别,则指向https://www.example.com/travelbooking的链接将显示在该帖子中。如果帖子属于“酒店”类别,则指向https://www.example.com/hotelbooking的链接 将显示在该帖子中。
我试过这段代码
<?php
$category = get_the_category();
$firstCategory = '$category[0]->cat_name';
if (strpos($firstCategory, 'travel') !== false) {
echo '<a href="https://www.example.com/travelbooking">Visit travelbooking</a>';
}
if (strpos($firstCategory, 'hotel') !== false) {
echo '<a href="https://www.example.com/hotelbooking">Visit hotelbooking</a>';
}
else {
echo '<a href="https://www.example.com">Visit homepage</a>';
}
?>
但它不起作用,有什么建议吗?谢谢!