how to add ajax load more repeater template - php

I'm trying to add a load more button to my front page. I have my posts set up so that I have one post in a row, and then 3 posts in a row, another 3 posts in a row, and then 1,3,3,etc. I want to add the load more button after 14 posts. I've added the short code however now when I press the load more button the posts appear all wonky. I have figured out its because I never customized my repeater code. However, I can't figure out how to properly add my template to the repeater code since I had a lot of help writing the code for my front-page.php. If anyone can help me out I would really appreciate it.
The plugin I am using is - https://wordpress.org/plugins/ajax-load-more/faq/
my front-page.php
<?php
/*
* Template Name:
*/
get_header();
get_template_part ('inc/carousel');
$the_query = new WP_Query( [
'posts_per_page' => 14,
'paged' => get_query_var('paged', 1)
] );
if ( $the_query->have_posts() ) { ?>
<?php
$i = 0;
while ( $the_query->have_posts() ) { $the_query->the_post();
if ( $i % 7 === 0 ) { // Large post: on the first iteration and every 7th post after... ?>
<article <?php post_class( 'col-sm-12 col-md-12' ); ?>>
<div class="large-front-container">
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?>
</div>
<h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part ('front-page-shop'); ?>
<?php get_template_part( 'share-buttons' ); ?>
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
</article><?php
} else { // Small posts ?>
<article <?php post_class( 'col-sm-6 col-md-4' ); ?>>
<div class="front-thumbnail-image"><?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?></div>
<a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part ('front-page-shop'); ?>
<?php get_template_part( 'share-buttons' ); ?>
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
</article>
<?php
}
$i++;
}
wp_reset_postdata();?>
</div>
<?php
}
else {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
echo do_shortcode( '[ajax_load_more id="ajax-load" post_type="post" posts_per_page="14" pause="true" scroll="false" button_label="LOAD MORE"]' );
get_footer();
I've asked the author for help and hist suggestion was to use Ajax Load More variables (https://connekthq.com/plugins/ajax-load-more/docs/variables/) so something like this...
if ( $alm_item % 7 === 0 ) { // Large posts
} else { // Small posts
}
so I have now tried the following for my repeater template but it still does not work...
<?php
/*
* Template Name:
*/
$the_query = new WP_Query( [
'posts_per_page' => 14,
'paged' => get_query_var('paged', 1)
] );
if ( $the_query->have_posts() ) { ?>
<div id="ajax-load">
<?php
$i = 0;
while ( $the_query->have_posts() ) { $the_query->the_post();
if ( $alm_item % 7 === 0 ) { // Large posts ?>
<article <?php post_class( 'col-sm-12 col-md-12' ); ?>>
<div class="large-front-container">
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?>
</div>
<h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part ('front-page-shop'); ?>
<?php get_template_part( 'share-buttons' ); ?>
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
</article><?php
} else { // Small posts ?>
<article <?php post_class( 'col-sm-6 col-md-4' ); ?>>
<div class="front-thumbnail-image"><?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?></div>
<a class="front-page-post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<p class="front-page-post-excerpt"><?php echo get_the_excerpt(); ?></p>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php get_template_part ('front-page-shop'); ?>
<?php get_template_part( 'share-buttons' ); ?>
<div class="front-comments"><?php comments_popup_link ('0', '1', '%', 'comment-count', 'none'); ?></div>
</article>
<?php
}
$i++;
}?>
</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();

Related

How to display each category most recent post and change the order of categories whenever there is a new post in each category?

I have been trying to customize my site but I have met a problem... As I have stated in the title, what shall I add in order to make it possible? I will like the make the category with the latest post move to the first. I have tried for 5 hours and still failed to do it. Please teach me how to fix it.
<?php
//Get the desired categories and order by ID
$cat_args = array(
'orderby' => 'id'
);
//For each category show a random post
$categories = get_categories($cat_args);
foreach ($categories as $category) {
?>
<?php
$post_args = array(
'numberposts' => 1,
'category' => $category->term_id,
);
$posts = get_posts($post_args);
foreach ($posts as $post) {
?>
<article <?php post_class('post-list animated fadeIn'); ?> role="article">
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<figure class="eyecatch<?php if (!has_post_thumbnail()) : ?> noimg<?php endif; ?>">
<?php the_post_thumbnail('home-thum'); ?>
<?php archivecatname(); ?>
</figure>
<section class="entry-content cf">
<h1 class="h2 entry-title"><?php the_title(); ?></h1>
<div class="byline entry-meta vcard">
<?php if (get_option('post_options_authordisplay', 'author_off') == 'author_on') : ?><span class="writer name author"><?php echo get_avatar(get_the_author_meta('ID'), 30); ?><span class="fn"><?php the_author(); ?></span></span><?php endif; ?>
</div>
<div class="description"><?php the_excerpt(); ?></div>
</section>
</a>
</article>
<?php get_template_part('loop'); ?>
<?php
}
}
?>
Query Arguments
$args = array(
'cat' => $category->term_id,
'post_type' => 'post',
'posts_per_page' => '1',
);
Running the Query
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<section class="<?php echo $category->name; ?> listing">
<h2>Latest in <?php echo $category->name; ?>:</h2>
<?php while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'category-listing' ); ?>>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php } ?>
<h3 class="entry-title">
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</h3>
</article>
<?php } // end while ?>
</section>
<?php } // end if
// Use reset to restore original query.
wp_reset_postdata();

How to customize owl carousel slider

I've added the owl carousel 2 (the most recent update) to my theme. I've added the carousel successfully. However, I have no control over customizing it. I've tried editing the owl-carousel.js file and I've tried editing my owl.carousel-init.js file, but neither of them give me the customizations I want. The main one being that I only want 5 owl items and there are currently 8. Have I added all of the correct files? This is the first theme I'm building, so I'm not 100% confident I did everything right. Can someone help spot the error I made, and help me customize it? Thanks in advance.
I set up my carousel.php file to show the most recent popular posts*
front-page.php
<?php
/*
* Template Name:
*/
get_header();
get_template_part ('inc/carousel');
$the_query = new WP_Query( [
'posts_per_page' => 14,
'paged' => get_query_var('paged', 1)
] );
if ( $the_query->have_posts() ) { ?>
<div id="ajax">
<?php
$i = 0;
$j = 0;
while ( $the_query->have_posts() ) { $the_query->the_post();
if ( $i % 7 === 0 ) { // Large post: on the first iteration and every 7th post after... ?>
<div class="row">
<article <?php post_class( 'col-sm-12 col-md-12' ); ?>>
<div class="large-front-container">
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?>
</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
} else { // Small posts ?>
<?php if($j % 3 === 0) echo '<div class="row">'; ?>
<article <?php post_class( 'col-md-4' ); ?>>
<?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?>
<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>
<?php $j++; if($j % 3 === 0) echo '</div>'; ?>
<?php
}
$i++;
}?>
</div>
<?php if(get_query_var('paged') < $the_query->max_num_pages) {
load_more_button();
}
}
elseif (!get_query_var('paged') || get_query_var('paged') == '1') {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
wp_reset_postdata();
get_footer();
carousel.php
<div class="owl-carousel">
<?php
$carousel_cat = get_theme_mod('carousel_setting','1');
$carousel_count = get_theme_mod('count_setting','4');
$month = date('m');
$year = date('Y');
$new_query = new WP_Query( array('posts_per_page' => $carousel_count, 'meta_key' => 'wpb_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC','monthnum'=>$month,'year'=>$year ));
?>
<?php if ( $new_query->have_posts() ) : ?>
<?php while ( $new_query->have_posts() ) : $new_query->the_post(); ?>
<div class="item">
<?php the_post_thumbnail('popular-posts'); ?>
<h2><a class="popular-category"
<?php
$categories = get_the_category();
$separator = ", ";
$output = '';
if ($categories) {
foreach ($categories as $category) {
$output .= '' . $category->cat_name . '' . $separator;
}
echo trim($output, $separator);
}
?></a></h2>
<p>
<a class="popular-excerpt" href="<?php the_permalink(); ?>"><?php echo get_the_excerpt(); ?></a>
</p>
</div>
<?php endwhile; wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, No Popular Posts Found ' ); ?></p>
<?php endif; ?>
</div>
owl.carousel-init.js
$(document).ready(function() {
$(".owl-carousel").owlCarousel({
autoPlay: 3000, //Set AutoPlay to 3 seconds
items : 4,
itemsDesktop : [1199,3],
itemsDesktopSmall : [979,3]
});
});
functions.php
unction templatename_add_owlcarousel() {
wp_enqueue_script ( 'jquery' );
wp_enqueue_script( 'owl-carousel-init', get_template_directory_uri() . '/js/owl.carousel-init.js', array( 'jquery' ), false, true );
wp_enqueue_script( 'owl-carousel', get_template_directory_uri() . '/js/owl.carousel.js', array( 'jquery' ), false, true );
wp_enqueue_style( 'owlcarousel-style-theme', get_template_directory_uri() . '/css/owl.theme.css' );
wp_enqueue_style( 'owlcarousel-style', get_template_directory_uri() . '/css/owl.carousel.css' );
}
add_action( 'wp_enqueue_scripts', 'templatename_add_owlcarousel' );
footer.php
script src="<?php bloginfo('template_directory'); ?>/js/owl.carousel-init.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/js/owl.carousel.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/js/jquery.js"></script>
FILES IV'E ADDED
css/owl.carousel.css
css/owl.theme.css
js/owl.carousel.js
owl.carousel-init.js
$(document).ready(function() {
$(".owl-carousel").owlCarousel({
autoPlay: 3000, //Set AutoPlay to 3 seconds
responsive:{
0:{
items:1,
nav:true
},
600:{
items:2,
nav:false
},
1000:{
items:4,
nav:true,
loop:false
}
}
});
});
and in footer.php
<script src="<?php bloginfo('template_directory'); ?>/js/jquery.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/js/owl.carousel.js"></script>
<script src="<?php bloginfo('template_directory'); ?>/js/owl.carousel-init.js"></script>
hope this helps..

How to correctly add in a row to bootstrap to fix grid system moving onto multiple lines

I am using bootstrap to display the posts on my front page. I have my posts alternate between 2 rows of 3 posts and 1 large post. Everything looked good except for the fact that I noticed that if there is a longer title or excerpt in one of the posts then the rest of the page would be messed up (example below). After researching I've noticed the best solutions are to use row and clear fix. However every time I try adding a div of row I seem to be placing it in incorrectly. (I had some help constructing my front-page.php so I don't know the correct way to add it in) Can anyone help me correctly add in a div row into my front-page.php? I would appreciate it a lot, I've been trying to find a solution for weeks!
I have tried looking at different solutions to questions on here but I still can't correctly change my own code. So if anyone can help at all it would be greatly appreciated.
how it should look...
how it looks when I have a post that has a title (or excerpt) that goes on to multiple lines...
as you can see example post 12 moves all the way over to the right (where it should be on the left) example post 13 and 14 are moved underneath.
my front-page.php
<?php
/*
* Template Name:
*/
get_header();
get_template_part ('inc/carousel');
$the_query = new WP_Query( [
'posts_per_page' => 14,
'paged' => get_query_var('paged', 1)
] );
if ( $the_query->have_posts() ) {
?>
<div id="ajax">
<?php
$i = 0;
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( $i % 7 === 0 ) { // Large post: on the first iteration and every 7th post after...
?>
<article <?php post_class( 'col-sm-12 col-md-12' ); ?>>
<div class="large-front-container">
<?php the_post_thumbnail('full', array('class' => 'large-front-thumbnail')); ?>
</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>
<?php
} else { // Small posts
?>
<article <?php post_class( 'col-md-4' ); ?>>
<?php the_post_thumbnail('full', array('class' => 'medium-front-thumbnail')); ?>
<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>
<?php
}
$i++;
}
?>
</div>
<?php
if(get_query_var('paged') < $the_query->max_num_pages) {
load_more_button();
}
}
elseif (!get_query_var('paged') || get_query_var('paged') == '1') {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
wp_reset_postdata();
get_footer();
You can create another variable $j = 0 and add row every 3rd small blog post.
<?php
/*
* Template Name:
*/
get_header();
get_template_part('inc/carousel');
$the_query = new WP_Query( [
'posts_per_page' => 14,
'paged' => get_query_var('paged', 1)
] );
if ($the_query->have_posts()) {
?>
<div id="ajax">
<?php
$i = 0;
$j = 0;
while ($the_query->have_posts()) {
$the_query->the_post();
if ($i % 7 === 0) { // Large post: on the first iteration and every 7th post after...
?>
<div class="row">
<article <?php
post_class('col-sm-12 col-md-12');
?>>
<div class="large-front-container">
<?php
the_post_thumbnail('full', array(
'class' => 'large-front-thumbnail'
));
?>
</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
} else { // Small posts
?>
<?php
if ($j % 3 === 0)
echo '<div class="row">';
?>
<article <?php
post_class('col-md-4');
?>>
<?php
the_post_thumbnail('full', array(
'class' => 'medium-front-thumbnail'
));
?>
<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>
<?php
$j++;
if ($j % 3 === 0)
echo '</div>';
?>
<?php
}
$i++;
}
?>
</div>
<?php
if (get_query_var('paged') < $the_query->max_num_pages) {
load_more_button();
}
} elseif (!get_query_var('paged') || get_query_var('paged') == '1') {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
wp_reset_postdata();
get_footer();

how to display posts in a grid layout

Im trying to make a page that displays all of the archive posts of my posts with the category food(category id is 10). I am trying to set it up using col-md-4 so I would have three posts in one row. I think I added all of the necessary elements to my php file, I just don't know how to set it up now. I tried to set it up so that there is 4 column's of 3 posts (so 12 posts) and then after every 12 posts it would load the ajax load more button. Can anyone help me out in trying to get this to work. Thanks in advance.
UPDATED it is still not looping through the posts correctly - and it is only showing 1 post
<?php
get_header();
get_template_part ('inc/carousel-food');
$the_query = new WP_Query( [
'posts_per_page' => 12,
'paged' => get_query_var('paged', 1)
] );
if ( $the_query->have_posts() ) { ?>
<div id="ajax">
<article class="post">
<div class="row">
<div class="col-md-4"><?php the_post_thumbnail('medium-thumbnail'); ?>
<h2><a class="post-title" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
<p class="post-excerpt"><?php echo get_the_excerpt(); ?></p>
<?php get_template_part( 'share-buttons' ); ?>
<a class="moretext" href="<?php the_permalink(); ?>">Read more</a>
<?php comments_popup_link ('No Comments', '1 Comment', '% Comments', 'comment-count', 'none'); ?>
</div>
</div>
</article>
</div>
<?php if(get_query_var('paged') < $the_query->max_num_pages) {
load_more_button();
}
}
elseif (!get_query_var('paged') || get_query_var('paged') == '1') {
echo '<p>Sorry, no posts matched your criteria.</p>';
}
wp_reset_postdata();
get_footer();
You are missing a loop that will repeat your posts code, once for each post.
Add this around your <article>:
<?php while ( $the_query->have_posts() ) : ?>
<?php $the_query->the_post(); ?>
<article> ...
... </article>
<?php endwhile; ?>

Editing Wordpress code to bring post type

Is there a way to edit the following so that rather than bringing in the most recent posts, it bring in posts from a specific category?
if( !function_exists('zolo_recent_posts') ) { function zolo_recent_posts($atts, $content = null) { ob_start(); extract(shortcode_atts(array( "num" => '4', 'columnsizepost' => '', 'data_animation'=>'fadeInDown', 'data_delay'=>'0' ), $atts)); global $post;
query_posts( 'post_type=post&posts_per_page='.$num.'&paged='. #$paged ); ?>
<div class="zolo-recent-post <?php echo $columnsizepost;?>">
<?php $i=1 ; while (have_posts()) : the_post(); if (has_post_thumbnail( $post->ID ) ): ?>
<?php //$image=w p_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'recent_posts_thumb' ); ?>
<?php endif; if( $i % 4==0 ) $class='last' ; else $class='' ; ?>
<div class="blog-box <?php echo $class;?> animated hiding" data-animation="<?php echo $data_animation;?>" data-delay="<?php echo $data_delay; ?>">
<div class="blog-img"><a href="<?php the_permalink(); ?>">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail('recent_posts_thumb');
}
else {
echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/thumb-img.jpg" />';
}
?>
</a>
</div>
<div class="blog-text-area">
<h3><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h3>
<div class="alltag-row"> <span class="date"><i class="fa fa-calendar"></i>
<?php the_time('F jS, Y') ?>
</span> <span class="add-comment"><a href="<?php the_permalink(); ?>#hello">
<?php comments_number( '0 <i class="fa fa-comment-o"></i>', '1 <i class="fa fa-comment"></i>', '% <i class="fa fa-comments"></i>' ); ?>
</a></span>
</div>
<?php the_excerpt() ;?>
</div>
</div>
</span>
<?php $i++; endwhile; ?>
</div>
<?php wp_reset_query(); $demolp_output=o b_get_clean(); return $demolp_output; } add_shortcode( "zolo_recent_post", "zolo_recent_posts"); }
<?php query_posts('category_name=CATEGORYNAME&showposts=5');
while (have_posts()) : the_post();
// do whatever you want
?>
<b><?php the_title(); ?>
<?php
endwhile;
?>
Using this way you can fetch posts of a specific category in WordPress.

Categories