WordPress插件:Ads by datafeedr.com随机轮播广告

2015年2月2日 | 分类: 【源码】

Ads by datafeedr.com是一个广告随机轮播插件,可以在页面同一个位置可以随机轮播显示你的多个广告。

官网:http://www.datafeedr.com/dfads/

项目:https://wordpress.org/plugins/ads-by-datafeedrcom/

安装:

1.首先,下载并解压缩;

2.将/wp-content/plugins/datafeedr-ads.php上传到WordPress 插件目录wp-content\plugins\

3.将 /wp-content/ads/上传到/wp-content/目录

4.激活插件

5.在ads目录生成txt文件,将你的广告代码写入到txt文件中,多个广告请用”|” 间隔(你可以看它提供的文件范例)

6.在对应的模板位置写入调用代码,例如你的广告txt文件为ads_468x60.txt,则按下述代码写入你需要显示的位置:

<?php if (function_exists('dfrad')) { echo dfrad('ads_468x60'); } ?>

Description

The Ads by datafeedr.com plugin allows you to add advertisements to your blog. You can add advertisements to your posts, pages or Text Widgets via the shortcode. You can also add ads to your site by adding a function to your theme’s template files.

You have full control over how many ads get displayed as well as their sort order.

Add ad types supported: Add Google Adsense, banners, images, text, in-house ads, videos, etc… If you can add it to a Post, you can add it as an ad.

Usage

Shortcode Usage:

[dfads params='groups=3&limit=2']

PHP Function Usage:

<?php echo dfads( 'groups=3&limit=2' ); ?>

Parameters

The following parameters can be passed to either the shortcode or the PHP function.

groups – This specifies the group or groups that ads are pulled from. Enter group IDs (separated by commas) or group slugs (separated by commas). The default value is ‘-1′ which returns ads from all groups.
limit – This limits the number of ads returned. Enter in an positive integer. The default value is ‘-1′ which returns all ads.
orderby – This sets the order in which the ads are displayed. Default is ‘random’. Possible values are:
ID – Orders ads by post ID.
post_title – Orders ads by ad title.
post_date – Orders ads by post date.
post_modified – Orders ads by ad modified date.
menu_order – Orders ads by Menu Order value.
impression_count – Orders ads by Impression Count.
impression_limit – Orders ads by Impression Limit.
start_date – Orders ads by Start Date.
end_date – Orders ads by End Date.
random – Orders ads randomly (default)
order – Sets the order value. Possible values are ‘ASC’ or ‘DESC’. Default value is ‘ASC’.
container_html – The type of HTML tag that will surround the list of ads. Default: div
container_id – The CSS ID of the HTML that surrounds the list of ads. Default: empty.
container_class – The CSS Class of the HTML that surrounds the list of ads. Default: empty.
ad_html – The type of HTML tag that will surround each ad. Default: div
ad_class – The CSS Class of the HTML that surrounds a single ad. Default: empty.
callback_function – (advanced) A callback function to handle ad display with your own function. Default: empty.
return_javascript – (advanced) Returns ads via a JavaScript call. This is useful when you are using a caching plugin and you still want random sorting and impression counts updated. Default: false.

Example 1

[dfads]

This displays all of your ads from all groups randomly sorted. Example of HTML output:

<div>
	<div class="dfad dfad_pos_1" id="_ad_7">
		Ad Content
	</div>
	<div class="dfad dfad_pos_2" id="_ad_4">
		Ad Content
	</div>
	<div class="dfad dfad_pos_3" id="_ad_10">
		Ad Content
	</div>
	<div class="dfad dfad_pos_4" id="_ad_15">
		Ad Content
	</div>
</div>

Example 2

[dfads params='groups=2&limit=2&orderby=impression_count&order=ASC']

This displays ads from group ID 2, limit the number of ads to 2, ordered by impression count ascending. Example of HTML output:

<div>
	<div class="dfad dfad_pos_1" id="_ad_4">
		Ad Content
	</div>
	<div class="dfad dfad_pos_2" id="_ad_7">
		Ad Content
	</div>
</div>

Example 3

[dfads params='container_html=ul&container_id=adList&container_class=sidebar-ads&ad_html=li&ad_class=list-item-ad']

This displays all ads from all groups in an unordered list (

    ) with each ad as a list item (

  • ). The list has a custom CSS class and ID and each ad has a custom CSS ID. Example of HTML output:
    <ul class="sidebar-ads" id="adList">
    	<li class="dfad dfad_pos_1 list-item-ad" id="adList_ad_15">Ad Content</a></li>
    	<li class="dfad dfad_pos_2 list-item-ad" id="adList_ad_7">Ad Content</a></li>
    	<li class="dfad dfad_pos_3 list-item-ad" id="adList_ad_10">Ad Content</a></li>
    	<li class="dfad dfad_pos_4 list-item-ad" id="adList_ad_4">Ad Content</a></li>
    </ul>
    

    Override the Output

    If you want to have complete control over the layout of your ads, you can provide your own callback function like this:

    <?php echo dfads( 'groups=3&limit=2&callback_function=myCallback' ); ?>
    

    Then if you have a function named myCallback, the plugin will pass your function 2 variables: $ads and $args. You function must accept these 2 arguments like this:

    function myCallback ($ads, $args) {
    	// Your code here...
    }
    

    $ads is an array containing all of the ads returned as $post objects. Example of what is returned:

    Array
    (
        [0] => stdClass Object
            (
                [ID] => 74
                [post_author] => 1
                [post_date] => 2013-03-15 18:47:20
                [post_date_gmt] => 2013-03-15 18:47:20
                [post_content] => HTML CONTENT
                [post_title] => AD TITLE
                [post_excerpt] => 
                [post_status] => publish
                [comment_status] => closed
                [ping_status] => closed
                [post_password] => 
                [post_name] => delirium-ad
                [to_ping] => 
                [pinged] => 
                [post_modified] => 2013-03-15 18:47:20
                [post_modified_gmt] => 2013-03-15 18:47:20
                [post_content_filtered] => 
                [post_parent] => 0
                [guid] => URL
                [menu_order] => 0
                [post_type] => dfads
                [post_mime_type] => 
                [comment_count] => 0
                [ad_imp_count] => 9
                [ad_imp_limit] => 50000
                [ad_start_date] => 1362096000
                [ad_end_date] => 1388448000
            )
    
        [1] => stdClass Object ( ... )
        [2] => stdClass Object ( ... )
        [3] => stdClass Object ( ... )
        [4] => stdClass Object ( ... )
    
    )
    

    $args is an array that contains the custom and default parameters passed to the dfads function (or shortcode). Example of what is returned:

    Array
    (
        [groups] => 2
        [limit] => 3
        [orderby] => random
        [order] => ASC
        [container_id] => 
        [container_html] => div
        [container_class] => 
        [ad_html] => div
        [ad_class] => my_custom_class
        [callback_function] => myOutput
        [return_javascript] => 
    )
    

    The default output( $ads, $args ) can be found here: /wp-content/plugins/dfads/inc/dfads.class.php. Your function must return HTML, not echo or print HTML. As of this writing, it currently looks like this:

    function output( $ads, $args ) {
    	
    	$ad_count = count( $ads );
    	$i = 1;
    	$html = '';
    	
    	// Determine if we should include tags for containers and ad wrappers.
    	// If 'none', then we remove the tag from output.
    	$container_html = ( $args['container_html'] == 'none' ) ? '' : $args['container_html'];
    	$ad_html = ( $args['ad_html'] == 'none' ) ? '' : $args['ad_html'];
    	
    	// If contain_html is not empty, get container's opening tag.
    	if ( $container_html != '') {
    		$html .= $this->open_tag( $container_html, $args['container_class'], $args['container_id'] );
    	}
    	
    	// Loop through ads.
    	foreach ($ads as $ad) {
    	
    		$first_last = ' ';
    		if ( $i == 1 ) {
    			$first_last = ' dfad_first ';
    		} elseif ( $i == $ad_count ) {
    			$first_last = ' dfad_last ';
    		}
    		
    		// If ad_html is not empty, get the ads's opening tag.
    		if ( $ad_html != '') {
    			$html .= $this->open_tag( 
    				$ad_html, 
    				'dfad dfad_pos_'.$i.$first_last.$args['ad_class'], 
    				$args['container_id'].'_ad_'.$ad->ID 
    			);
    		}
    		
    		// Get ad content.
    		$html .= $ad->post_content;
    		
    		// If ad_html is not empty, get the ads's closing tag.
    		if ( $ad_html != '') {
    			$html .= $this->close_tag( $ad_html );
    		}
    		
    		$i++;
    	}
    	
    	// If contain_html is not empty, get container's closing tag.
    	if ( $container_html != '') {
    		$html .= $this->close_tag( $container_html );
    	}
    	
    	return $html;
    }