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