Injecting Multiple adsense Ads into a WordPress Loop

The versatile code injects for instance adsense ad codes several times into the posts of an index or archive page, starting with a pre-set post, and then repeated in regular intervals (up to a set limit of ads per page).

For example, in a child theme of Twenty Fourteen, this code would be added into content.php at a suitable location:

<?php 
//set the variables//
$nth = 3; //1,2,3,,4...; repeated every n-th post
$snr = 1; //1,2,3...; pre-set start with post nr
$limit = 3; //adsene TOS limit of ads per web page

$lsnr = $snr - 1; // correct for loop counter start at zero

if( $wp_query->current_post >= $lsnr && ( $wp_query->current_post - $lsnr )%$nth == 0 && (int)(( $wp_query->current_post - $lsnr ) / $nth ) < $limit ) { ?>
ADSENSE CODE
<?php } ?>

The example values would start the add in the first post, and repeated every three posts for a maximum of 3 ads.

A Real ‘Full Width Page’ Template for Twenty Fourteen

To create a real full width page – with no sidebars whatsoever – in your WordPress site while using a child theme of the default theme Twenty Fourteen, program this page template file with the file name real-full-width.php (important as this is referenced in the CSS) and save it in a sub-folder /page-templates in the child theme:

<?php
/**
 * Template Name: Real Full Width Page
 *
 * @package WordPress
 * @subpackage Twenty_Fourteen
 * @since Twenty Fourteen 1.0
 */

get_header(); ?>

<div id="main-content" class="main-content">

<?php
	if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
		// Include the featured content template.
		get_template_part( 'featured-content' );
	}
?>

	<div id="primary" class="content-area">
		<div id="content" class="site-content" role="main">
			<?php
				// Start the Loop.
				while ( have_posts() ) : the_post();

					// Include the page content template.
					get_template_part( 'content', 'page' );

					// If comments are open or we have at least one comment, load up the comment template.
					if ( comments_open() || get_comments_number() ) {
						comments_template();
					}
				endwhile;
			?>
		</div><!-- #content -->
	</div><!-- #primary -->
</div><!-- #main-content -->

<?php
get_footer();

Add this section to style.css of the child theme:

.page-template-page-templatesreal-full-width-php .site {
	width: 100%;
}

.page-template-page-templatesreal-full-width-php  .hentry {
	margin: 0 auto 48px;
	max-width: 90%;
}

.page-template-page-templatesreal-full-width-php .site-content .entry-header,
.page-template-page-templatesreal-full-width-php .site-content .entry-content,
.page-template-page-templatesreal-full-width-php .site-content .entry-summary,
.page-template-page-templatesreal-full-width-php .site-content .entry-meta,
.page-template-page-templatesreal-full-width-php .page-content {
	margin: 0 auto;
	max-width: 90%;
}

.page-template-page-templatesreal-full-width-php .main-content,
.page-template-page-templatesreal-full-width-php .content-area {
	float: left;
	width: 100%;
}
.page-template-page-templatesreal-full-width-php .site-content,
.page-template-page-templatesreal-full-width-php .site-main .widecolumn {
	margin-left: 0;
}

.page-template-page-templatesreal-full-width-php .site:before {
	width: 0;
}

.page-template-page-templatesreal-full-width-php .site-content {
	margin-left: 0;
	margin-right: 0;
}

(not optimized)

Activate the template in the ‘Edit Page’ dashboard page, under ‘Page Attributes’ by ticking ‘Real Full Width Page’.

Featured Image in Header of Archive Pages in Twenty Eleven

To show the ‘featured image’ of the top post as header image in an archive page in the Twenty Eleven theme, edit header.php in the child theme;

change this section:

<?php
/*
* The header image.
* Check if this is a post or page, if it has a thumbnail, and if it's a big one
*/
if ( is_singular() && has_post_thumbnail( $post->ID ) &&
( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
$image[1] >= $header_image_width ) :
// Houston, we have a new header image!
echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
else :
// Compatibility with versions of WordPress prior to 3.4.
if ( function_exists( 'get_custom_header' ) ) {
$header_image_width  = get_custom_header()->width;
$header_image_height = get_custom_header()->height;
} else {
$header_image_width  = HEADER_IMAGE_WIDTH;
$header_image_height = HEADER_IMAGE_HEIGHT;
}
?>
<img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
<?php endif; // end check for featured image or standard header ?>

to:

<?php
/*
* The header image.
* Check if this is an archive and if the latest post has a thumbnail, and if it's a big one
* or Check if this is a post or page, if it has a thumbnail, and if it's a big one
*/
if ( is_archive() ) :
the_post();
$archive_thumb = '';
if( has_post_thumbnail( $post->ID ) &&
( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
$image[1] >= $header_image_width ) :
$archive_thumb = get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
endif;
rewind_posts();
endif;
if ( is_archive() && $archive_thumb ) :
echo $archive_thumb; //the featured image of the latest post in the archive
elseif ( is_singular() && has_post_thumbnail( $post->ID ) &&
( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
$image[1] >= $header_image_width ) :
// Houston, we have a new header image!
echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
else :
// Compatibility with versions of WordPress prior to 3.4.
if ( function_exists( 'get_custom_header' ) ) {
$header_image_width  = get_custom_header()->width;
$header_image_height = get_custom_header()->height;
} else {
$header_image_width  = HEADER_IMAGE_WIDTH;
$header_image_height = HEADER_IMAGE_HEIGHT;
}
?>
<img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
<?php endif; // end check for featured image or standard header ?>

Alphabetize Category Posts Using ‘pre_get_posts’ Action

Occasionally you want to have a different sort order in some of the category archives, for example, you want to output the posts of a specific category in alphabetical order.
Instead of having to create a category template for each of those categories and to add a custom query to change the query parameters, WordPress providdes the ‘pre_get_posts’ action where you can add a corresponding function:

function category_archive_sort_alphabetical( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_category( array( 'Category One', 'category-two', 'Another Cat' ) ) ) {
		$query->set( 'orderby', 'title' );
		$query->set( 'order', 'ASC' );
        return;
    }
}
add_action( 'pre_get_posts', 'category_archive_sort_alphabetical', 1 );

resources:
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
http://codex.wordpress.org/Function_Reference/is_category