Last active
May 27, 2025 08:42
-
-
Save isuke01/738194bfa5498ae8437b070809b6855c to your computer and use it in GitHub Desktop.
WP - Assign to page separated template file and extend pages status texts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'display_post_states', 'use_template_as_post_state', 100, 2); | |
function use_template_as_post_state( $post_states, $post ) { | |
$available_templates = get_page_templates($post); | |
$post_template_file = get_post_meta( $post->ID, '_wp_page_template', true ); | |
ksort( $available_templates ); | |
foreach ( array_keys( $available_templates ) as $template ) { | |
if($available_templates[ $template ] == $post_template_file) $post_states[] = '<small style="display: inline-block; font-size: 10px; font-weight: 500; padding: 2px 6px; margin-left: 6px; border-radius: 4px; background: rgba(0,124,186,0.1); color: #007cba; line-height: 1.2; border: 1px solid rgba(0,124,186,0.3); text-transform: uppercase;">'.esc_html__('Template: ', 'textdomain').$template.'</small>'; | |
} | |
return $post_states; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'display_post_states', 'use_template_as_post_state', 100, 2); | |
function use_template_as_post_state( $post_states, $post ) { | |
$available_templates = get_page_templates($post); | |
$post_template_file = get_post_meta( $post->ID, '_wp_page_template', true ); | |
ksort( $available_templates ); | |
foreach ( array_keys( $available_templates ) as $template ) { | |
if($available_templates[ $template ] == $post_template_file) $post_states[] = '<small style="display: inline-block; font-size: 10px; font-weight: 500; padding: 2px 6px; margin-left: 6px; border-radius: 4px; background: rgba(0,124,186,0.1); color: #007cba; line-height: 1.2; border: 1px solid rgba(0,124,186,0.3); text-transform: uppercase;">'.esc_html__('Template: ', 'textdomain').$template.'</small>'; | |
} | |
// Example with extra elements like ACF based archives pages ... | |
if ( $post->post_type === 'page' ) { | |
global $option; | |
if ( $option['archives-projects'] == $post->ID ) { | |
$post_states[] = '<small>'.__('Archive for projects','textdomain').'</small>'; | |
} | |
} | |
return $post_states; | |
} | |
// Re-write default template file with one you want. | |
add_filter( 'template_include', 'archive_for_peojects', 99 ); | |
function archive_for_peojects( $template ) { | |
global $post, $option; | |
if ( $post->ID == $option['archives-projects'] ) { | |
$new_template = locate_template( array( 'template/archive-projects.php' ) ); | |
if ( '' != $new_template ) { | |
return $new_template ; | |
} | |
} | |
return $template; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Simple way to assign to page that you want (id is from $option['archives-projects'] or whenever you save this page ) a separated file used as template for this page.
Nice way to build special archives based on custom files, and add to option a way to assign page.
And second file is for add more info in backed for page status title.