I'm having trouble being able to insert an ad after every 3rd post in Wordpress. This is the code in the theme. I know I need a counter and an if statement.
<section id="recentnews">
<div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
<?php $args = array(
'posts_per_page' => '5',
'offset' => '5',
'ignore_sticky_posts' => '1'
);
?>
<?php $query = new WP_Query($args); ?>
<?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
<article class="post">
<div class="row">
<div class="five columns">
<div class="post-gallery">
<?php the_post_thumbnail('recent'); ?>
<?php echo thb_DisplayImageTag(get_the_ID()); ?>
</div>
</div>
<div class="seven columns">
<div class="post-title">
<aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
<h2><?php the_title(); ?></h2>
</div>
<div class="post-content">
<p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
<?php echo thb_DisplayPostMeta(true,true,true,false); ?>
</div>
</div>
</div>
</article>
<?php endwhile; else: ?>
<article>
<?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
</article>
<?php endif; ?>
<a id="loadmore" href="#" data-loading="<?php _e( 'Loading ...', THB_THEME_NAME ); ?>" data-nomore="<?php _e( 'No More Posts to Show', THB_THEME_NAME ); ?>" data-count="5" data-action="thb_ajax_home"><?php _e( 'Load More', THB_THEME_NAME ); ?></a>
</section>
I could show you what I tried and failed at, just to show that I tried. Don't give me negative points for that please.
<section id="recentnews">
<div class="headline"><h2><?php _e( 'Recent News', THB_THEME_NAME ); ?></h2></div>
<?php $args = array(
'posts_per_page' => '5',
'offset' => '5',
'ignore_sticky_posts' => '1'
);
?>
<?php $query = new WP_Query($args); ?>
<?php $i = 1; if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); if($i == 1) : ?>
<article class="post">
<div class="row">
<div class="five columns">
<div class="post-gallery">
<?php the_post_thumbnail('recent'); ?>
<?php echo thb_DisplayImageTag(get_the_ID()); ?>
</div>
</div>
<div class="seven columns">
<div class="post-title">
<aside><?php echo thb_DisplaySingleCategory(false); ?></aside>
<h2><?php the_title(); ?></h2>
</div>
<div class="post-content">
<p><?php echo ShortenText(get_the_excerpt(), 150); ?></p>
<?php echo thb_DisplayPostMeta(true,true,true,false); ?>
</div>
</div>
</div>
</article>
<div class="clear"> </div>
<?php if ( $i == 3|| $i == 9 || $i == 15 ) : ?>
<?php if (function_exists ('adinserter')) echo adinserter (2); ?>
<div class="clear"> </div>
<?php endif; ?><?php endif; ?>
<?php $i++; ?>
<?php endwhile; else: ?>
<article>
<?php _e( 'Please select tags from your Theme Options Page', THB_THEME_NAME ); ?>
</article>
<?php endif; ?>
</section>
Thank you.
You're close. You'll want to increment your counter within the while loop (using $i++), and then use the modulus operator % to determine whether or not the counter is evenly divisible by 3:
<?php if ($query->have_posts()) : $i = 1; while ($query->have_posts()) : $query->the_post(); ?>
<!-- Article -->
<?php if ( $i % 3 == 0 ) : ?>
<!-- Advertisement Here -->
<?php endif; ?>
<?php $i++; endwhile; else: ?>
<!-- Display Notice -->
<?php endif; ?>
Related
I am trying to paginate archived post in WordPress when I click "next button" but it doesn't work. Please guide me to do it in a simple way. My code is given below:
<div class="grid-wrapper ">
<?php $args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="inner-blog">
<div class="row">
<div class="col-lg-4 col-md-2">
<div class="post-thumbnail">
<img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
</div>
</div>
<div class="col-lg-8 col-md-9">
<h2 class="post-title"><?php the_title();?></h2>
<?php the_excerpt(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php wp_reset_postdata(); ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $the_query->max_num_pages ); ?> ยป</li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
</div>
</div>
I need to display all posts on the main page that will vary. That is, they will not repeat on the same page. I display them using WP_Query, but for each column of posts I need to have another 'posts_per_page'. How to do it?
I trying this:
<?php $my_query = new WP_Query($args); ?>
and
this <?php $my_query->set('posts_per_page', 3);
$my_query->query($my_query->query_vars); ?>
Here is my code example one:
<div class="row">
<div class="col-xl-9 col-lg-9 col-md-12 featured-posts">
<?php $my_query->set('posts_per_page', 3); $my_query->query($my_query->query_vars); ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<article class="post post-id-<?php echo the_ID(); ?>">
<a class="post-thumbnail" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
<div class="post-content">
<h3 class="post-title"><?php the_title(); ?></h3>
<?php healthybroom_posted_on(); ?>
<?php healthybroom_entry_footer(); ?>
<p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
</div>
</article>
<?php endwhile; ?>
</div>
<div class="col-xl-3 col-lg-3 col-md-12 front-random-posts">
<?php $my_query->set('posts_per_page', 5); $my_query->query($my_query->query_vars); ?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<article class="post post-id-<?php echo the_ID(); ?>">
<a class="post-thumbnail" href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail(); ?></a>
<div class="post-content">
<?php healthybroom_entry_footer(); ?>
<h3 class="post-title"><?php the_title(); ?></h3>
<?php healthybroom_posted_on(); ?>
</div>
</article>
<?php endwhile; ?>
</div>
</div>
I want the posts to not be repeated simply.
If you have 2 loops with random posts and you want to avoid to have same posts in the 2 loops, you can use the following code :
<!-- We create an array for saving the IDs of the post displaying in the first loop -->
<?php $store_posts_ids = array(); ?>
<div class="row">
<div class="col-xl-9 col-lg-9 col-md-12 featured-posts">
<?php
$my_query->set('posts_per_page', 3);
$my_query->query($my_query->query_vars);
?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<!-- We store the post id for the second loop -->
<?php array_push( $store_posts_ids, get_the_ID() ); ?>
<!-- Your code here -->
<?php endwhile; ?>
</div>
<div class="col-xl-3 col-lg-3 col-md-12 front-random-posts">
<?php
/*
If you want to display 5 posts, you need to have a loop
of 7 posts because you have 3 posts in your first loop
*/
$number_post = 0;
$my_query->set('posts_per_page', 5);
$my_query->query($my_query->query_vars);
?>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<?php array( $store_posts_ids, get_the_ID() ); ?>
<!-- If we have our 5 posts, no need to display more posts -->
<?php if( $number_post < 5 ): ?>
<!-- If the post is not in the first loop -->
<?php if( !in_array( $store_posts_ids, get_the_ID() ) ): ?>
<?php $number_post = $number_post + 1; ?>
<!-- Your code here -->
<?php endif; ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
</div>
I am trying to have the layout of my posts on my search results be 2 posts per line. Right now there is 1 post, so I am trying to add in 'col-sm-6 col-md-6' to my search.php file. However, every time I try to add it in nothing changes. Does anyone have any solutions?
search.php
<?php get_header(); ?>
<div id="page">
<div class="column span-9 first" id="maincontent">
<div class="content">
<?php if (have_posts()) : ?>
<form role="search" method="get" class="search-form-search form-inline-search" action="">
<div class="input-group-search">
<input type="search" value="" name="s" class="input-sm-search search-field-search form-control-search" placeholder="<?php echo $s ?>">
</div>
</form>
<div class="clear"></div>
<?php while (have_posts()) : the_post(); ?>
<div class="search-post" id="post-<?php the_ID(); ?>">
<?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?>
<div class="front-page-date"><?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
<div class="front-page-post-info">
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part( 'front-shop-the-post' ); ?>
<?php get_template_part( 'share-buttons' ); ?>
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<h2 class="center">No posts found. Try a different search?</h2>
<?php include (TEMPLATEPATH . '/searchform.php'); ?>
<?php endif; ?>
</div> <!-- /content -->
</div> <!-- /maincontent-->
</div> <!-- /page -->
<?php get_footer(); ?>
Here is another version of a search.php I have been playing around with. However, with this one the web page doesn't show up.
<?php
get_header();
$the_query = new WP_Query( [
'posts_per_page' => 15,
'paged' => get_query_var('paged', 1)
] );
if ( $the_query->have_posts() ) { ?>
<div id="ajax">
<?php
while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
<div class="row">
<article <?php post_class( 'col-sm-6 col-md-6' ); ?>>
<div class="large-front-container">
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?>
</div>
<div class="front-page-date"><?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?></div>
<h2><a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
<div class="front-page-post-info">
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part( 'front-shop-the-post' ); ?>
<?php get_template_part( 'share-buttons' ); ?>
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
</div>
</article>
</div>
<?php if(get_query_var('paged') < $the_query->max_num_pages) {
}
}
elseif (!get_query_var('paged') || get_query_var('paged') == '1') {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
wp_reset_postdata();
get_footer();
I'm working on a Wordpress site. I'm trying to make a category with the id 968 show up on the Wordpress site under the stories and successes div. However for some reason the stories and successes div is not having the content from the category inserted into it. Any information on how I can improve my query or PHP is much appreciated. If I need to update this post with more information please let me know. Below you will see the php template file I am using.
<?php
/**
* Template name: Random Template
*
*
*/
// Custom styles
wp_enqueue_style('stories-styles', get_bloginfo('stylesheet_directory') . '/assets/css/stories.css');
// TEMP -- Get the category from the slug
the_post();
$categorySlugs = array(
'religious-freedom' => 'religious-freedom',
'social-and-economic-justice' => 'social-and-economic-justice',
'civil-and-human-rights' => 'civil-and-human-rights'
);
// Get category
$categorySlugs = $categorySlugs[$post->post_name];
$storyQuery = new WP_Query(array(
'post_type' => array('stories', 'press-releases'),
'category_name' => $categorySlugs,
'posts_per_page' => 4,
'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));
$successesQuery = new WP_Query(array(
'post_type' => 'stories',
'cat' => 968,
'posts_per_page' => 1,
'offset' => 5
));
// Remove yarpp
remove_filter('the_content', array($yarpp, 'the_content'), 1200);
?>
<?php get_header(); ?>
<div class="category-title">
<div class="container">
<h2><?php the_title() ?></h2>
</div>
</div>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<div class="container">
<div class="row">
<div class="col-md-12">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
<article class="featured-issue-article">
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="entry-thumbnail">
<?php the_post_thumbnail('full', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
</div>
<?php endif; ?>
<div class="content">
<?php the_content() ?>
</div>
</article>
</div>
</div>
<hr class="large blue" />
<div class="row">
<div class="col-md-8">
<ul class="section-list">
<?php while ($storyQuery->have_posts()) : $storyQuery->the_post(); ?>
<li>
<h3><?php the_title() ?></h3>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('post-thumbnail', array( 'class' => "img-responsive attachment-post-thumbnail")); ?>
<?php endif; ?>
<?php if( get_field('custom_excerpt', $values_id)): ?>
<p><?php echo the_field('custom_excerpt'); ?></p>
<?php endif; ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
<?php
set_query_var('category_slug', $categorySlug);
get_template_part('issues-nav', 'bottom')
?>
<hr class="small blue" />
<div class="success-stories">
<h2>Stories & Successes</h2>
<ul>
<?php while ($successesQuery->have_posts()) : $successesQuery->the_post(); ?>
<li>
<h4>
<?php the_title() ?>
</h4>
<?= strip_tags($post->post_excerpt) ?>
<a class="more" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More...</a>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
<div class="col-md-4">
<?php get_sidebar(); ?>
</div>
</div>
</div>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
In my theme I have a blog page and a homepage (without blog loop). Now I would like to add some blog posts on my homepage (like most recent).
I am using the blog page as a template for the homepage ( I copied and renamed the original), wich works good. But I can't make any changes to it without changing the original blog page (the loop is called in an external php).
I Have no idea were to start.
This is the piece on the homepage calling the blog loop:
<?php if(have_posts()) : the_post(); ?>
<div id="main">
<div class="central-wrapper clearfix">
<div id="center" class="fullwidth">
<div id="content">
<?php if($gallery_select && $gallery_position == 1) : ?>
<?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
<?php endif; ?>
<div class="inner">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php codeus_blog_list(); ?>
</div>
<?php if($quickfinder_position == 2) : ?>
<?php codeus_quickfinder($quickfinder_select); ?>
<?php endif; ?>
<?php if($portfolio_position == 2) : ?>
<?php codeus_portfolio(implode(',',$portfolio_select), $portfolio_size, $portfolio_count, $portfolio_filter, $portfolio_title); ?>
<?php endif; ?>
<?php if($gallery_select && $gallery_position == 3) : ?>
<?php codeus_gallery($gallery_select, $gallery_size, $gallery_style); ?>
<?php endif; ?>
</div>
</div><!-- #content -->
</div><!-- #center -->
</div><!-- .central-wrapper -->
</div><!-- #main -->
<?php endif; ?>
</div><!-- wrap end -->
And this is the piece in the external php called blog.php
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
<ul class="styled">
<?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
<li class="clearfix">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
<div class="comment-info">
<?php echo get_the_date('d'); ?>
<div class="date-month"><?php echo get_the_date('M'); ?></div>
</div>
<div class="post-info">
<h3><?php the_title(); ?></h3>
<?php if($image_url[0]) : ?>
<div class="post-image">
<div class="image wrap-box shadow middle">
<div class="shadow-left"></div><div class="shadow-right"> </div>
<img src=" <?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endif; ?>
<div class="text clearfix"><?php the_excerpt(); ?></div>
<?php codeus_author_info(get_the_ID()); ?>
</div>
</div>
</li>
<?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
<?php endwhile; ?>
</ul>
<?php codeus_pagination($page_num, $pages_count); ?>
</div>
<?php endif; ?>
Remember, I don't want to make changes in the original blog.php, because the normal blog page needs to work as it is now. I would like to make the blog work on the homepage also, but just with a maximum of 3 posts showing, perhaps of a certain category.
Help would be so welcome!!
I think I found a solution, I edited the homepage template like this:
<!-- wrap start --><div class="content-wrap">
<?php if(have_posts()) : the_post(); ?>
<div id="main">
<div class="central-wrapper clearfix">
<div id="center" class="fullwidth">
<div id="content">
<div class="inner">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="pagination"><div class="page-links-title">' . __( 'Pages:', 'codeus' ) . '</div>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<div class="bloghome">
<?php
$page_num = (get_query_var('paged')) ? get_query_var('paged') : 1;
$items_per_page = 2;
$blog_loop = new WP_Query(array(
'post_type' => 'post',
'posts_per_page' => $items_per_page,
'paged' => $page_num
));
$pages_count = ceil($blog_loop->found_posts/$items_per_page);
global $post;
$portfolio_posttemp = $post;
?>
<?php if($blog_loop->have_posts()) : ?>
<div class="blog_list">
<ul class="styled">
<?php while ( $blog_loop->have_posts() ) : $blog_loop->the_post(); ?>
<li class="clearfix">
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php $image_url = wp_get_attachment_image_src(get_post_thumbnail_id(), 'codeus_post_list_image'); ?>
<div class="comment-info">
<?php echo get_the_date('d'); ?>
<div class="date-month"><?php echo get_the_date('M'); ?></div>
</div>
<div class="post-info">
<h3><?php the_title(); ?></h3>
<?php if($image_url[0]) : ?>
<div class="post-image">
<div class="image wrap-box shadow middle">
<div class="shadow-left"></div><div class="shadow-right"></div>
<img src="<?php echo $image_url[0]; ?>" alt="<?php the_title(); ?>" />
</div>
</div>
<?php endif; ?>
<div class="text clearfix"><?php the_excerpt(); ?></div>
<?php codeus_author_info(get_the_ID()); ?>
</div>
</div>
</li>
<?php $post = $portfolio_posttemp; wp_reset_postdata(); ?>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
</div>
</div>
</div><!-- #content -->
</div><!-- #center -->
</div><!-- .central-wrapper -->
</div><!-- #main -->
<?php endif; ?>
</div><!-- wrap end -->