如您在网页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/

10-11 14:21
查看更多