ShuDudu's Home was started in 2011, but the web data is lost, so now begin again, I would like to make some friends, I hope you like ShuDudu's home.
Current position: ShuDudu > Net Web >

Placing ads on Typecho Summary

Sunday on April 26th, 2020Net Web

As we all know, this thing has always been a personal blog throwing money do not make money dry, Xiangnong some advertising revenue subsidy, the user experience will plummet. This article will attempt to add advertising in the summary line of Typecho.

Abstract lines and advertising

In Typecho, the summary line is -! More -, page source code, you can see this line in HTML comment out there, so the purpose of this article is to try to place ads summary line below.

About ad position, I consider a lot of places, most beginning on the copyright notice below, but the Google ad network is a thief, ah, you do not put onto the page below, he is not a show, so my day on average no amount of 100 impressions of it. But with Google's automated ad, which I do not like the location of the election. For example, a page opens article put in front of an advertisement, which of course helps to return, but I personally think that this position is too aggressive, and people came to see the point of the article, you give people to put ads, people feel too the emphasis on profit. Other locations, such as the middle of the article inserted in my opinion is totally unacceptable, after all, a whole article or the interposition of a very ad affect the reading experience. For now, the site is not all articles are the digest line advertising. The original is in the "Fan 1080P drama completion plan", "casually write" at "reading" classification, that I am very concerned about the article reading experience, I have turned off the ads. After all, I still feel my personal blog or in the living mainly profit or second place.

But there is a line to say a CN2 is really expensive, otherwise I will not have to be subsidized by advertising point home.

About ad network, I chose Google AdSense, other ad networks should also, if relatively wild, then select TrafficJunky may well be a way that may CTR R18 will be higher, income is also higher, anyway, I was not hungry return to such a degree, it is Google good.

About ad type, ad unit type I used in the copyright office is "display advertising," he is more suitable in a self-contained box. The intermediate article of this type is less line, I use is "embedded advertising articles" on the PC more fitting number, the phone Well, I am now the phone is broken, the epidemic so much noise and I could not leave, buy spare machine and has not been shipped, it remains to be seen. But given the size of the phone screen, I think the phone side of these two very different ads.

Plug-storage settings

In order to facilitate future replace the ad code, I decided not to write tags died in the PHP page, which is on the one hand, on the other hand I am not familiar with PHP, multi-line strings I will not get the last change over the whole of file looks very elegant. So in the end I decided to use a plug-what is not dry, special storage tags.

Please /usr/plugins/AdSense/Plugin.php written in the root directory of the site following which path where AdSense is a plug-in name, you can customize, but to ensure that the content and the following:

? Php/** * AdSense * * @package AdSense * @author sky Blond * @version 1.0.0 * @link https://skyblond.info */class AdSense_Plugin implements Typecho_Plugin_Interface {/ * activate the plugin method */public static function activate () {}/* disable widget method */public static function deactivate () {}/* widget configuration */public static function config (Typecho_Widget_Helper_Form $ form) {$ ad = new Typecho_Widget_Helper_Form_Element_Textarea ( 'adsenseCode', NULL, ' ', _t (' AdSense Code '), _t (' recommended articles embedded ad units')); $ form-addInput ($ ad);}/* individual users to configure */public static function personalConfig (Typecho_Widget_Helper_Form $ form) {}/* widget implementation */public static function render () {}}?

This template comes from Typecho plugin development documentation regarding changes to the above code, you can also refer to the document.

The above code do is register a plug-in set, called adsenseCode, you can enter multiple lines of text. After by Typecho_Widget :: widget ( 'Widget_Options') - plugin ( 'AdSense') - adsenseCode to call the value stored there.

Replace the summary line and display ads.

This part would have been like to use plug-ins to achieve the, but the plug-mentioned document does not seem to have an interface for the control content of the article. Here it is displayed directly responsible for the file by modifying the article to be reached. In the theme I'm using, the file is responsible for displaying the web page file /usr/themes/Mirages/post.php root directory, which is responsible for displaying the echo Content :: parse ($ this-content); this one, we need modify this sentence him to show ads.

$ This-content will include -! More--, where you can use PHP's own string replacement functions to achieve, and in order to make whether to display the summary line advertising controllable, you can use custom fields to control the article. Here I judge whether the value hideAds field is true, it is the hidden advertising, otherwise the default display.

Note that if only the replacement -! More -, there may dispose of the contents of the article, the practice of summary dividing line must be in the paragraph (p/p), so you can connect together as a judge/p, replaced.

Eventually replace code is as follows:

? Php if (Utils :: isTrue ($ this-fields-hideAds)) {echo Content :: parse ($ this-content);} else {echo str_replace (/ p - more -, '/ p -!! - more --br/'. Typecho_Widget :: widget (' Widget_Options') - plugin ( 'AdSense') - br/', Content :: parse ($ this-content)) adsenseCode.';}?

Utils :: isTrue which is the subject comes with a function I use, which is implemented as follows:

public static function isTrue ($ field, $ key = NULL) {if (is_array ($ field) empty ($ key)!) {return in_array ($ key, $ field);} return $ field0 || strtolower ($ field) == 'true';}

In fact, as long as the last return statement on the line.

Finally, save the file, remember to fill in the correct tag in the settings, it should be on it. To close the summary line advertising, custom field may be set, and hideAds name is set true value or an integer greater than 0.

If you want hidden by default ad, you can use showAds to be judged, and reversed field if-else code above.

Copyright Protection: ShuDudu from the original article, reproduced Please keep the link: https://www.shududu.com/netweb/Placing-ads-on-Typecho-Summary.htm