简体   繁体   中英

Wordpress show only on posts

i have a problem with my WP function. I need it to ONLY be shown on blog posts. Right now it get shown on the other pages such as the frontpage.

Here is my function in functions.php:

    function my_sp_function( $content ) {
        $include = FALSE;
        $post = FALSE;

        if ( in_category( array( 3, 4, 5, 6, 91, 133 ) ) ) {

            $include = TRUE;

        }
        if (get_post_type(get_the_ID()) == 'post'){
            $post = TRUE;
        }

        if ( $post = TRUE AND $include == TRUE ) {
                return $content;
        }
}

Anybody there knows what i do wrong?

I have tried with this code now, it still shows the $content on the front page..

function my_sp_function( $content ) {
        $include = FALSE;
        $post = FALSE;

        if ( in_category( array( 3, 4, 5, 6, 91, 133 ) ) ) {

            $include = TRUE;

        }

        if( is_home() || is_front_page() ) {
        $include = FALSE;
        }

        if ( $post == TRUE AND $include == TRUE ) {
                return $content;
        }
}

If you would like to include your function only on the home page you have two options - only insert the function into the single.php template in your theme, or the prefered method is to only hook your function to the_content when is_single() is true but is_page() is false.

function my_sp_function( $content ) {
    $content .= "Some more stuff for the_content!";
    return $content;
}
if ( is_single() && ! is_page() ){
    add_filter( 'the_content', 'my_sp_function' );
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM