Aslan
Aslan

Reputation: 105

IF condition with OR inside doesn't work properly React JS

I'm creating plugin for wordpress on react js and got an issue.

When user is entering in admin page and choose for example posts, I'm checking if it is not a post, so user can't use customizable block:

if ( type !== "post" ) {
  return <p>{__("Sorry, Post Navigation block is available only for Posts")}</p>;
}

and it's working, but in wordpress can be more types of posts, so I'm trying to use || inside this condition to check on another type:

if ( type !== "post" || type !== "portfolio" ) {
  return <p>{__("Sorry, Post Navigation block is available only for Posts and Portfolio")}</p>;
}

And now is my problem, it's not working.

Variable type I'm getting from here:

const type = wp.data.select('core/editor').getCurrentPostType();

it returns a string.

What am I doing wrong?

Can you help me, please?

Upvotes: 1

Views: 145

Answers (1)

Nicholas Tower
Nicholas Tower

Reputation: 84912

if ( type !== "post" || type !== "portfolio" ) {

This will always be true. If type is "post", then it will not be equal to "portfolio". If it's "portfolio", then it will not be equal to "post". And if it's anything else, it will not be equal to either.

Change it to use &&

if ( type !== "post" && type !== "portfolio" ) {

Upvotes: 1

Related Questions