2

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="/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?

7
  • 3
    Both work from inside header.php. Please show your code and why you have a style.php file.
    – kaiser
    Feb 8, 2013 at 10:56
  • 3
    If your running a database query to pass a variable to a style.php file your doing_it_wrong()
    – Chris_O
    Feb 8, 2013 at 11:10
  • I edited my question and added the code. Feb 8, 2013 at 11:51
  • And I have a style php, because my slider works just with CSS and therefore the CSS has to know how much slides there are. Feb 8, 2013 at 11:55
  • 1
    There is almost assuredly a better way to do this. Can you please post the style.php code? Feb 8, 2013 at 12:41

1 Answer 1

0

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="<?php echo get_stylesheet_directory_uri(); ?>/style.php?tcount=<?php echo $count;?>" />

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.