Forums

get post_type in template hook (3 posts)

  1. Parsa
    Member
    Posted 4 months ago #

    hi
    my problem about template hook and detect post type

    this code don't work!

    add_filter('template', 'echo_post_type');
    function echo_post_type($template){
    	if ( !is_admin() ) {
    		global $wp_query;
    		echo $wp_query->post->post_type;
    
    		//or
    
    		global $post;
    		echo $post->post_type;
    
    		exit();
    	}
    }

    why?

  2. What are you trying to do? Display or act on the post_type outside of the admin page i.e. within a post or page?

    As you probably know the is_admin() checks if you are on an admin page and not if you are an admin user.

    You might have better luck with this instead (if you want to work with the post).

    add_action( 'the_post' , 'echo_post_type' );
  3. Parsa
    Member
    Posted 4 months ago #

    tnx, i need detect post type for change website theme
    in page setting plugin, user can specific theme for post type (example: news).

Reply

You must log in to post.

About this Topic