-1

I m using this simple function to add text to my wordpress site post !

add_filter( 'afip_new_post_content', 'myprefix_change_afip_post_content', 10, 2 );
/* Grabs the image source for the newly created image and inserts it
* into the new post content along with a one line paragraph. */
function myprefix_change_afip_post_content( $post_content, $attachment_id ) {

$my_uploaded_image = wp_get_attachment_image_src( $attachment_id );
$post_content = '<p>This is my new uploaded image....</p>';
return $post_content;

I'm trying to add the title , the author and the date to

$post_content = '<p>This is my new uploaded image....</p>';

How do i insert wordpress post title , post author and the date to the above text

Like

$post_content = ‘This post – $post_title() ; is uploaded by the_author() on the_time() on category the_category()
3

1 Answer 1

0

As your hook only passes the $post_content and $attachment_id, you'll first need to get the relevant post, and then you can output your required data.

Please try this and comment if it does not meet your requirements.

    function myprefix_change_afip_post_content( $post_content, $attachment_id ) {

            global $post
            $post = get_post( $attachment_id );
            setup_postdata( $post );

            $my_uploaded_image = wp_get_attachment_image_src( $attachment_id );

            $post_content = sprintf(
                    'This post – %1$s is uploaded by %2$s on %3$s on category %4$s',
                    get_the_title(),
                    get_the_author_meta( 'display_name' );
                    get_the_date(),
                    get_the_category()
            );

            wp_reset_postdata()

            return $post_content;

    }

Note - this will overwrite the previous $post_content. If you want to add to it instead simply replace $post_content = sprintf( with $post_content.= sprintf(.

2
  • Thanks David for your help...but its not working .. I hope ..you can fix this ! Many Thanks
    – pradip
    Commented May 2, 2014 at 16:51
  • Somehow i m able to get this working but its not displaying the category and author name ... title and Date its working
    – pradip
    Commented May 2, 2014 at 19:04

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.