admin管理员组文章数量:1026971
I've been using the following functions (updating IDs manually) to group parent & child pages into an array, which I then use in an if/else statement to present content depending on whether or not the current page is in that array. Examples below:
function id_array_function() {
$array = array(
10, // Example Parent ID
12, // Example Child ID
14 // Example Child ID
);
return $array;
}
function content_placement_function() {
if( is_page( id_array_example() ) ) {
// If current page is in array, do this
}
else {
// Otherwise, do this
}
}
Ideally, I'd like to create a reusable function which I can plug any page name into (avoiding IDs due to issues with local/production installs having different page IDs), and return an array of both the parent and any child page names for use elsewhere, such as:
if( is_page( id_array_function('About') ) ) {
// Function would return array as ('About', 'Our Company', 'Careers', 'etc...')
// If current page is in array, do this
}
I've attempted this with wp_list_pages (could not return, only echo), and get_posts/get_terms (both returned empty arrays). If anyone has a pre-existing snippet or an idea as to how I could achieve the reusable function, I'd be massively appreciative of the help.
==========
EDIT: Working answer from Krzysiek below. Possible alternative option on CSS Tricks (targeting IDs): /
I've been using the following functions (updating IDs manually) to group parent & child pages into an array, which I then use in an if/else statement to present content depending on whether or not the current page is in that array. Examples below:
function id_array_function() {
$array = array(
10, // Example Parent ID
12, // Example Child ID
14 // Example Child ID
);
return $array;
}
function content_placement_function() {
if( is_page( id_array_example() ) ) {
// If current page is in array, do this
}
else {
// Otherwise, do this
}
}
Ideally, I'd like to create a reusable function which I can plug any page name into (avoiding IDs due to issues with local/production installs having different page IDs), and return an array of both the parent and any child page names for use elsewhere, such as:
if( is_page( id_array_function('About') ) ) {
// Function would return array as ('About', 'Our Company', 'Careers', 'etc...')
// If current page is in array, do this
}
I've attempted this with wp_list_pages (could not return, only echo), and get_posts/get_terms (both returned empty arrays). If anyone has a pre-existing snippet or an idea as to how I could achieve the reusable function, I'd be massively appreciative of the help.
==========
EDIT: Working answer from Krzysiek below. Possible alternative option on CSS Tricks (targeting IDs): https://css-tricks/snippets/wordpress/if-page-is-parent-or-child/
Share Improve this question edited Mar 27, 2019 at 17:58 Graeme Bryson asked Mar 27, 2019 at 16:22 Graeme BrysonGraeme Bryson 291 silver badge11 bronze badges1 Answer
Reset to default 1OK, so what we want to achieve is to write a function that will take a title of a page and return the array containing its ID and IDs of its children. So here's the code:
function get_page_and_its_children_ids_by_title( $page_title ) {
$result = array();
$page = get_page_by_title( $page_title ); // find page with given title
if ( $page ) { // if that page exists
$result[] = $page->ID; // add its ID to the result
$children = get_children( array( // get its children
'post_parent' => $page->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
) );
$result = array_merge( $result, array_keys( $children ) ); // add children ids to the result
}
return $result;
}
I've been using the following functions (updating IDs manually) to group parent & child pages into an array, which I then use in an if/else statement to present content depending on whether or not the current page is in that array. Examples below:
function id_array_function() {
$array = array(
10, // Example Parent ID
12, // Example Child ID
14 // Example Child ID
);
return $array;
}
function content_placement_function() {
if( is_page( id_array_example() ) ) {
// If current page is in array, do this
}
else {
// Otherwise, do this
}
}
Ideally, I'd like to create a reusable function which I can plug any page name into (avoiding IDs due to issues with local/production installs having different page IDs), and return an array of both the parent and any child page names for use elsewhere, such as:
if( is_page( id_array_function('About') ) ) {
// Function would return array as ('About', 'Our Company', 'Careers', 'etc...')
// If current page is in array, do this
}
I've attempted this with wp_list_pages (could not return, only echo), and get_posts/get_terms (both returned empty arrays). If anyone has a pre-existing snippet or an idea as to how I could achieve the reusable function, I'd be massively appreciative of the help.
==========
EDIT: Working answer from Krzysiek below. Possible alternative option on CSS Tricks (targeting IDs): /
I've been using the following functions (updating IDs manually) to group parent & child pages into an array, which I then use in an if/else statement to present content depending on whether or not the current page is in that array. Examples below:
function id_array_function() {
$array = array(
10, // Example Parent ID
12, // Example Child ID
14 // Example Child ID
);
return $array;
}
function content_placement_function() {
if( is_page( id_array_example() ) ) {
// If current page is in array, do this
}
else {
// Otherwise, do this
}
}
Ideally, I'd like to create a reusable function which I can plug any page name into (avoiding IDs due to issues with local/production installs having different page IDs), and return an array of both the parent and any child page names for use elsewhere, such as:
if( is_page( id_array_function('About') ) ) {
// Function would return array as ('About', 'Our Company', 'Careers', 'etc...')
// If current page is in array, do this
}
I've attempted this with wp_list_pages (could not return, only echo), and get_posts/get_terms (both returned empty arrays). If anyone has a pre-existing snippet or an idea as to how I could achieve the reusable function, I'd be massively appreciative of the help.
==========
EDIT: Working answer from Krzysiek below. Possible alternative option on CSS Tricks (targeting IDs): https://css-tricks/snippets/wordpress/if-page-is-parent-or-child/
Share Improve this question edited Mar 27, 2019 at 17:58 Graeme Bryson asked Mar 27, 2019 at 16:22 Graeme BrysonGraeme Bryson 291 silver badge11 bronze badges1 Answer
Reset to default 1OK, so what we want to achieve is to write a function that will take a title of a page and return the array containing its ID and IDs of its children. So here's the code:
function get_page_and_its_children_ids_by_title( $page_title ) {
$result = array();
$page = get_page_by_title( $page_title ); // find page with given title
if ( $page ) { // if that page exists
$result[] = $page->ID; // add its ID to the result
$children = get_children( array( // get its children
'post_parent' => $page->ID,
'post_type' => 'page',
'numberposts' => -1,
'post_status' => 'publish'
) );
$result = array_merge( $result, array_keys( $children ) ); // add children ids to the result
}
return $result;
}
本文标签: phpGenerate an array of parentchild page IDsbased on parent page name
版权声明:本文标题:php - Generate an array of parentchild page IDs, based on parent page name 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745653367a2161450.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论