wordpress插入代码_如何在WordPress的帖子内容中插入广告

wordpress插入代码

Have you seen blogs that has ads inside their post content? These ads are either after the first paragraph or the second paragraph in most cases. Most beginners wonder whether these sites insert ads manually when they write the content, or if there is a special code for this. In this article, we will show you how to insert ads within your post content after specific paragraphs.

您是否看到过在帖子内容中包含广告的博客? 在大多数情况下,这些广告位于第一段或第二段之后。 大多数初学者想知道这些网站在编写内容时是否手动插入广告,或者是否有特殊的代码。 在本文中,我们将向您展示如何在特定段落之后在帖子内容中插入广告。

Often when beginners want to include ads in their post, they either add the code manually which is extremely inefficient specially if you have to change advertisers, or they insert ads above or below their post using one of the ad management plugins.

通常,当初学者想要在其帖子中添加广告时,他们要么手动添加代码,这特别是在您必须更换广告商的情况下效率极低,或者使用一个广告管理插件在其帖子的上方或下方插入广告。

Well, we’ve been adding ads in List25 after first paragraphs, and a lot of users asked about it, so here’s the easiest way to do it.

好吧,我们一直在第一段之后在List25中添加广告,并且很多用户对此都提出了要求,因此这是最简单的方法。

Simply install and activate the plugin we created called Insert Post Ads. Upon activation, the plugin will add a new menu item called Post Adverts.

只需安装并激活我们创建的名为Insert Post Ads的插件。 激活后,插件将添加一个名为Post Adverts的新菜单项。

Click on Post Adverts » Add New. On the next screen, simply add the name of your ad for identification purposes, enter the ad code itself, and select the paragraph you want to display the ad code after. Once done simply click publish.

点击发布广告»添加新内容 。 在下一个屏幕上,只需添加广告名称以供识别,输入广告代码本身,然后选择要在其后显示广告代码的段落。 完成后,只需单击发布。

Insert Post Ads

Next you want to go to Post Adverts » Settings to select which post types you want to show your ads on such as posts, pages, and custom post types.

接下来,您要转到发布广告»设置 ,选择要在其上展示广告的帖子类型,例如帖子,页面和自定义帖子类型。

Now if you don’t like using the plugin, and want to do it the code way, then follow the directions below.

现在,如果您不喜欢使用该插件,并希望以代码方式进行操作,请按照以下说明进行操作。

Open your theme’s functions.php or a site-specific plugin file and paste the following code:

打开您主题的functions.php或特定站点的插件文件,然后粘贴以下代码:


<?php
 
//Insert ads after second paragraph of single post content.

add_filter( 'the_content', 'prefix_insert_post_ads' );

function prefix_insert_post_ads( $content ) {
	
	$ad_code = '<div>Ads code goes here</div>';

	if ( is_single() && ! is_admin() ) {
		return prefix_insert_after_paragraph( $ad_code, 2, $content );
	}
	
	return $content;
}
 
// Parent Function that makes the magic happen
 
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 );
}

To add your ad code, simply edit $ad_code value where it says “Ad code goes here” on line 9. Once you do that, you are done. To change the paragraph number, simply change the number 2 to another paragraph number on line 12.

要添加广告代码,只需编辑$ ad_code值(第9行的“广告代码在此处”),即可。 要更改段落编号,只需将数字2更改为第12行上的另一个段落编号。

We hope that this article helped you insert ads within your post content in WordPress.

我们希望本文能帮助您在WordPress的帖子内容中插入广告。

If you liked this article, then please consider subscribing to our YouTube channel and join us on Twitter.

如果您喜欢这篇文章,请考虑订阅我们的YouTube频道,在Twitter上加入我们

Thanks to @GaryJ for improving the code that we had.

感谢@GaryJ改进了我们拥有的代码。

翻译自: https://www.wpbeginner.com/wp-tutorials/how-to-insert-ads-within-your-post-content-in-wordpress/

wordpress插入代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值