如何在WordPress发布的每个开头添加站点URL

时间:2018-07-25 09:25:59

标签: php wordpress

如何在我的WordPress内容的每个开头添加我的网站URL(例如:test.com),所以它变成这样:

  

test.com-Lorem ipsum dollor bla bla bla Lorem ipsum dollor bla bla bla   bla Lorem ipsum dollor bla bla bla

我希望有人能提供帮助。

3 个答案:

答案 0 :(得分:0)

这将提供您想要的结果

<?php 
  var $content_fetch = explode('://',home_url());
  echo $content_fetch[1];
?>

您可以像这样回声

<?php echo $content_fetch[1];?> - Lorem ipsum dollor bla bla bla

答案 1 :(得分:0)

您可以这样做

//The filter for changing content without saving it in DB
add_filter( 'the_content', 'stender_filter_the_content');


function stender_filter_the_content( $content ) {

    // Check if we're inside the main loop in a single post page.
    if ( is_single() && in_the_loop() && is_main_query() ) {
        return "test.com -> ".$content;
    }

    return $content;
}

这应该将您的文本添加到单个帖子中,而无需在存档中进行更改。

/ 编辑 /

由于您需要将其作为第一个单词,因此您或许可以执行类似的操作。

remove_filter( 'the_content', 'wpautop' ); 

add_filter( 'the_content', 'stender_filter_the_content', 30 );


function stender_filter_the_content( $content ) {

    // Check if we're inside the main loop in a single post page.
    if ( is_single() && in_the_loop() && is_main_query() ) {
        return "test.com -> ".$content;
    }

    return $content;
}
add_filter( 'the_content', 'wpautop' , 99 );

这只是一个主意-尚未测试。

答案 2 :(得分:0)

我假设您可能希望将此文本放在大文本的顶部。在这种情况下,您应该使用Wordpress的the_content过滤器。您可以在插件或主题的functions.php中定义自己的过滤器。例如:

add_filter('the_content', 'addUrlToContent', 10, 1);

在主题的某个位置调用the_content()后,将立即执行核心响应过滤器。因此,它将生成内容,然后将内容发送到您的过滤器功能(在这种情况下为addUrlToContent()),该功能可以在将内容返回主题之前对其进行任何处理。最后两个参数是优先级(您可能在同一个'the_content'挂钩上具有多个过滤器,并希望以特定顺序执行它们)以及函数中期望的参数数量。在这种情况下,只有1($ content)。

您的addUrlToContent()函数应如下所示:

function addUrlToContent($content) {
    $content = "test.com: " . $content;
    return $content;
}

就是这样!