Get 2 posts per slide, Unslider, Wordpress - php

I'm trying to get two posts per slide in my Wordpress theme. I get posts from the category "news" and want to display about 5 pages with 2 posts. At this moment it displays just one.
This is the code I've got at the moment.
<div class="banner">
<ul>
<?php $logo = new WP_Query( array( 'category_name' => 'news', 'posts_per_page' => 10 ) ); ?>
<?php while ($logo->have_posts()) : $logo->the_post(); ?>
<?php $do_not_duplicate[] = $post->ID; ?>
<li class="news-item col-1-1 no-pad">
<div class="description">
<h4><?php the_title(); ?></h4>
<p class="post-date">Gepubliceerd op: <?php echo get_the_date('d-m-Y'); ?></p>
<p><?php content(160); ?></p>
</div>
<a class="button-med" href="<?php echo get_permalink(); ?>">Lees meer</a>
</li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
</div>

Related

AJAX Load More Plugin Loads Posts That are Already Shown

I am working on a site with a page that has multiple sections, each section has multiple loops featuring multiple categories. I use Ajax Load More plugin to load new posts for each sections. The issue is when I click on Load More, it loads both the posts already shown and the one that hasn't been shown. I want it to load only new posts not already shown.
Here is the shortcode I used:
echo do_shortcode('[ajax_load_more container_type="div" post_type="post" posts_per_page="3" preloaded="true" preloaded_amount="4" pause="true" scroll="false" button_loading_label="Loading..." seo="true" category="church-music-news"]');
Here is the loop on on of the sections
<div class="row">
<div class="col-lg-12 col-sm-12">
<div class="music_box bg-color1">
<div class="music_box_top">
<?php
$sticky = get_option( 'sticky_posts' );
rsort( $sticky );
$args = array(
'post__in' => $sticky,
'posts_per_page' => 1,
'cat' => 34
);
$sticky_query = new WP_Query( $args );
while ( $sticky_query->have_posts() ) : $sticky_query->the_post();
?>
<a href="<?php the_permalink(); ?>">
<div class="fashion_box_thumb">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'full', array() );
}
?>
</div>
</a>
<div class="fashion_box_text">
<a href="<?php the_permalink(); ?>">
<h3><?php the_title(); ?></h3>
</a>
<p><?php the_excerpt(); ?></p>
<div class="post_cont_icons">
<span class="fa fa-comments cmnt"> <?php comments_number('0','1','%'); ?></span>
<?php echo getPostLikeLink(get_the_ID());?>
<span class="matchtime2"><i class="fa fa-clock-o"></i> <?php the_time();?><br></span>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<div class="clear"></div>
</div><!--music_box_top-->
<div class="fashion_box_bottom">
<?php
$args = array(
'post__not_in' => get_option( 'sticky_posts' ),
'posts_per_page' => 4,
'cat' => 34
);
$sticky_query = new WP_Query( $args );
$count = 0;
while ( $sticky_query->have_posts() ) : $sticky_query->the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) :
?>
<div class="fashion_box_bottom_item">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php elseif ($count == 2) : ?>
<div class="fashion_box_bottom_item">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php elseif ($count == 3) : ?>
<div class="fashion_box_bottom_item">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<?php elseif ($count == 4) : ?>
<div class="fashion_box_bottom_item">
<a href="<?php the_permalink(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</div>
<div class="clear"></div>
</div><!--music_box_bottom-->
</div><!--music_box-->
</div><!--col-lg-12-->
<?php else :
get_template_part( 'woodclefpro/pro_template3' );
endif;
endwhile;
wp_reset_postdata(); ?>
</div><!--row-->
<div class="row">
<?php
echo do_shortcode('[ajax_load_more container_type="div" post_type="post" posts_per_page="3" preloaded="true" preloaded_amount="4" pause="true" scroll="false" button_loading_label="Loading..." seo="true" category="church-music-news"]');
?>
</div>
This is for those that might come across the question above and are facing the same issue that I was. Here is how I solved it.
Add the code below right before endwhile
$do_not_duplicate[] = $post->ID;
Add this inside your shortcode: post__not_in="'.$post__not_in.'"
Then your final shortcode looks like this:
echo do_shortcode('[ajax_load_more ajax_load_more post__not_in="'.$post__not_in.'" container_type="div" post_type="post" posts_per_page="3" preloaded="true" preloaded_amount="4" pause="true" scroll="false" button_loading_label="Loading..." seo="true" category="church-music-news"]');
Not quite right. The fact is that on the page of one of the Addons to this plugin say that the template output single entry, for example "single.php" should be nothing but a shortcode. All content single.php should be placed in the template used by the plugin. Sorry for the crooked English.
https://connekthq.com/plugins/ajax-load-more/add-ons/single-posts/
Note: Ajax Load More will take care of loading ALL posts, including
the initial post when a user lands on the page. All that should remain
in your single.php loop is the ajax_load_more shortcode (as seen
above).

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; ?>

PHP - How do I Add Exceptions or Exclude Pages, Sub Pages, or Objects from a "For Each" Loop?

I am trying to exclude certain sub-Pages (just listed as "Pages") using WordPress 4.2.2
I've been having troubles trying to exclude a few Pages, because with the code below, the script grabs ALL the Pages or sub-Pages under the Parent Page and executes commands on them.
There is a "foreach" loop, and I was wondering what I could add to exclude a few Pages.
The Code, which is part of theme and is the PHP for a particular Page/Section of the site is:
<?php get_header(); ?>
<div class="b-page">
<h1 class="b-page__title"><span><?php the_title(); ?></span></h1>
<div class="container">
<div class="main">
<div class="b-services">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<ul>
<?php $subPages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
foreach($subPages as $item): ?>
<li class="item">
<h3 class="item-title"><?php echo get_the_title($item); ?></h3>
<div class="item-image"><?php echo get_the_post_thumbnail($item->ID, 'page-thumbnail');?></div>
<div class="item-content">
<?php //echo apply_filters('the_excerpt', wp_trim_words($item->post_excerpt, 35)); ?>
<?php echo apply_filters('the_content', substr(strip_tags($item->post_content), 0, 140) . '…'); ?>
<a class="b-read-more" href="<?php echo get_permalink($item); ?>">Read More</a>
</div>
</li>
<?php endforeach; ?>
</ul>
You can make an array of titles you want to skip. And then match them with the title to see if you need to display it or continue to the next item.
$skip = array("Title1", "Title2", "Very long page title");
foreach($subPages as $item): ?>
<?php
// Skip these page title
if (in_array(get_the_title($item), $skip)) {
continue;
}
?>
<li class="item">
<h3 class="item-title"><?php echo get_the_title($item); ?></h3>
<div class="item-image"><?php echo get_the_post_thumbnail($item->ID, 'page-thumbnail');?></div>
<div class="item-content">
<?php //echo apply_filters('the_excerpt', wp_trim_words($item->post_excerpt, 35)); ?>
<?php echo apply_filters('the_content', substr(strip_tags($item->post_content), 0, 140) . '…'); ?>
<a class="b-read-more" href="<?php echo get_permalink($item); ?>">Read More</a>
</div>
</li>
<?php endforeach; ?>
Or you can exclude them in the get_pages function with their id.
$subPages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order', 'exclude' => array(1,5,8,13), ));

Recent posts on homepage showing full post not excerpt

I have managed to get recent posts showing on my homepage which is working well. They only problem i have is that the full post and not the excerpt is showing.
<div id="home-news-container">
<?php query_posts('cat=#&posts_per_page=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="home-post-container">
<div class="home-post-thumb">
<a href="<?php echo get_permalink(); ?>">
<?php the_post_thumbnail('large_wide'); ?>
</a>
</div>
<div class="home-post-copy">
<h4>
<a href="<?php echo get_permalink(); ?>">
<?php the_title(); ?>
</a>
</h4>
<h5>
<?php the_date(); ?>
</h5>
<?php echo the_excerpt(); ?>
<div class="home-news-readmore">
read more
</div>
</div>
</div> <!-- end home-post-container -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<div class="home-news-readmore news-extra">
more news
</div>
</div> <!--- end home-post-container -->
I don't understand what the problem is to be honest. I created a new full width template for the homepage which i thought might be causing it but its not. Bit stumped to be honest. Any help would be greatly appreciated
Use wp_get_recent_posts to get recent post
Use substr to display some limeted character of post character
<?php
$args = array (
'numberposts' => 3,
'post_type' => 'your post type name'
);
// the query
$recent_posts = new wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
echo substr($recent["post_content"],0,100);
}
?>

Display featured posts from a specific WordPress category

I have some code written which displays the latest 5 posts from a specific category however I can't work out how to make it display the latest 5 posts from that category that are marked as featured only. By featured I mean the post has been stickied, so basically it will display the 5 posts from each category that have been stickied.
<ul id="index-blog">
<?php $the_query = new WP_Query( 'category_name=whats-on&showposts=5' ); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<div class="index-thumb"><?php the_post_thumbnail(array(50,50), array ('class' => 'alignleft')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<li>
<?php the_excerpt(__('(more…)')); ?>
</li>
<?php endwhile;?>
</ul>
Try this:
$sticky=get_option('sticky_posts');
$query_args=array(
'post__in' => $sticky,
'category__in'=>array($category)
);
$the_query = new WP_Query($query_args);
You can get the top 5 sticky posts using rsort and array_slice as shown in http://codex.wordpress.org/Sticky_Posts
The issue with the other answer is that introduces a variable - $category - that you have to first populate.
Here's the revised code, including how to populate the variable:
<ul id="index-blog">
<?php $category_id = get_cat_ID( 'whats-on' );
$args = array(
'cat' => $category_id,
'post__in' => get_option('sticky_posts'),
);
?>
<?php $the_query = new WP_Query($args); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
// Fix some markup issues here - the children of `ul` elements must be `li` elements....
<li>
<div class="index-thumb"><?php the_post_thumbnail(array(50,50), array ('class' => 'alignleft')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<div class="excerpt">
<?php the_excerpt(__('(more…)')); ?>
</div>
</li>
<?php endwhile;?>
</ul>

Categories