2

I would like to add some text or image at the start of each post (in a given post layout).

I have found numerous ways to implement this before the content starts ie atop the content (of course, I could just do this by adding the text above <?php the_content(); ?>).

But I want to insert it really at the start of the content--on the same line, like:

HERE IS SOME CUSTOM TEXT and here starts the content entered for the post.

Any ideas, Community?

5
  • How does your PHP code look like? What's the generated HTML and what's the desired HTML?
    – birgire
    Commented May 24, 2015 at 21:18
  • In my single page php layout, I call with <?php the_content(); ?> I'm not sure if this calls from the content.php or content-single.php but I can paste either one in here if you let me know that. I also don't really understand what you would like for "desired HTML?" Sorry, I've been learning code myself for the last month and don't know the communicative terms. Commented May 24, 2015 at 21:23
  • I'm just wondering what kind of HTML is coming from your the_content();. For example if you check the HTML source of your page the relevant HTML might be like: <article><h1>Hello World</h1><div class="entry"><p>This is my first post</p></div></article> but your desired output might be: <article><h1>Hello World</h1><div class="entry"><p>HERE IS SOME CUSTOM TEXT This is my first post</p></div></article>. This kind of information will help. Please update your question with any extra information, thanks.
    – birgire
    Commented May 24, 2015 at 21:44
  • Oh, ok. I have "<article class="post page"> ... containers with widgets ... <div class="center-column"> <h2>Title</h2> ... div with widget ... <p>Text of article</p></div> Commented May 24, 2015 at 21:50
  • So it's just a single <p> tag, that's preventing you from adding the extra words above the line containing <?php the_content();?>?
    – birgire
    Commented May 24, 2015 at 21:53

1 Answer 1

3

You can try the following:

! is_admin() && add_filter( 'the_content', function( $content )
{
    if( in_the_loop() )   // <-- Target the main loop
    {
        $prepend = 'HERE IS SOME CUSTOM TEXT'; // <-- Edit your text here
        $content = $prepend . $content;
    } 
    return $content;
}, 9 ); // <-- Choose some priority < 10

where we choose the priority before the content is taken through wpautop.

6
  • Yes, that absolutely worked. People, please upvote this answer. I don't have the reputation points. Thank you, birgire Commented May 24, 2015 at 22:10
  • Glad to hear it worked for you. I updated it to work only for the main loop. @JustinMunce
    – birgire
    Commented May 24, 2015 at 22:16
  • Is there a way to understand this process more deeply? I find myself again needing to insert something (and I think it would require the same process). In this case, I would like to have, under the headline, a textwidget (about 10 char wide) beside a string of meta information). To read: "WIDGET Some custom meta text"... I've tried many different options with divs and IDs, etc., Commented May 25, 2015 at 5:32
  • but this creates formatting issues with both resizing and longer meta display. Can I use the same code you gave earlier to insert something into either the textwidget or the meta so they can display as one string (of text), wrapping at the end of the line back to the beginning)? Commented May 25, 2015 at 5:36
  • So you mean how to inject text into the widget text or widget title? Since the original quesiton is regarding the post and not widget, I think it would be better served as a new question, with all relevant information (like HTML structure and PHP code etc) @JustinMunce
    – birgire
    Commented May 25, 2015 at 12:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.