簡體   English   中英

WordPress僅在帖子上顯示

[英]Wordpress show only on posts

我的WP功能有問題。 我只需要在博客文章中顯示它。 現在,它會顯示在其他頁面上,例如首頁。

這是我在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;
        }
}

那里有人知道我做錯了嗎?

我現在嘗試使用此代碼,它仍在首頁上顯示$ content。

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;
        }
}

如果您只想在首頁上包含函數,則有兩個選擇-僅將函數插入主題中的single.php模板中,或者首選方法是僅在is_single()為true時將函數掛接到the_content 。但是is_page()為假。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM