Add banner after the third post in wordpress 4.0 - wordpress

How do I add a banner or a DIV with content after the third post on the index or category of Wordpress 4.0?
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>

You can also make us of the inbuilt current_post property that is populated in WP_Query so it looks like:
// Start the Loop.
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
//count starts at 0
if( $current_post == 2 ) :
echo '<div class="banner"><img src="your_banner_url" /></div>';
endif;
endwhile;

$counter = 1;
// Start the Loop.
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
if( $counter == 3 )
echo '<div class="banner"><img src="your_banner_url" /></div>';
$counter++;
endwhile;

Related

Wordpress custom loop in template part

I'm using a theme for some Wordpress-integrated software which means that I am basically stuck with the template hierarchy, otherwise I'd just set up a custom template to get around this.
Anyway, I have my page.php, which looks a little like the following
<?php //start the main loop
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<?php
if ( is_page('contact-us') ) {
get_template_part( 'content', 'contact' );
}else{
get_template_part( 'content', 'page' );
}
?>
<?php //end the main loop
endwhile;
else:
?>
Something is missing
<?php
endif;
?>
This works fine, and as expected, I am able to add html within content-page.php
However, I would like to add a custom loop within content-page.php to display customer testimonials. I've attempted this with the code below inside content-page.php:
<?php //close the main loop
endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php
//close the main loop and open a custom loop
wp_reset_postdata();
$args = array (
'post_type' => 'testimonial',
'posts_per_page' => 5,
'orderby' => 'rand'
);
$the_query = new WP_Query ( $args );
if ( have_posts() ) : while ( $the_query ->have_posts() ) : $the_query ->the_post(); ?>
Do HTML stuff here
<?php //close the custom loop
endwhile; else: ?>
Uh oh, there is meant to be a testimonial here. Please create some.
<?php endif; ?>
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
This code creates a PHP error (unexpected endwhile where I attempt to close the main loop). However, I put this exact same code straight inside page.php, it works. It only errors when inside content-page.php. Am I not able to include custom loops with get_template_part?
the last endwhile and endif are not closed
<?php //re-open the main loop
wp_reset_postdata();
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
endwhile;
endif;
?>

My WordPress loop for posts fails

i use that code to post an ad in my homepage every 3 posts but instead of that the ad appears behind the 1st post and it seems like it doesnt recognise the loop at all
<div id="mason-layout" class="transitions-enabled fluid">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'blog' ); ?>
<?php if ($i == 3) { ?> ADHERE <?php } ?>
<?php $i++; ?>
<?php endwhile; ?>
any ideas?
thanks
You're missing part of your loop.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
Edit: If you want to break the if statement onto multiple lines you'll probably want to use a different syntax. Eg:
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
Recommended reading: http://codex.wordpress.org/The_Loop

In Wordpress, how do I get a child page to use it's page template?

I have a child page with the slug /en/work/ which I want to use a page template found by slug. I've uploaded both page-work.php and page-en-work.php but when I navigate to the page none of them is used.
Have a look at the following link
http://codex.wordpress.org/Function_Reference/get_template_part
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
And on the custom page template ("my_child.php") include the file like so:
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'my_child' ); ?>
<?php endwhile; // end of the loop. ?>
Now you can include the get_template_part on other pages as well like so:
<?php
global $wp_query;
// is Page a parent page
if ( $post->post_parent == 0 ) {
// on a parent page, get child pages
$pages = get_pages( 'hierarchical=0&parent=' . $post->ID );
// loop through child pages
foreach ( $pages as $post ){
setup_postdata( $post );
// get the template name for the child page
$template_name = get_post_meta( $post->ID, '_wp_page_template', true );
$template_name = ( 'default' == $template_name ) ? 'page.php' : $template_name;
// default page template_part content-page.php
$slug = 'page';
// check if the slug exists for the child page
if ( locate_template( 'content-' . basename( $template_name ) , $load, $require_once ) != '' ) {
$slug = pathinfo( $template_name, PATHINFO_FILENAME );
}
// load the content template for the child page
get_template_part( 'content', $slug );
}
}
?>

Why won't my wordpress query work? Custom post types

I can't get this to show posts from my custom post type named "resources." Any help?
<?php
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
function add_custom_post_type_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'page', 'resources' ) );
return $query;
}
?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
This part should go into functions.php :
<?php
add_action( 'pre_get_posts', 'add_custom_post_type_to_query' );
function add_custom_post_type_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'page', 'resources' ) );
return $query;
}
?>
while this part is in template file :
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentytwelve_content_nav( 'nav-below' ); ?>
<?php else : ?>
Explanation :
The 1st part is activating a filter / action on pre_get_posts() . but when you put it inside a theme file or template file , the filter will fire too late or not fire at all.
Your functions.php file in a theme is the first one to be parsed, and therefore it should contain all the functions that are not DIRECTLY related to rendering or visualization of the page .
The loop however, which is a rendering mechanism, should be in the appropriate theme file.

wordpress wp query posts per page default

Here is the code:
<?php if ( have_posts() ) : ?>
<?php
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query- >the_post();
?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php higher_content_nav( 'nav-below' ); ?>
<?php $wp_query = null; $wp_query = $temp;?>
<?php endif; ?>
How can I show posts per page not 5 but default? I mean post that we can set at back end at Reading Settings, Blog pages show at most - number of post.
The safest way is to call get_option, which gets the value directly from the database:
$showposts = get_option('posts_per_page');
$wp_query->query('showposts='.$showposts.'&paged='.$paged);
Global variables like $numposts are not guaranteed to be set.
For future reference, you can usually determine what to pass to get_option by finding the name attribute of the setting's <input> in WP Admin.
<?php query_posts( 'posts_per_page=5' ); ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php endif; ?>
You are using WP_Query(), it does not have any default value for the number of posts per page.
You should however note that showposts has been replaced by posts_per_page in your code:
$wp_query->query('showposts=5'.'&paged='.$paged);
Here is the solution:
global $numposts;
$wp_query->query( 'showposts='.$numposts.'&paged='.$paged );

Resources