Add Image Before Posts Entry Title

This code adds the image before all titles on the page.

How do i add one single image before the posts entry title?

function adt_abovetitle($title){

    //Return new title if called inside loop
    if ( in_the_loop() ) {

    $x = do_shortcode('[shortcode id="'. $id . '"]');
        return  $x . $title;    

    } else {
    //Else return regular   
    return $title;
    }
}

add_filter( 'the_title', 'adt_abovetitle');

Topic title the-content posts Wordpress

Category Web


The in_the_loop() and is_singular() functions are too heavy and slow down the page load. You just need to edit single.php file in your theme root folder. The code will look like this:

if (has_post_thumbnail()) {
    the_post_thumbnail('full');
}
<h1 class="entry-title"><?php the_title(); ?></h1>

This works fine for me when i test it. I did edit your code as you had a small error in the function name not matching.

Also, you might want to add a conditional tag and wrap the shortcode in a div but otherwise it works.

function adt_abovetitle($title){

    //Return new title if called inside loop
     if ( in_the_loop() && is_singular('post') ) {

    $x = do_shortcode('[shortcode id="'. $id . '"]');
        return  $x . $title;    

    } else {
    //Else return regular   
    return $title;
    }
}

add_filter( 'the_title', 'adt_abovetitle');

You might want to test your else statement as well.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.