• Hi all,

    I have what seems like a simple problem.

    I want to insert the_date() right before the closing </p> in the_content().

    I’ve seen a lot of examples of simple add_filter solutions that will insert text before/after, but none that will insert code between those <p> tags.

    Basically, I want my output to look like this:

    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque porta, felis vel volutpat. 12-March-2011.

    If I just write:
    the_content()
    the_date()

    I obviously get that closing paragraph tag kicking the date to the next line. I’ve tried writing a function using get_the_content(), but of course that sends the content out unformatted, which doesn’t really work either.

    Anyone got any ideas?

    Thanks

    Terry

Viewing 10 replies - 1 through 10 (of 10 total)
  • I think you can use get_the_content(), append the date, and then call apply_filters on the result. This is UNTESTED, but it should be close:

    $date = the_time('j-F-Y');
    $content = get_the_content() . " $date. " ;
    echo apply_filters('the_content',$content);
    Thread Starter Terry Sutton (saltcod)

    (@saltcod)

    Hurm. Sadly that didn’t work. The content just comes out without the date.

    I’ve been trying add_filter instead of apply_filters all along. Is there an easy way to explain the difference.

    Thanks for having a look at this.

    Terry

    I’m sorry, I should have used get_the_time() instead of the_time().

    add_filter registers a filter for a group with WP, to be called along with all the other filters for the same filter group.

    apply_filters actually executes all the filter functions that have been registered.

    Thread Starter Terry Sutton (saltcod)

    (@saltcod)

    Thanks for the apply vs. add explanation. I didn’t even realize there was an apply_filters. Great.

    Sadly again, however, even with get_the_time() the code isn’t doing as it should.

    To be completely clear, I have the following pasted as-is into functions.php:

    $date = get_the_time('j-F-Y');
    $content = get_the_content() . " $date. " ;
    
    echo apply_filters('the_content', $content);

    But still, it doesn’t modify anything.

    Appreciate the help very much,

    Terry

    Thread Starter Terry Sutton (saltcod)

    (@saltcod)

    Some of the code does work, however.

    If I put this in instead:

    $test_string = "Test string to see if the filter works";
    $content = get_the_content() . $test_string ;
    
    echo apply_filters('the_content', $content);

    It prints the test string right at the top of the page.

    Thread Starter Terry Sutton (saltcod)

    (@saltcod)

    A ha! Interestingly, when I wrap it in php tags and put it in sidebar.php it finally works. Breakthrough!

    More experimentation and I’ll post back proper results.

    Thanks.

    The code I gave you should be a replacement for calling the_content() in the template used to display the page, not a function to go in functions.php.

    Thread Starter Terry Sutton (saltcod)

    (@saltcod)

    Thanks a million @vtxyzzy. Works perfectly now. This also gave me a great opportunity to learn about filters! So thanks for that too.

    As if I haven’t asked enough questions today, if you know how I might turn the date into a clickable permalink, that would be A++!

    I’ve tried a few versions of this, but none have worked so far.

    <?php
      $date = get_the_time('d-m-y');
      $content = get_the_content() . "<a href="<?php the_permalink(); ?>">$date</a>" ;
    
      echo apply_filters('the_content',$content);
    ?>

    I’d hoped that by just putting the permalink in before the date and turning it all into a link it would work….but no avail.

    If you have any ideas, I’d love to hear them, otherwise, thanks a whole lot for helping with this today.

    Terry

    Thread Starter Terry Sutton (saltcod)

    (@saltcod)

    I suspect this is another one of those easily explainable things.

    If I try just a plain-text string in before $date it works fine. Like so:

    <?php
      $date = get_the_time('d-m-y');
      $content = get_the_content() . "testing" . $date;
    
      echo apply_filters('the_content',$content);
    ?>

    But trying to put a link in place of the string “testing” doesn’t work:

    <?php
      $date = get_the_time('d-m-y');
      $content = get_the_content() . "<a href="#">test link</a>" . $date;
    
      echo apply_filters('the_content',$content);
    ?>

    So there must be something funny about not being able to put hypertext in there?

    My guess is that the filters are stripping out the link. There may be a better way to do this, but give this a try:

    <?php
      $date = get_the_time('d-m-y');
      $content = get_the_content() . '$$$';
      $content = apply_filters('the_content',$content);
      $content =  str_replace('$$$'," <a href='#'>test link</a> " . $date);
      echo $content;
    ?>
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Add code to the_content()’ is closed to new replies.