首页 > 解决方案 > 如何编写 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 } ?>

标签: phpwordpress

解决方案


您需要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。


推荐阅读