【问题标题】:How to add ad code in wordpress Single page without affecting the second single page template如何在wordpress单页中添加广告代码而不影响第二个单页模板
【发布时间】:2015-08-30 18:55:45
【问题描述】:

我为单个页面使用了两个不同的模板。一个有侧边栏,另一个没有侧边栏。

现在我只想在侧边栏模板中注入广告代码。

谁能给我这个的代码。

注意:

  1. 广告会自动插入到第二个段落之后。我有代码。

    add_filter('the_content', 'prefix_insert_post_ads');

    function prefix_insert_post_ads( $content ) {
    
    
            if ( is_single() && ! is_admin()) {
                return prefix_insert_after_paragraph( $ad_code, 2, $content );
            }
    
    
        return $content;
    }
    
    
    function prefix_insert_after_paragraph( $insertion, $paragraph_id, $content ) {
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p, $content );
        foreach ($paragraphs as $index => $paragraph) {
    
            if ( trim( $paragraph ) ) {
                $paragraphs[$index] .= $closing_p;
            }
    
            if ( $paragraph_id == $index + 1 ) {
                $paragraphs[$index] .= $insertion;
            }
        }
    
        return implode( '', $paragraphs );
    }
    

以上代码在侧边栏模板和无侧边栏模板中均显示广告代码。但实际上我只想在侧边栏模板中显示广告代码。谁能帮我看看怎么弄。

【问题讨论】:

  • 您需要在此处提供更多信息,您应该将您的 single.php 或任何模板上传到 pastebin。侧边栏显示和不显示有什么区别?它们是由小部件等设置的吗?

标签: php wordpress templates


【解决方案1】:

您可以检查get_page_template_slug()使用的模板:

    if ( is_single() && ! is_admin() && get_page_template_slug() == 'template-sidebar.php') {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }

【讨论】:

    【解决方案2】:

    您可以在单个 php 文件中使用此代码

    if (is_page(12)) {
            return prefix_insert_after_paragraph( $ad_code, 2, $content );
        }
    

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多