templates - Edit css for search results page

admin2025-06-03  3

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

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

Share Improve this question edited Feb 13, 2019 at 5:00 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 12, 2019 at 16:30 deshdesh 1372 silver badges10 bronze badges 1
  • Is the content-search.php file placed directly in the root of the theme? if not you need to specify the relative path to the root of the theme – Fabrizio Mele Commented Feb 12, 2019 at 16:39
Add a comment  | 

1 Answer 1

Reset to default -1

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748913316a314763.html

最新回复(0)