admin管理员组

文章数量:1122832

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');

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');
Share Improve this question edited Feb 4, 2015 at 16:29 Brad Dalton 6,9672 gold badges36 silver badges47 bronze badges asked Feb 4, 2015 at 15:42 AriAri 1,1971 gold badge17 silver badges28 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

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.

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>

本文标签: Add Image Before Posts Entry Title