php - 有条件的 PHP

标签 php html css

如您所见网页www.evolvedtools.org/Genr8PageIntro.php我从我的 wordpress.org 博客中获取博客,并使用一些简单的 php 标记将它们显示在此页面上: 在页眉中:

<?php 
require('./blog/wordpress/wp-blog-header.php');
?>

...在页面正文中:

<div id="PHPBlog">
<?php 
global $post;
$args = array( 'posts_per_page' => 8 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a><br >
<?php endforeach; ?>
<p>Feeds - Personal Blog - Master Thesis</p>
</div>

问题是我只希望在您处于全屏桌面时显示博文。我已经针对不同的环境进行了媒体查询。 除了在这些条件下,我不想显示数据库内容:

<link href="TextPstyle.css" rel="stylesheet" type="text/css" media="only screen and (min-width: 41em)" id="stylesheet-TextP">

我对 PHP 知之甚少,所以我才刚刚开始研究这些东西。如果有人能就此向我提出建议,我将非常高兴:-)

最佳答案

您可以使用 $_SERVER['HTTP_USER_AGENT'] 并在其中搜索桌面操作系统,然后有条件地执行一些代码:

function is_desktop() {
    $agent = $_SERVER['HTTP_USER_AGENT']
    // Code here to check for desktop
}

if (is_desktop()) {
    // Show conditional content
} else {
    // Otherwise
}

之所以在服务器端使用 PHP 而不是在客户端使用 CSS,是因为对于非桌面用户,数据根本不应该加载(不加载然后隐藏)。

编辑

当然,重用别人的代码是个好主意。我用过 Mobile_Detect之前你可以做这个检查:

include 'Mobile_Detect.php';
$detect = new Mobile_Detect;
if ($detect->isMobile() || $detect->isTablet()) {
    // Handle mobile version
} else {
    // Handle desktop version
}

关于php - 有条件的 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17361185/

相关文章:

php - 用于删除整个目录中起始字符和结束字符之间的所有文本的 Shell 脚本或命令

javascript - 如何将选择复选框值动态传递给ajax调用变量动态发布到php变量

javascript - 基于自动图像检索系统生成自动链接(Freewall.js - 网格布局)

jquery - 如何使用 z-index 在前面显示 ui 时间选择器?

ios - 媒体查询不适用于 iPhone 系列

php - 执行另一个 PHP 文件而不等待它完成执行

php - 如何处理 XML 中 URL 中的特殊字符

html - p :layoutUnit ignore the margin-top

javascript - 同步加载 HTML 内容

javascript - Angular 为 2 的三态复选框