$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to get Post Type from the functions.php file|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to get Post Type from the functions.php file

matteradmin10PV0评论

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' );

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far