is_home() in HTML head

Is there a way to check if the current page is the home page from within the head?

My style.php needs the number of posts in the slider, but only on the home page. So I count my posts like this:

<?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="https://wordpress.stackexchange.com/wordpress/wp-content/themes/roots/style.php?tcount=<?php echo $count;?>" />

I tried to wrap it in an if statement, but is_home() or is_frontpage() do not work.

This is the style.php (Just some relevant parts, to show how it works):

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”);

Is it bad practice to do such things in the HTML head?

1 Answer
1

both is_home() and is_front_page() works in the header or anywhwere you are using a theme template.

Most likely, the problem is because you’ve hardcoded this: /wordpress/wp-content/themes/roots/style.php

Try instead creating your url like this:

<link rel="stylesheet" type="text/css" media="all" href="https://wordpress.stackexchange.com/questions/85053/<?php echo get_stylesheet_directory_uri(); ?>/style.php?tcount=<?php echo $count;?>" />

Leave a Comment