How to get Post Type from the functions.php file

admin2025-06-03  3

TLDR: How can I get the post type within the functions.php file?

I am trying to add the following code only when the post type is a 'page' and not when it is a 'post.' However, I am not able to call either $post or get_post_type() within the file functions.php

For example, here is what I would like to do:

if($post->post_type == 'page') {
    remove_filter('the_content', 'wpautop');
} elseif ($post->post_type == 'post') {
    add_filter('the_content', 'wpautop');
}

However, I have tried a myriad of different ways to get the actual post type, to no avail.

I have tried:

global $post;
echo $post_type;
echo $post_type_object;

printf( __( 'The post type is: %s', 'textdomain' ), get_post_type( get_the_ID() ) );

global $post;
var_dump($post);
var_dump($post->ID);
echo get_post_type($post->ID);
global $post_type;
$post_type = get_post_type();
echo $post_type;
print_r (get_post_type());
echo $post->post_type;

I think that I have pretty much tried everything :(

None of these will give me a post type for either the pages or the posts.

Any idea how I could get this information?

Thanks a lot!

TLDR: How can I get the post type within the functions.php file?

I am trying to add the following code only when the post type is a 'page' and not when it is a 'post.' However, I am not able to call either $post or get_post_type() within the file functions.php

For example, here is what I would like to do:

if($post->post_type == 'page') {
    remove_filter('the_content', 'wpautop');
} elseif ($post->post_type == 'post') {
    add_filter('the_content', 'wpautop');
}

However, I have tried a myriad of different ways to get the actual post type, to no avail.

I have tried:

global $post;
echo $post_type;
echo $post_type_object;

printf( __( 'The post type is: %s', 'textdomain' ), get_post_type( get_the_ID() ) );

global $post;
var_dump($post);
var_dump($post->ID);
echo get_post_type($post->ID);
global $post_type;
$post_type = get_post_type();
echo $post_type;
print_r (get_post_type());
echo $post->post_type;

I think that I have pretty much tried everything :(

None of these will give me a post type for either the pages or the posts.

Any idea how I could get this information?

Thanks a lot!

Share Improve this question asked Feb 5, 2019 at 18:25 Brad AhrensBrad Ahrens 1312 silver badges9 bronze badges 1
  • Have you tried is_page() ? – Lucien Dubois Commented Feb 5, 2019 at 19:13
Add a comment  | 

1 Answer 1

Reset to default 3

You have to create the function and call it when post ID is already set. Otherwise it will be empty. E.g. wp_head meets this requirement, init is too early.

function my_selective_wpautop() {
    global $post;

    if( is_singular() ) {
        if( 'page' == $post->post_type ) {
            remove_filter( 'the_content', 'wpautop' );
        } elseif ( 'post' == $post->post_type ) {
            add_filter( 'the_content', 'wpautop' );
        }
    }
}

add_action( 'wp_head', 'my_selective_wpautop' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748939909a315000.html

最新回复(0)