Search code examples
phpcssposts

Edit post pages with PHP and CSS in Wordpress?


I would edit the pages of posts (eg: http://example.com/author/wordpress_post) with PHP and CSS. How can I do it? Is there already a default PHP file for posts that I can modify?


Solution

  • You may need to read up on WordPress' Template Hierarchy

    Posts, by default, use the single.php file, located in the theme directory. Note if you have a Parent Theme and an active Child Theme, your child theme may not have a single.php file. You can, however, copy the single.php file from the parent to the child and modify it from there, knowing that if the parent theme structure changes, your child theme may need to accommodate.

    If your theme uses a different structure, you can always modify the single post functionality with the is_single() function in your functions.php file. If you just want to edit post titles, you could put this code in functions.php

    function my_title_filter( $title, $post_id = null ) {
        if( is_single() ){
            // Only modifies Post titles, no other titles.
            $title = 'Add This Before Post Titles '. $title;   
        }
        return $title;
    }
    add_filter( 'the_title', 'my_title_filter' );
    

    For CSS, just, well, use CSS. Open up style.css or the customizer and go to "Additional CSS" and make the changes you want. On single posts, the body (should) have the class single-post so you can filter out CSS changes only for posts by prefixing your selectors with that.

    /* Make paragraphs a light blue, but only on single posts */
    .single-post p {
        color: #0095EE;
    }