drf65218 2013-10-20 16:33
浏览 68
已采纳

Wordpress PHP如何将函数的返回值转换为If()语句

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!

  • 写回答

1条回答 默认 最新

  • duanrong3308 2013-10-20 16:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)