[Support request] Replace/Filter the_content if post is in parent OR child categories

Home Forums Support [Support request] Replace/Filter the_content if post is in parent OR child categories

Home Forums Support Replace/Filter the_content if post is in parent OR child categories

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2311662
    Tesco

    I have two parent categories in my site. Each one has several child categories.

    How can I replace/filter the_content if the post is inside a parent OR it’s child categories in the single.php?

    #2311963
    David
    Staff
    Customer Support

    Hi there,

    you could create a helper function eg.

    function db_category_has_parent() {
        $categories = get_the_category();
        if ($categories[0]->category_parent > 0) {
            return true;
        }
        return false;
    }

    This will check if the post category HAS a parent ( ie. its a child ).

    Then you can use that in your the_content filter eg.

    function my_content_filter_function( $content ) {
        // If single post and category has parent
        if ( is_single() && 'post' == get_post_type() && db_category_has_parent() = ) {
            // do something for child category post
        }
        // if single post in parent category 
        if ( is_single() && 'post' == get_post_type() && !db_category_has_parent() = ) {
            // do something for parent category post
        }
        return $content;
    }
    add_filter( 'the_content', 'my_content_filter_function');
    #2313694
    Tesco

    Can I replace the_content() completely by any other thing when I need?

    I need to do something like this:

    function my_content_filter_function( $content ) {
    // If single post and category has parent
    if ( is_single() && ‘post’ == get_post_type() && db_category_has_parent() = ) {
    // here I will do something without calling the_content()
    echo ‘blá, blá, blá…’;
    }
    // if single post in parent category
    if ( is_single() && ‘post’ == get_post_type() && !db_category_has_parent() = ) {
    // here I will do something BUT I will also call the_content()
    echo ‘blá, blá, blá…’;
    echo the_content();
    }
    return $content;
    }
    add_filter( ‘the_content’, ‘my_content_filter_function’);

    #2314135
    David
    Staff
    Customer Support

    Don’t use echo in your conditions. Just load the $content variable with what you need and then return it.
    eg.

    function my_content_filter_function( $content ) {
        // If single post and category has parent
        if ( is_single() && 'post' == get_post_type() && db_category_has_parent() = ) {
            // do something for child category post
            $content = 'this replaces the content';
        }
        // if single post in parent category 
        if ( is_single() && 'post' == get_post_type() && !db_category_has_parent() = ) {
            // do something for parent category post
            $content = 'this will be added before the content' . $content;
        }
        return $content;
    }
    add_filter( 'the_content', 'my_content_filter_function');
    #2314331
    Tesco

    Not quite what I expected…

    What I need is to manipulate the order where the_content renders

    eg.

        // let's suppose that I have 3 variables:
           $string-one ="this is the first text";
           $string-two ="this is the second text";
           $string-three = $content;
        if ( is_single() && 'post' == get_post_type() && db_category_has_parent() = ) {
            // do something for child category post
           $content = $string-one;
           $content = $content;
           $content = $string-two;
        }

    OR

    $content = $string-two;
     $content = $string-one;
     $content = $content;
    #2314336
    David
    Staff
    Customer Support

    As an aside this is NOT theme related, and there is only so far i can go in teaching how PHP and WordPress functions work.

    the_content function can only be rendered once on a post.

    the_content filter hook aka add_filter( 'the_content', 'my_content_filter_function'); expects there to be one value returned to it. In your function callback that value is stored in $content

    If you want to combine multiple strings then you can use the concatenate operator .:

    https://www.php.net/manual/en/language.operators.string.php

    For example:

    $content = $string-one . $content . $string-two;

    or

    $content = $string-one . $string-two . $content;

    #2314376
    Tesco
    $content = $string-one . $content . $string-two;
    return $content;

    I already tried that but it’s not respecting the order… It always show’s $content for last

    #2314441
    David
    Staff
    Customer Support

    Then thats something up with your code.
    This is an example of how it works:

    function my_content_filter_function( $content ) {
    
        $string_one = 'this is string one';
        $string_two = 'this is string two';
        if ( is_single() ) {
            $content = $string_one . $content . $string_two;
        }
        return $content;
    	
    }
    add_filter( 'the_content', 'my_content_filter_function');
    

    And that works.

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.