0

I'm trying to create and if() statement that works in accordance to the kind of post (i.e., page or article) and, if page, the title of such page.

The type of post can be obtained from its class function post_class() and the title from its title function the_title().

So I know where I can get the info I need but, then, no matter what, I cannot turn this info into a string I can test. Wherever I put any of these two functions I get an output onto the page.

First, I tried:

if(strpos(post_class(), 'page')) {
    //DO SOMETHING
}

Didn't work. Just had the post_class() dumped onto the page.

Then, I tried calling the function as the value of a variable:

$this_class = post_class();

And had the same result.

I've since tried a couple of other dirtier ways of doing it but to no avail. It seems wherever these WP functions are placed, they will dump their values onto the page.

Perhaps somebody out there knows how I can successfully get the type of post and title and set them to if() statements in order to trigger whatever else.

Thanks!

5
  • 2
    post_class prints the value, get_post_class returns the value. Commented Oct 20, 2013 at 16:34
  • Hey! That does it! Thanks! Commented Oct 20, 2013 at 16:36
  • 1
    Would it be ok if I would write it as an answer? Commented Oct 20, 2013 at 16:36
  • Do you want to add it as an Answer or should I do it on your behalf? Commented Oct 20, 2013 at 16:37
  • Yes, perfect! (Answer to your previous question.) Commented Oct 20, 2013 at 16:37

1 Answer 1

3

According to Wordpress Docs:

post_class()

When the post_class function is added to a tag within the loop, for example >, it will print out and add various post-related classes to the div tag.

In case you would like to be able to retrieve the value you should use the get_post_class() function which returns that value.

Retrieve the classes for the post div as an array.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, Ofir, this does it! Thanks a lot for the quick and accurate response!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.