I am working on a wordpress cms. On a page-template, I need to display recent 10 posts. So I tried to use, wp_get_recent_posts, found here in the codex, which I think is an appropriate hook for the purpose.The rest of the code is from my archive.php ( which displays the post-thumbnails in a masonry grid just fine in archive.php). I simply would like to achieve the same thing with the recent posts-thumbnails being on this page-template. Currently, I have this code on a template.
<div id="masonry">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
$args = array('numberposts' => '10', 'meta_key' => '_thumbnail_id');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
if (has_post_thumbnail($recent["ID"])) {
echo '<a href="' . get_permalink($recent["ID"]) . '">';
echo get_the_post_thumbnail($recent["ID"], 'archive_grid');
echo '</a>';
}
}
?>
</div>
</div>
<?php endwhile;
endif; ?>
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
<div id="masonry-new"></div>
<div class="post-navigation clearfix"><!--BEGIN .post-navigation -->
<?php dt_pagination(); ?>
</div><!--END .post-navigation -->
ISSUE : This code returns just a single thumbnail from the most recent posts. I have no clue what is wrong with the loop. The other weird thing to notice is when I var_dump the $recent_posts it returns other text-contents of the posts just fine. If you need to know this, I have my setting as setting->reading->Blog pages show at most->20 posts.
First of all you don't need to have two loops if I understood correct what you want to do. Also 'meta_key' => '_thumbnail_id' isn't necessary. I suppose is just the thumbnail id and not a meta key created by you.
You could achieve what you want like that:
<div id="masonry">
<?php
$args = array(
'posts_per_page' => 10,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => false );
$recent_posts = get_posts( $args );
foreach ($recent_posts as $key=>$post): setup_postdata( $post ); {
?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
if (has_post_thumbnail(get_the_ID())) {
echo '<a href="' . get_permalink(get_the_ID()) . '">';
echo get_the_post_thumbnail(get_the_ID(), 'archive_grid');
echo '</a>';
}
?>
</div>
</div>
<?php } ?> //end of foreach loop
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
<div id="masonry-new"></div>
<div class="post-navigation clearfix"><!--BEGIN .post-navigation -->
<?php dt_pagination(); ?>
</div><!--END .post-navigation -->
But this way you have to fix the pagination your self.
UPDATE:
It seems that get_posts doesn't work with pagination.
<div id="masonry">
<?php
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => false );
$wp_query = new WP_Query( $args );
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
if (has_post_thumbnail(get_the_ID())) {
echo '<a href="' . get_permalink(get_the_ID()) . '">';
echo get_the_post_thumbnail(get_the_ID(), 'archive_grid');
echo '</a>';
}
?>
</div>
</div>
<?php endwhile; ?> //end while loop
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
<div id="masonry-new"></div>
<div class="post-navigation clearfix"><!--BEGIN .post-navigation -->
<?php dt_pagination(); ?>
</div><!--END .post-navigation -->
<?php wp_reset_postdata(); ?>
If the second solution doens't work you have to provide the dt_pagination() function to see how is working.
Hope it helps.
First thing is you dont need two loops. Try new wp query instead of wp_get_recent_posts. here is an example for you.
<div id="masonry">
<?php $args = array('post_type' => 'post','posts_per_page' => '10' );
$loop = new WP_Query($args); if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post(); global $post; ?>
<div class="item normal" data-order='1'><!--BEGIN .item -->
<div <?php post_class(); ?> id="featured-<?php the_ID(); ?>">
<?php
if (has_post_thumbnail(get_the_ID())) {
echo '<a href="' . get_permalink(get_the_ID()) . '">';
echo get_the_post_thumbnail(get_the_ID(), 'archive_grid');
echo '</a>';
}
?>
</div>
</div>
<?php endwhile; endif; wp_reset_Query(); ?>
<?php get_template_part('includes/index-loadmore'); ?>
</div><!--END #masonry -->
Related
i have a problem when trying to show a current 4 post using WP_Query.
But, the older post (The very first post) also shown in the first loop.
This is my code:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => '3'
);
$query = new WP_Query( $args ); //show 4 post
if ( $query->have_posts() ){
/* Start the Loop */
while ( $query->have_posts() ) {
$query->the_post();?>
<div class="col-md-3 d-flex align-items-stretch">
<div class="card ">
<?php if (has_post_thumbnail()) {
$featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
?>
<img class="miniimg" src="<?php echo $featured_img_url; ?>" width="auto" height="200px">
<?php } ?>
<div class="card-body">
<h4><a href="<?php the_permalink(); ?>">
<?php the_title();
$post_date = get_the_date( 'j F Y' );
?>
</a></h4>
<p class="card-text"><?php the_excerpt();?></p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group">
<button type="button" class="btn btn-sm btn-primary">Read More</button>
</div>
<small class="text-muted"><?= $post_date; ?></small>
</div>
</div>
</div>
</div>
<?php
$counter++; }
}
?>
This is the result =>
How to fix this problem? is there any problem with my code?
Thankyou.
If specifying 'posts_per_page' => 3 gives back 4 posts, it is almost 100% sure that the first post is a sticky post. Use the option ignore_sticky_posts to ignore them.
$args = array(
'post_type' => 'post',
'posts_per_page' => 4,
'ignore_sticky_posts' => 1,
);
...
A Sticky Post is the post will be placed at the top of the front page of posts. This feature is only available for the built-in post type post and not for custom post types.
Source # https://developer.wordpress.org/themes/functionality/sticky-posts/
I'm pretty sure you applied is sticky to your post from 2012.
To verify you can just add the following to your loop inside the while statement:
<?php //...
while( have_posts() ): the_post();
if( is_sticky() ):
echo 1;
else: echo 0;
endif;
endwhile;
//... ?>
Or go to you post in the admin console, and verify that you didn't check the "is sticky" checkbox on the right side in the publish panel.
<?php
$args = array( 'post_type' => 'movies');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?><div class="movie-content" >
<?php
echo '<a href="' . get_permalink( $post->ID ) . '" title="' . esc_attr( $post->post_title ) . '">' ?>
<h2><?php echo the_title(); ?></h2>
<div><?php the_post_thumbnail('thumbnail'); ?></div>
<div class="excerpt"><?php the_excerpt(); ?></div>
<?php
the_content();
echo '<div class="entry-content">';
echo '</div>';
echo "</div></a>";
endwhile;
?>
I want to create a loop for wordpress that returns each two posts inside its own div and alternating columns every new row (see example)... Im not experimented in php enough to make this happen. I dont manage to get it working appropiatly. And see how to make the last div to bee 100% width if it does not have another column.
I would appreciate your support to make this happen since I tried many things and still no luck. (im using visual composer bootstrap classes, it does work but not as expected.This is the example I want to create
This is my code:
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'inversion',
'category_name' => '',
'order' => 'DESC',
'orderby' => 'DATE'
);
$the_query = new WP_Query( $args );?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="vc_row">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); $i++; $imagen = get_the_post_thumbnail_url(get_the_ID(),'full'); ?>
<?php if(($i % 2) == 2) : ?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
<div class="vc_col-sm-6 cont-der" >
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url(<?php echo $imagen; ?>)no-repeat; background-size:cover;">
</div>
</div>
</div>
</div>
<?php else : ?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-der" >
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url(<?php echo $imagen; ?>)no-repeat; background-size:cover;">
</div>
</div>
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
</div>
</div>
<?php endif; endwhile; ?>
</div>
<?php endif; ?>
<?php wp_reset_query(); ?>
[EDIT]Try this:
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'inversion',
'category_name' => '',
'order' => 'DESC',
'orderby' => 'date',
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<div class="vc_row">
<?php
$float_class = '';
while ( $the_query->have_posts() ) :
$the_query->the_post();
if ( $the_query->current_post &&
$the_query->current_post % 2 === 0 ) {
$float_class = $float_class ? '' : 'vc_pull-right';
}
$imagen = get_the_post_thumbnail_url( get_the_ID(), 'full' );
?>
<div class="vc_col-sm-6">
<div class="vc_row vc_row-fluid">
<div class="vc_col-sm-6 cont-der <?php echo $float_class; ?>">
<a class="click-info">Más Información</a>
<div class="img-dentro kenburns-top" style="background:url('<?php echo esc_url( $imagen ); ?>') no-repeat; background-size:cover;">
</div>
</div>
<div class="vc_col-sm-6 cont-izq">
<h3><?php the_title(); ?></h3>
</div>
</div>
</div>
<?php endwhile; // end have_posts() loop ?>
</div><!-- .vc_row -->
<?php endif; // end have_posts() check ?>
<?php wp_reset_query(); ?>
So basically, I'm working on a custom wordpress theme. What i'm trying to do is to set an icon for each category. If the loop starts and the post has a category, It'll show up with the icon that it has assigned. Right now it shows the correct icons, but the title and exerpt of the post keeps changing to the name of the page. Here is an example I have three posts math, english and history all of them have the correct icon, but display the name blog post page instead of math, english, or history.
<?php /* Template Name: News Blog Page */ get_header(); ?>
<div id="blog-post-wrapper" class="section_wrapper">
<div class="column three-fourth">
<?php $currentPage = get_query_var('paged');
$args = array(
'post_type' => 'post',
'order' => 'DESC',
'posts_per_page' => 9,
'paged' => $currentPage
);
$the_query = new WP_Query($args);
if($the_query -> have_posts()):
while ($the_query -> have_posts()): $the_query -> the_post();
get_template_part('postloopcontent', get_post_format());
endwhile;
echo "<div class='pagination'>";
echo paginate_links(array(
'total' => $the_query -> max_num_pages
));
echo "</div>";
endif;
?>
</div>
<div class="column one-fourth">
<?php get_sidebar(); ?>
</div>
</div>
<?php get_footer(); ?>
the top one is my basic layout and it grabs my loop. the bottom one is my loop
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
In your loop file, you're resting post data i.e. wp_reset_postdata(); outside the $imgquery loop/condition. If you could wrap the postdata rest function inside the condition, I think that should work.
You code must look like this
<?php
// Standard Post Format
?>
<?php $bgImage = get_the_post_thumbnail_url(); ?>
<div class="column one-third" style="background-image:url(<?php echo $bgImage; ?>);">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="nws-img">
<?php
// Find the first category the post is in.
$categories = get_the_category();
$category = $categories[ 0 ]->term_id;
$imgargs = array(
'cat' => $category,
'post_status' => 'inherit',
'post_type' => 'attachment',
'posts_per_page' => '1'
);
$imgquery = new WP_Query( $imgargs );
if ( $imgquery->have_posts() ) {
while ( $imgquery->have_posts() ) { $imgquery->the_post(); ?>
<div class="category-featured-image">
<?php echo wp_get_attachment_image( $post->ID, 'thumbnail' ); ?>
</div>
<?php
}
// Reset postdata to restore ordinal query.
wp_reset_postdata();
}
?>
</a>
<div id="content-box">
<h1> <a href="<?php the_permalink(); ?>" > <?php the_title(); ?> </a> </h1>
<?php the_excerpt(); ?>
</div>
</div>
I'm trying to add pagination to a custom loop, but I can't figure out how to do it. When I manage to add "previews" and "next" buttons it always shows the same 10 posts. I found some solutions for a while loop but not for a foreach loop (which I actually never used before).
This is the loop (is get_posts a problem?) :
<?php
$news = get_posts(array('posts_per_page' => 10));
$news['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; ?>
<?php foreach ($news as $article): ?>
<div class="col-md-4">
<h3><?php echo $article->post_title ?></h3>
<hr>
<p class="desc"><?php echo $article->post_excerpt ?></p>
<?php echo get_the_post_thumbnail($article->ID,'thumbnail'); ?>
<p class="btn_text"> Ler mais</p>
</div>
<?php endforeach; ?>
<?php previous_posts_link( '<<' );
next_posts_link( '>>', $custom_query->max_num_pages ); ?>
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source : http://web-profile.net/wordpress/themes/wordpress-custom-loop/
I have tried many things but i didn't get Read more link in paragraph
page.php
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="inner-box-post">';
echo '<h2><a href="get_permalink();">';
the_title();
echo '</a></h2>';
echo '<div class="thumbnail">';
the_post_thumbnail( 'thumbnail' );
get_comments( $args );
echo '</div>';
echo '<div class="post-containt">';
echo '<div class="post-date"><strong>';
echo get_the_date();
echo '</strong> </div>';
echo the_excerpt(); ;
echo '</div> ';
echo '</div>';
endwhile; ?>
</div>
function.php
function new_excerpt_more($output) {
return $output . '<p>' . 'Read more »' . '</p>';
}
add_filter('get_the_excerpt', 'new_excerpt_more');
Filter is excerpt_more,
add_filter('excerpt_more', 'new_excerpt_more');
Reference.
THIS IS NOT AN ANSWER, JUST CORRECTIONS
You really don't need to overuse echo in your code. Just use opening and closing php tags correctly. Rather use this. It also helps with readibility
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="inner-box-post">
<h2><?php the_title(); ?></h2>
<div class="thumbnail">
<?php the_post_thumbnail( 'thumbnail' ); ?>
<?php get_comments( $args ); ?>
</div>
<div class="post-containt">
<div class="post-date"><strong>
<?php get_the_date(); ?>
</strong> </div>
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
</div>
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="inner-box-post">
<h2><?php the_title(); ?></h2>
<div class="thumbnail">
<?php the_post_thumbnail( 'thumbnail' ); ?>
<?php get_comments( $args ); ?>
</div>
<div class="post-containt">
<div class="post-date">
<strong><?php echo get_the_date(); ?></strong>
</div>
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
Remove [...] and add read more option using
function new_excerpt_more($output) {
$output = rtrim($output,'[...]');
return $output . '<p>' . 'Read more >>' . '</p>';
}
add_filter('excerpt_more', 'new_excerpt_more');