开发者

An php if-statement with three '||' an '&&' and then an '||'?

I'm using the following if statement to add a class to the list item:

<?php
     global $post;
     $the_post_parent = $post->post_parent;
     $the_post_ID = $post->ID;
     $bbp_forums = get_posts('post_type=forum&orderby=title&order=a开发者_StackOverflow社区sc&posts_per_page=-1&order=ASC');
?>

<li <?php if ( $post->ID == $the_post_ID || $post->ID == $the_post_parent ) echo 'class="current"'; ?>>

In this case, if the global post id matches the current post id the class .current is echoed.

I want to add three more conditions to the statement:

Excute the echo only if the current post type is a forum or topic or topic tag:

'forum' == get_post_type() 
'topic' == get_post_type() 
'topic-tag' == get_post_type()

So at this end if should read:

If the ...

current post type is a forum OR topic OR topic tag AND its

global post id is equal to the current post id

OR the global post id is equal to the current post parent

echo the class.

How that if-statement would look like?


You can do something like that : if ( in_array(get_post_type(), array('forum', 'topic', 'topic-tag') && in_array($post->ID, array($the_post_ID,$the_post_parent) )


It would look exactly as you described it:

if ((get_post_type() == 'forum' || get_post_type() == 'topic' ...) &&
    (id == whatever || id == somethingelse))
      echo "whatever"

Just make sure that the parenthesis give you the precedence you actually want.


if( in_array( ( $filter, array('forum','topic','topic-tag') && $global_post_id == $current_post_id ) || ( $global_post_id == $current_post_id ) )

You could pull the array from a model or cfg, etc.


It would be as you read it..

((current post type is a forum OR topic OR topic tag )
AND
(its global post id is equal to the current post id))
OR
(the global post id is equal to the current post parent)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜