WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 3 months ago

#33045 reviewing enhancement

New conditional tags for child/parent pages

Reported by: ramiy Owned by: johnbillion
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: has-patch
Focuses: template Cc:

Description

New enhancements for theme developers with new conditional tags:

  • has_parent() - return boolean result using $post->post_parent.
  • parent_page() - return the parent page id using $post->post_parent.
  • is_child_of( $id ) - return boolean result using $post->post_parent.

We can change the names, but we need the functionality.

Attachments (4)

33045.patch (993 bytes) - added by ramiy 2 years ago.
33045.2.patch (967 bytes) - added by ramiy 2 years ago.
33045.3.patch (1.0 KB) - added by sebastian.pisula 2 years ago.
33045.4.patch (848 bytes) - added by ramiy 2 years ago.

Download all attachments as: .zip

Change History (25)

#1 @ramiy
2 years ago

See the patch.

Last edited 2 years ago by ramiy (previous) (diff)

@ramiy
2 years ago

#2 @ramiy
2 years ago

For consistency, the patch is based on the code of has_excerpt() and get_the_excerpt() functions.

@ramiy
2 years ago

#3 @ramiy
2 years ago

  • Keywords has-patch added

This ticket was mentioned in Slack in #core by chriscct7. View the logs.


2 years ago

#5 follow-up: @johnbillion
2 years ago

  • Component changed from General to Posts, Post Types
  • Keywords reporter-feedback 2nd-opinion added

@ramiy What's the use case for filtering the return value of get_parent()?

Also these functions need post in their name to avoid ambiguity. eg. wp_get_post_parent().

#6 @sebastian.pisula
2 years ago

I added my version of patch

#7 in reply to: ↑ 5 @ramiy
2 years ago

  • Keywords reporter-feedback removed

Replying to johnbillion:

@ramiy What's the use case for filtering the return value of get_parent()?

Also these functions need post in their name to avoid ambiguity. eg. wp_get_post_parent().

You are right, there is no reason to filter the returned value. @sebastian.pisula revised my patch, he removed the filter and added the required post to the function name.

#8 @nir_r
2 years ago

+1 for this feature

#9 follow-up: @ramiy
2 years ago

Actually,

I don't agree with the new function name wp_get_post_parent(),
I prefer it would be get_post_parent(), without the wp_ prefix.

We should stick with the naming conventions in post-template.php, non of the get functions use the wp_ prefix:

  • get_the_ID()
  • get_the_title()
  • get_the_guid()
  • get_the_content()
  • get_the_excerpt()
  • get_post_class()
  • get_body_class()
Last edited 2 years ago by ramiy (previous) (diff)

@ramiy
2 years ago

#10 in reply to: ↑ 9 @maor
2 years ago

Love these functions! Great idea.

I actually just wrapped up a project where these functions could have been super helpful if existed.

Also, I agree with @ramiy on the naming convention. IMHO, I think there shouldn't be a wp_ prefix for the proposed conditional tags.

#11 @ramiy
2 years ago

Same logic can be applied for the boolean function, rename back from has_post_parent() to has_parent().

The other has_() functions don't use the post in their name:

  • has_excerpt()
  • has_tag()
  • has_term()
  • has_nav_menu()

Only one function use post in the name:

  • has_post_thumbnail()

This ticket was mentioned in Slack in #core by ramiy. View the logs.


23 months ago

This ticket was mentioned in Slack in #core by ramiy. View the logs.


3 months ago

#14 @SergeyBiryukov
3 months ago

  • Milestone changed from Awaiting Review to 4.9

#15 @ramiy
3 months ago

  • Keywords 2nd-opinion removed

@SergeyBiryukov Let's add this enhancement to 4.9. The ticket has a patch, it was tested and got a very good feedback from @johnbillion and @sebastian.pisula.

The only issue here was the function names. In the last patch I applied the naming conventions set in post-template.php to match similar template functions.

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


3 months ago

#17 @johnbillion
3 months ago

  • Owner set to johnbillion
  • Status changed from new to reviewing

#18 @johnbillion
3 months ago

I've decided these functions should be named get_parent_post() and has_parent_post(). The "parent post" is the object being gotten.

This also allows functions such as get_parent_term() to be introduced in the future in order to fetch a term's parent.

#19 @johnbillion
3 months ago

And just as I submitted that I questioned it. Maybe it needs to be get_parent_post_id(). Naming things is hard.

#20 @johnbillion
3 months ago

  • Milestone changed from 4.9 to Future Release

#21 @SergeyBiryukov
3 months ago

For reference, comment:28:ticket:24164 has a good overview of existing get_*() and the_*() template functions.

Until a new function is added, get_post_field( 'post_parent', $post ) could be used for the same purpose.

Note: See TracTickets for help on using tickets.