【问题标题】:How to place script in dynamic paragraph如何在动态段落中放置脚本
【发布时间】:2019-03-27 14:47:30
【问题描述】:

我有 google adsense 脚本,我可以将它放置在我页面上的不同位置。

我还有 body 文本,其中每个 postdescription 是,我想知道如何动态添加 adsense 脚本进入我的帖子正文? (Google 建议将其放在第二段之后)。

我正在使用laravel,这就是我获取每个帖子的身体部分的方式

{!! $post->body !!}

Google Adsense 代码示例:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
  style="display:block"
  data-ad-client="ca-pub-6565454545454774"
  data-ad-slot="548855465655"
  data-ad-format="auto"
  data-full-width-responsive="true"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

有什么想法吗?

更新

single post function

//single post
public function single($slug)
{
        $post = Post::where('slug', $slug)->where('publish', '=', 'y')->firstOrFail();

        $post->addPageView();

        $previous = Post::where('slug', '<', $post->slug)->max('slug');

        $next = Post::where('slug', '>', $post->slug)->min('slug');

        $products = Product::all()->where('status', 'enable')->random(3);

        $categories = PostCategory::all();

        $settings = Setting::all();

        $author = AuthorInfo::where('user_id', $post->user->id)->first();

        return view('front.singlepost', compact('post', 'previous', 'next', 'products','categories', 'settings','author'));
}

【问题讨论】:

  • 你能用用于post页面的控制器更新吗?
  • $post-&gt;body 文本是否使用 html 标签(如段落标签)预先格式化?您可以通过找到第二个

    and using the substr_replace function.

    将 adsense 文本插入到 body
  • @ourmandave 是的,它会打印帖子正文的内容,例如 &lt;p&gt;....&lt;/p&gt; 以及我们在 ckeditor 中创建该内容时添加的任何其他内容。
  • @thisiskelvin 我只是根据他们的slug 获得帖子,然后在刀片中获得每个帖子的不同列,例如$post-&gt;body 控制器部分没有什么特别之处,但是这里是:$post = Post::where('slug', $slug)-&gt;where('publish', '=', 'y')-&gt;firstOrFail();跨度>
  • @ourmandave 抱歉,但我不知道如何使用 substr_replace PS:如果有帮助,我在之前的评论中包含了我的控制器部分。

标签: javascript laravel adsense


【解决方案1】:

我还没有机会对此进行测试,但是您可以创建一个 accessor(在本例中为 getBodyWithAdsenseAttribute),它将创建正文内容的更改版本并在 第二段:

在您的 Post 模型文件中:

public function getBodyWithAdsenseAttribute()
{
    $javascript = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    <ins class="adsbygoogle"
      style="display:block"
      data-ad-client="ca-pub-6565454545454774"
      data-ad-slot="548855465655"
      data-ad-format="auto"
      data-full-width-responsive="true"></ins>
    <script>
    (adsbygoogle = window.adsbygoogle || []).push({});
    </script>';

    $paragraphs  = explode('</p>', $this->body); // Explode current body field
    $new_content = ''; // Variable for new content
    $count       = 1; // Set count for adding new content

    foreach ($paragraphs as $paragraph) {
        $new_content .= $paragraph;

        if ($count == 2) {
            $new_content .= $javascript;
        }

        $count++;
    }

    return $new_content;
}

在这里,我们将所有的 adsense 数据存储在一个 $javascript 变量中。

我们然后explode() body 内容通过关闭&lt;/p&gt; 标签,从内容创建一个数组。

使用foreach(),我们重新创建body 内容,计算它是否在&lt;/p&gt; 标记的2nd 实例之后。如果是这样,我们将$javascript 内容添加到新内容中。

最后,我们返回所有内容。这可以在刀片中使用,如下所示

{!! $post->bodyWithAdsense !!}

注意:如果只有一个段落,或者根本没有正文内容,则需要更多代码来回退。

【讨论】:

  • 非常感谢,我会试试这个并告诉你结果
  • 3 小时后我会测试它,抱歉耽搁了。我是夜班的:)
  • 它删除了我的正文内容。
  • 你的身体肯定有&lt;p&gt;标签吗?
  • accessors 不能那样工作。创建完成后,您需要取出setattribute。请在您的问题中发布您的控制器。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-02
  • 1970-01-01
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 2014-11-11
  • 1970-01-01
相关资源
最近更新 更多