有没有办法从头部检查当前页面是否为主页?
我的style.php
需要滑块中的帖子数量,但仅在主页上。所以我这样计算我的帖子:
<?php
$meta_key = \'teaser\';
$posts_per_page = 6;
$sql = "SELECT count(DISTINCT pm.post_id)
FROM $wpdb->postmeta pm
JOIN $wpdb->posts p ON (p.ID = pm.post_id)
WHERE pm.meta_key = \'$meta_key\'
AND pm.meta_value != \' \'
AND (p.post_type = \'post\' OR p.post_type = \'page\' OR p.post_type = \'ai1ec_event\')
AND p.post_status = \'publish\'";
$count = $wpdb->get_var($sql);
if($count > $posts_per_page) {
$count = $posts_per_page;
}
?>
<link rel="stylesheet" type="text/css" media="all" href="/wordpress/wp-content/themes/roots/style.php?tcount=<?php echo $count;?>" />
我试着用
if
声明,但
is_home()
或
is_frontpage()
不要工作。
这就是风格。php(只是一些相关部分,以展示其工作原理):
header("Content-Type: text/css");
$seconds_to_cache = 86400;
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
$numTeasers = $_GET[\'tcount\'];
/* ==========================================================================
Slider Controls
========================================================================== */
$max = $numTeasers;
for ($s = 1; $s <= $max; $s++) {
$next = $s+1;
if($next == $max+1){$next = 1;}
echo "#wdgt_slider #slide".$s.":checked ~ #controls label:nth-child(".$next."){background: url(\'/wordpress/wp-content/themes/roots/assets/img/slider/next.png\') no-repeat scroll 0 0 transparent;display:block;position:absolute;right:0;margin:0 20px 0 0;}";
$prev = $s-1;
if($prev == 0){$prev = $max;}
echo "#wdgt_slider #slide".$s.":checked ~ #controls label:nth-child(".$prev."){background: url(\'/wordpress/wp-content/themes/roots/assets/img/slider/prev.png\') no-repeat scroll 0 0 transparent;display:block;position:absolute;float:left;margin: 0 0 0 20px;}";
echo "#wdgt_slider #slide".$s.":checked ~ #active label:nth-child(".$s."){color:#c2b29e}";
echo "#wdgt_slider #slide".$s.":checked ~ #slides #slide-teaser-".$s." .info, #wdgt_slider #slide".$s.":checked ~ #slides #slide-teaser-".$s."{visibility:visible;width:100%;height:auto;opacity:1;}";
}
@import url(“../adventon/style.css”);
在HTML头部做这样的事情是不是很糟糕?
最合适的回答,由SO网友:WP Themes 整理而成
二者都is_home()
和is_front_page()
工作在标题或任何你使用主题模板的地方。
问题很可能是因为您硬编码了以下内容:/wordpress/wp-content/themes/roots/style.php
请尝试创建如下url:
<link rel="stylesheet" type="text/css" media="all" href="<?php echo get_stylesheet_directory_uri(); ?>/style.php?tcount=<?php echo $count;?>" />