Closed. This question is off-topic. It is not currently accepting answers.admin管理员组文章数量:1417711
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 questionI 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.
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 questionI 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.
1 Answer
Reset to default 1the 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
ispost
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 ispost.php
, the base name ispost
. However, when creating a post/Page/CPT, the file name is actuallypost-new.php
, but still the$screen->base
ispost
(and notpost-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
isedit
oradd
(e.g.'edit' == $screen->action
to check if the action is editing).
I need to add another custom post type (
player
) in thatif
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
版权声明:本文标题:php - understanding if statement 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745274554a2651101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
post
and that the post type ispage
. 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
toin_array( $screen->id, [ 'page', 'player', 'etc-cpt' ] )
– Sally CJ Commented Aug 2, 2019 at 2:35