admin管理员组

文章数量:1417711

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I have few lines of code in a theme, I did not understood, can anyone explain me this.

$screen = get_current_screen(); // understood this
if ( $screen && 'post' == $screen->base && 'page' == $screen->id ) { // not sure about this line
// code to execute something
}

As per I have understood the code is executed when post type is post or page, I am not sure how, I need to add another custom post type (player) in that if statement.

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I have few lines of code in a theme, I did not understood, can anyone explain me this.

$screen = get_current_screen(); // understood this
if ( $screen && 'post' == $screen->base && 'page' == $screen->id ) { // not sure about this line
// code to execute something
}

As per I have understood the code is executed when post type is post or page, I am not sure how, I need to add another custom post type (player) in that if statement.

Share Improve this question edited Aug 2, 2019 at 3:20 Sally CJ 40.3k2 gold badges29 silver badges50 bronze badges asked Aug 1, 2019 at 22:44 user2445user2445 212 bronze badges 2
  • Actually, that conditional means, check if the base of the current file in the URL is post and that the post type is page. I.e. If you're editing a Page, the conditional would return true. If you want to check for another post type, just change the 'page' == $screen->id to in_array( $screen->id, [ 'page', 'player', 'etc-cpt' ] ) – Sally CJ Commented Aug 2, 2019 at 2:35
  • 1 @SallyCJ That sounds like a wonder answer you could post in the answer section below :) – Howdy_McGee Commented Aug 2, 2019 at 14:10
Add a comment  | 

1 Answer 1

Reset to default 1

the code is executed when post type is post or page

No, only if the post type is page (because of the 'page' == $screen->id).

Because that if statement actually checks if the current admin screen is the one for editing or creating a Page (post type of page), where the URL looks like so: (note that the post type is not visible in the URL when a post/Page/CPT is being edited)

http://example/wp-admin/post.php?post=2&action=edit

  • On such screens — editing or creating a post/Page/CPT, $screen->id is equivalent to $screen->post_type which is the post type (slug).

  • And $screen->base is post which is the base name of the file name in the current URL — base name is the file name without the extension — so if the file name is post.php, the base name is post. However, when creating a post/Page/CPT, the file name is actually post-new.php, but still the $screen->base is post (and not post-new) because WordPress strips the -new part.

  • Additionally, you can distinguish between editing and creating by checking the screen's action; i.e. whether $screen->action is edit or add (e.g. 'edit' == $screen->action to check if the action is editing).

I need to add another custom post type (player) in that if statement

Just change the:

'page' == $screen->id

to:

in_array( $screen->id, array( 'post', 'page', 'player' ) )

Or the full code:

if ( $screen &&                // 1. we've got a valid screen
    'post' == $screen->base && // 2. the screen base is either post.php or post-new.php
    // and 3. the post type is one of the array values.
    in_array( $screen->id, array( 'post', 'page', 'player' ) )
) {
    // your code here
}

I hope that helps, and you can check the get_current_screen()'s function reference here. :)

本文标签: phpunderstanding if statement