猿问

如何在每个帖子的开头和结尾放置特定文本?

我目前正在使用此代码,它显示了每个帖子的代码开头。我想在每篇文章的开头和结尾显示代码。有人能帮忙吗?


add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );

function filter_the_content_in_the_main_loop( $content ) {


    // Check if we're inside the main loop in a single post page.

    if ( is_single() && in_the_loop() && is_main_query() ) {


       return esc_html__("So Friends, How is our Article of ".get_the_title().". Do You Like it? Don't Forget to Comment below if Any Queries. For More Article regarding ".get_the_title()." Subscribe Us.").$content;

    }


    return $content;

}


PIPIONE
浏览 71回答 1
1回答

三国纷争

你已经知道答案了。在“the_content”过滤器中,您将一些文本添加到 $content 变量中。现在你想要做的是追加。前任:add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );function filter_the_content_in_the_main_loop( $content ) {    // Check if we're inside the main loop in a single post page.    if ( is_single() && in_the_loop() && is_main_query() ) {       return "Beginning text" .$content . "Ending Text";    }    return $content;}“开始文本”是您已经添加的文本。[2020 年 1 月 7 日]:每个 OP 请求添加代码片段以根据 pos 类别进行更改。add_filter( 'the_content', 'filter_the_content_in_the_main_loop' );function filter_the_content_in_the_main_loop( $content ) {    // Check if we're inside the main loop in a single post page.    if ( is_single() && in_the_loop() && is_main_query() ) {    //get categories        $categories = get_the_category();        foreach($categories as $category){            //you can check by term_id, name, slug            if($category->$term_id == $target_term_id){                return "Beginning text" .$content . "Ending Text";            }        }    }    return $content;}
随时随地看视频慕课网APP
我要回答