admin管理员组

文章数量:1303343

Im wondering if it's possible to add custom post state's to other pages? Like Frontpage and Blog

I mean the black text: --Voorpagina (Frontpage)

I have tried this in functions.php.

add_filter('display_post_states', 'wpsites_custom_post_states');

function wpsites_custom_post_states($states) { 
    global $post; 
    if( ('page' == get_post_type($post->ID) ) 
        && ( '/themes/Lef-en-Liefde/woningoverzicht.php' == get_page_templ‌​ate_slug( $post->ID ) ) ) {

            $states[] = __('Custom state'); 

    } 
} 

But then the original post states (frontpage and blog) are gone.

Thanks in advance,

Gino

Im wondering if it's possible to add custom post state's to other pages? Like Frontpage and Blog

I mean the black text: --Voorpagina (Frontpage)

I have tried this in functions.php.

add_filter('display_post_states', 'wpsites_custom_post_states');

function wpsites_custom_post_states($states) { 
    global $post; 
    if( ('page' == get_post_type($post->ID) ) 
        && ( '/themes/Lef-en-Liefde/woningoverzicht.php' == get_page_templ‌​ate_slug( $post->ID ) ) ) {

            $states[] = __('Custom state'); 

    } 
} 

But then the original post states (frontpage and blog) are gone.

Thanks in advance,

Gino

Share Improve this question edited Sep 21, 2016 at 14:57 Andy Macaulay-Brook 3,8593 gold badges19 silver badges44 bronze badges asked Sep 21, 2016 at 13:47 GinoGino 211 silver badge2 bronze badges 3
  • What research have you done and what code have you tried? – Andy Macaulay-Brook Commented Sep 21, 2016 at 13:53
  • May you please elaborate your question? As @AndyMacaulay-Brook stated, what have you tried so far? This way we can help understand your situation. – Ethan Rævan Commented Sep 21, 2016 at 13:56
  • I have tried this in functions.php. <?php add_filter('display_post_states', 'wpsites_custom_post_states'); function wpsites_custom_post_states($states) { global $post; if( ('page'==get_post_type($post->ID)) && ('/themes/Lef-en-Liefde/woningoverzicht.php'==get_page_template_slug($post->ID)) ) { $states[] = __('Custom state'); } } But then the original post states (frontpage and blog) are gone. – Gino Commented Sep 21, 2016 at 14:12
Add a comment  | 

3 Answers 3

Reset to default 3

Your function doesn't return a value, so you are effectively wiping the existing states.

Also, this hook passes you the current post as an object, so you can use that instead of the global $post.

Additionally get_page_templ‌​ate_slug returns a path that is relative to your theme's root, so if your theme directory is called Lef-en-Liefde and the template file placed at the top level of that directory is called woningoverzicht.php, then your condition '/themes/Lef-en-Liefde/woningoverzicht.php' == get_page_templ‌​ate_slug($post->ID) can never be true.

So, correcting/changing for those points, this should work:

add_filter('display_post_states', 'wpse240081_custom_post_states',10,2);

function wpse240081_custom_post_states( $states, $post ) { 
    
    if ( ( 'page' == get_post_type( $post->ID ) ) 
        && ( 'woningoverzicht.php' == get_page_templ‌​ate_slug( $post->ID ) ) ) {

            $states[] = __('Custom state'); 

    } 

    return $states;
} 

Here is how WooCommerce does it:

<?php
add_filter( 'display_post_states', 'add_display_post_states', 10, 2 );

/**
 * Add a post display state for special WC pages in the page list table.
 *
 * @param array   $post_states An array of post display states.
 * @param WP_Post $post        The current post object.
 */
function add_display_post_states( $post_states, $post ) {
    if ( wc_get_page_id( 'shop' ) === $post->ID ) {
        $post_states['wc_page_for_shop'] = __( 'Shop Page', 'woocommerce' );
    }

    if ( wc_get_page_id( 'cart' ) === $post->ID ) {
        $post_states['wc_page_for_cart'] = __( 'Cart Page', 'woocommerce' );
    }

    if ( wc_get_page_id( 'checkout' ) === $post->ID ) {
        $post_states['wc_page_for_checkout'] = __( 'Checkout Page', 'woocommerce' );
    }

    if ( wc_get_page_id( 'myaccount' ) === $post->ID ) {
        $post_states['wc_page_for_myaccount'] = __( 'My Account Page', 'woocommerce' );
    }

    if ( wc_get_page_id( 'terms' ) === $post->ID ) {
        $post_states['wc_page_for_terms'] = __( 'Terms and Conditions Page', 'woocommerce' );
    }

    return $post_states;
}

If you use that filter in custom plugin, you will need to use the function "get_post_meta" because the function "get_page_templ‌​ate_slug" is not already loaded

exemple:

add_filter('display_post_states', 'wpse240081_custom_post_states', 10, 2);

function wpse240081_custom_post_states( $states, $post ) {
    
    // if is list page and name template file php...
    if ( ( 'page' == get_post_type( $post->ID ) ) && ( 'your-template-name.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) ) {

        $states[] = __('Custom state');
    }

    return $states;
}

本文标签: pagesChange WordPress poststate in Admin Area