php - 如何编写 if 语句以使用 PHP 在网页上显示某些过滤器和文本?
问题描述
我正在尝试在 wordpress 网站的一个特定页面上显示某些文本并应用过滤器。我正在使用下面的代码,但它不起作用。具体来说,if 语句似乎没有正确捕获网页并传递结果代码。有关如何解决此问题的任何提示?
我要应用此代码的 wordpress 站点中的页面称为“预测续订”。它目前被列为私有,而不是公共。我想澄清一下,以防这是问题的一部分。
<?php
global $user_select_name;
global $_POST;
?>
<?php if ($_POST == 'Predicting Renewals') { ?>
<script>
$('#current-view').html('<center><h4 style="margin-bottom: 0px; margin-top: 0px;"><?php echo $user_select_name ?> Chapter</h4></center> <hr>');
</script>
<?php } ?>
解决方案
您需要is_page()
使用 slug 或页面 ID 进行检查。
<?php global $user_select_name; ?>
<?php if (is_page('predicting-renewals') ) { ?>
<script>
$('#current-view').html('<center><h4 style="margin-bottom: 0px; margin-top: 0px;"><?php echo $user_select_name ?> Chapter</h4></center> <hr>');
</script>
<?php } ?>
https://developer.wordpress.org/reference/functions/is_page/的文档is_page()
如果这不是页面,而是普通帖子,您可以使用is_single()
并传递相同的 slug 或 id。
推荐阅读
- vba - 在一组复数中找到一个实数
- c# - 在 Web API 中返回 JSON
- haskell - 通过镜头折叠或连接 Aeson 阵列
- android - 如何在 Android 中实现 Livedata 和 Databinding?
- amazon-web-services - 如何将数据从 AWS s3 导入 dynamoDB?
- r - 根据存储在 .txt 文件中的列表复制文件
- swift - 为什么 compactMap 返回 nil 结果?
- python - 内部函数背后的逻辑
- ios - 如何使用 YUV 颜色空间将 UIImage 转换为 CVPixelBufferRef?
- c++ - StaticJsonBuffer' 未在此范围内声明,使用 Feather Huzzah (ESP8266) board using MSFT IOT Azure tutorial