• Resolved goxu

    (@goxu)


    Hi,

    How do I retrieve the post title when parsing the content with add_filter(‘the_content’, ‘myfunction’, 10);.

    I’m making a plugin which embeds links in the bottom of the post based on the terms used in the title and post content.

    So in this case, “myfunction” should recieve the title and the content at the same time. Is is possible to do? Do I need to make a fuction to save the title and need to call it from the myfunction?

    If you know the way, it would be greatly appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • For working with the post title you typically use the API hook ‘the_title’ — but if you’re testing on it to achieve an action on the post’s content, you may be better off doing something like scoping the $post object to global in your function (it should be hanging around considering you’re hooking into ‘the_content’):

    global $post;

    From here on the post title is available through $post->post_title

    Thread Starter goxu

    (@goxu)

    Thanks, Kafkaesqui.

    I really appreciate it.

    Thread Starter goxu

    (@goxu)

    I have been working on it and found that another way of doing this.

    global $wp_query;
    $post_title = $wp_query->post->post_title;

    What is the difference? Which one is faster?

    I also wonder how the program knows which post to retrieve. I guess this only work in single pages, wp-pages. Is it correct?

    What is the difference? Which one is faster?

    There is really no difference as the $post object is assigned through the WP_Query class. I’ve used both ways.

    As far as I know there’s no variation on speed between the two; they are both made ‘available’ on single posts and Pages.

    I guess this only work in single pages, wp-pages. Is it correct?

    Yes and no. They are WordPress objects classified during the post query, and there for use by both internal and external (plugin) functions. They do exist on multiple query (posts) pages, but will hold only the *latest* post on a page.

    Thread Starter goxu

    (@goxu)

    Thank you so much Kafkaesqui, for answering the questions. It helps a lot!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Retrieving the Title and Post Content with add_filter’ is closed to new replies.