admin管理员组文章数量:1130349
New to WordPress programming (coming from a more conventional environment) and trying to understand some of its "unique" qualities.
There is a directory page on our website and this code resides, that is in functions.php, tweaks the results if condition is true.
if( $query->is_post_type_archive( 'directory' ) ){
...//do stuff
}
I would like to know how to access the "value of is_post_type_archive that is "directory." When I use test for the value...
var_dumb($query->is_post_type_archive());
..I get bool(true) which makes sense. But how/where is the value "directory" stored/passed/accessed?
New to WordPress programming (coming from a more conventional environment) and trying to understand some of its "unique" qualities.
There is a directory page on our website and this code resides, that is in functions.php, tweaks the results if condition is true.
if( $query->is_post_type_archive( 'directory' ) ){
...//do stuff
}
I would like to know how to access the "value of is_post_type_archive that is "directory." When I use test for the value...
var_dumb($query->is_post_type_archive());
..I get bool(true) which makes sense. But how/where is the value "directory" stored/passed/accessed?
2 Answers
Reset to default 1First up, the first thing you should do is read the developer docs. This question touches on a lot of topics, and it's not going to be possible to explain them all in one answer.
Anyway, directory would be a Custom Post Type registered by the theme or a plugin.
When a post type is registered the developer can tell WordPress what the URL path for its archive should be.
WordPress will then create a rewrite rule so that when a user visits that URL, WordPress receives a parameter that tells it that the user is requesting the archive for that post type.
When the user visits that URL WordPress will query the posts it needs to show for that post type archive. It will do that with a WP_Query() object. As part of this query WordPress will set the is_post_type_archive property of that object to true, and the post_type property to directory.
Developers can change the behaviour of post queries using the pre_get_posts filter. Since WordPress can run more than one post query on any given request, any functions used on this filter will receive the current WP_Query object as a parameter. The developer can then choose to modify the only the main query for the post type archive by checking if $query->is_post_type_archive( 'directory' ) is true for the current query.
You found a regular WordPress object, $query, which is well documented in the Code Reference on its own page:
WP_Query::is_post_type_archive( mixed $post_types = '' )Is the query for an existing post type archive page?
Description
Parameters
$post_types (mixed) (Optional) Post type or array of posts types to check against.
Default value: ''
The source code is given there as well. So what does is_post_type_archive($post_types) do?
is_post_type_archive()returnstrueif the current page is a post type archive of any kindis_post_type_archive('foo')returnstrueif the current page if a post type archive for the (custom) post typefoo(could bepost,page, etc)is_post_type_archive(['foo', 'bar'])returnstrueif the current page is a post type archive for any of those (custom) post typesfoo,bar.
New to WordPress programming (coming from a more conventional environment) and trying to understand some of its "unique" qualities.
There is a directory page on our website and this code resides, that is in functions.php, tweaks the results if condition is true.
if( $query->is_post_type_archive( 'directory' ) ){
...//do stuff
}
I would like to know how to access the "value of is_post_type_archive that is "directory." When I use test for the value...
var_dumb($query->is_post_type_archive());
..I get bool(true) which makes sense. But how/where is the value "directory" stored/passed/accessed?
New to WordPress programming (coming from a more conventional environment) and trying to understand some of its "unique" qualities.
There is a directory page on our website and this code resides, that is in functions.php, tweaks the results if condition is true.
if( $query->is_post_type_archive( 'directory' ) ){
...//do stuff
}
I would like to know how to access the "value of is_post_type_archive that is "directory." When I use test for the value...
var_dumb($query->is_post_type_archive());
..I get bool(true) which makes sense. But how/where is the value "directory" stored/passed/accessed?
2 Answers
Reset to default 1First up, the first thing you should do is read the developer docs. This question touches on a lot of topics, and it's not going to be possible to explain them all in one answer.
Anyway, directory would be a Custom Post Type registered by the theme or a plugin.
When a post type is registered the developer can tell WordPress what the URL path for its archive should be.
WordPress will then create a rewrite rule so that when a user visits that URL, WordPress receives a parameter that tells it that the user is requesting the archive for that post type.
When the user visits that URL WordPress will query the posts it needs to show for that post type archive. It will do that with a WP_Query() object. As part of this query WordPress will set the is_post_type_archive property of that object to true, and the post_type property to directory.
Developers can change the behaviour of post queries using the pre_get_posts filter. Since WordPress can run more than one post query on any given request, any functions used on this filter will receive the current WP_Query object as a parameter. The developer can then choose to modify the only the main query for the post type archive by checking if $query->is_post_type_archive( 'directory' ) is true for the current query.
You found a regular WordPress object, $query, which is well documented in the Code Reference on its own page:
WP_Query::is_post_type_archive( mixed $post_types = '' )Is the query for an existing post type archive page?
Description
Parameters
$post_types (mixed) (Optional) Post type or array of posts types to check against.
Default value: ''
The source code is given there as well. So what does is_post_type_archive($post_types) do?
is_post_type_archive()returnstrueif the current page is a post type archive of any kindis_post_type_archive('foo')returnstrueif the current page if a post type archive for the (custom) post typefoo(could bepost,page, etc)is_post_type_archive(['foo', 'bar'])returnstrueif the current page is a post type archive for any of those (custom) post typesfoo,bar.
本文标签: How to read the value of a WordPress query associative array (hash) key
版权声明:本文标题:How to read the value of a WordPress $query associative array (hash) key 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749194233a2330593.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论