3

I have a loop, and need to pass one more argument through it. I need to get the current page's single post title, and then use that as the value.

I tried going about it like this:

$tagz = the_title();

$args = array( 
    'post_type' => 'product', 
    'posts_per_page' => 5, 
    'product_tag' => "{$tagz}"  );

and the $tagz variable just automatically echo's the current post title. I found out through this resource this resource that the_title() merely echos the title and never returns it, making the $tagz variable null.

How can I use the current posts title as an argument? I am really stumped!!

  • Did you try $tagz = the_title( ''. '', 0 ); ? – birgire May 18 '15 at 22:53
  • Submit that as the answer and I'll mark you correct :D I used $tagz = the_title( ''. '', false ); but it's the same thing, and it worked!!!!!!! I reread the resource I linked, and now I understand it better because of you. I just started learning php last month, thank you so much :D you've made an impact on my life, stranger! – Chris Haugen May 18 '15 at 22:56
  • I'm sure we can find a duplicate question and vote for it and the corresponding answers instead ;-) Glad to hear it worked for you and welcome to the communitiy by the way ;-) – birgire May 18 '15 at 23:01
  • Sounds good :) is there a specific way to search for a duplicate question? :o – Chris Haugen May 18 '15 at 23:04
  • Maybe with is:question the_title return post title or some variants? – birgire May 18 '15 at 23:07
11

You can use get_the_title() to return the current post title in the loop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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