• Resolved stilldreaming

    (@stilldreaming)


    I have the following situation:

    I’ve got a custom post ‘my_custom_post’ that has no ‘rewrite’, so it does not have it’s own pages. It is just used to generate some text somewhere on some page.

    While reading the custom posts I apply the the_content filter like this:
    $my_content = apply_filters( 'the_content', get_the_content() );

    I need to apply the the_content filter.

    So far so good.

    For my posts and pages I add some text before all content like this:

    add_filter('the_content', 'myfunc_add_before_page_content', -1 );
    function myfunc_add_before_page_content( $content ){
    	if( ( is_single() || is_page() ) && in_the_loop() && is_main_query() ) {
    		return( "some text" . $content );
    	}
    	else
    	{
    		return( $content );
    	}
    }

    The problem is that “some text” is added to the above custom post ‘my_custom_post’ as well. If it was a custom post with a ‘rewrite’ and having its own page, I could add
    && !is_singular('my_custom_post')
    to myfunc_add_before_page_content( ).

    Is there any way of knowing inside myfunc_add_before_page_content( ) what kind of post the content being filtered belongs to? Or what the ID of that post is? That way I could ignore my custom posts in the filter. Or is there a simpler way that I just don’t see?

    I am sort of stuck with this…

Viewing 2 replies - 1 through 2 (of 2 total)
  • Use get_post_type() to get the custom post type and you’ll be able to add that as a condition in your if() statement. As a (very quick and untested) example…

    if( ( is_single() || is_page() ) && in_the_loop() && is_main_query() && get_post_type () != 'my_custom_post_type' ) {

    Thread Starter stilldreaming

    (@stilldreaming)

    So simple. Why didn’t I think of that? Thank you so much. You saved the day!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘the_content filter & custom posts’ is closed to new replies.