Question

I have a search results page with a template call content-search.php.

Basically what this file does is to display the results after a person searches something on the search bar, and i would like to edit the css of this file.

So what I attempted was , i went to functions.php and added the usual code like this

functions.php

function content_search_styles() {
    if ( is_page_template( 'content-search.php' ) ) {
        wp_enqueue_style( 'page-template', get_stylesheet_directory_uri(). '/css/content-search.css' );
    }
}
add_action( 'wp_enqueue_scripts', 'content_search_styles' );

content-search.php

<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?>>
    <h2>Search Results</h2>
    <b>Results as following</b>
    <?php the_title('<h1 class="entry-title">','</h1>' ); ?>

    <?php if( has_post_thumbnail() ): ?>
        <div class="pull-left"><?php the_post_thumbnail('thumbnail'); ?></div>
    <?php endif; ?>

    <small><?php the_category(' '); ?> || <?php the_tags(); ?> || <?php edit_post_link(); ?></small>

    <?php the_excerpt(); ?>

    <hr>

</article>

content-search.css

body {
    display:none !important;
}

The CSS does not reflect on the search results page,

Please advise and thank you very much once again

Was it helpful?

Solution

I bet that you’re misunderstood what is_page_template is really doing. From Codex:

This template tag allows you to determine if you are in a page template. You can optionally provide a template name or array of template names and then the check will be specific to that template.

And if you’ll take a look at it’s code:

function is_page_template( $template = '' ) {
    if ( ! is_singular() ) {
        return false;
    }
 
    $page_template = get_page_template_slug( get_queried_object_id() );
    ...

... you’ll get, that you can’t use this function to target search results. It won’t work, because it’s meant to check if current page uses given page template. On the other hand you want to check if given file is currently used...

If your theme is coded correctly, then you should use is_search() instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top