I'm new to Wordpress and I'm trying to loop through sticky posts with this code:
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Query sticky posts */
$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>
<?php
foreach ($stickies as $sticky) {
the_title();
comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' );
}
?>
However in the out put if I have 2 stickies posts, the first one is displayed two times...
Any idea ?
Thanks a lot for your help !
EDIT :
It seems that
foreach ($stickies['WP_Post Object'] as $sticky) {
get the good two articles, but I still have this error message : Warning: Invalid argument supplied for foreach()...
Full code :
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>
<?php
foreach ($stickies['WP_Post Object'] as $sticky) {
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('thumbnail', array('class' => "media-object img-rounded"));
}
the_title();
comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' );
}
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="media">
<a class="pull-left" href="<?php the_permalink() ?>">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail('thumbnail', array('class' => "media-object img-rounded"));
}
?>
</a>
<div class="media-body">
<h3 class="media-heading"><a class="permalink" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title(); ?></a></h3>
Par <?php the_author(); ?>, le <?php the_time('j F Y'); ?> - <?php comments_number( 'Pas de commentaires', '1 commentaire', '% commentaires' ); ?>
<?php the_excerpt(); ?>
</div>
</div> <!-- /media -->
<?php endwhile; ?>
<?php endif; ?>
Try this:
$stickies = query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1, 'ignore_sticky_posts' => true) );
EDIT:
I think you should use standard wordpress posts loop:
while (have_posts()) : the_post();
the_title();
endwhile;
EDIT:
You can use rewind_posts() to start a new loop:
// main loop
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
// rewind
<?php rewind_posts(); ?>
// new loop
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
Related
I am currently using the following code
Now it shows all the posts of my site, I just want it not to show the last 4 posts and start showing from post 5
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post();?>
<div class="pctn animate"><div class="c2enter">
<div class="plimo plimind">
<div class="he3"></div>
<h2><?php echo wp_trim_words( get_the_title(), 19 ); ?></h2>
</div><div class="he3"></div>
<a href="<?php the_permalink();?>" target="_blank">
<?php
$address = get_the_permalink();
$arr = explode('/',$address);
the_post_thumbnail( 'thumbnail', array( 'alt' => ''.$arr[3], 'title' => ''.get_the_title().'' ) ); ?></a>
</div>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
I want to display the 3 most recent posts at the bottom of my single-blog page, but without the current posts.
my code:
<div class="blog__posts">
<h2><?php esc_html_e('andere blogberichten', 'dfib-theme'); ?></h2>
<?php esc_html_e('alle blogberichten', 'dfib-theme'); ?>
<div class="blog__posts--wrapper">
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="blog__single">
<div class="blog__single--img" style="background-image: url(<?php echo get_the_post_thumbnail_url();?>);"></div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
lees meer
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
It displays 3 posts, but when I visit the most recent post, the same post is displayed at the bottom again. Is there a way to exclude the current one every time?
I just found the solution on this website: https://pineco.de/snippets/exclude-current-post-from-wp_query/
I just added this piece to my query:
'post__not_in' => array(get_the_ID())
You have to exclude the current post with the post__not_in.
Add post__not_in in WP_Query array like below.
<?php
$the_query = new WP_Query( array(
'posts_per_page' => 2,
'post__not_in' => array( get_the_ID() )
));
?>
I'm using this code in different sections of my website and I want to display different posts in each section of the front page according to their category.
Can someone please help em to add a category there one is "surf" , I've tried everything.
Or maybe a different way to do it?
Thank you.
<div class="posts">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );
if ( "1" < $paged ) : ?>
<div class="page-title section small-padding">
<h4 class="section-inner"><?php printf( __('Page %s of %s', 'radcliffe'), $paged, $wp_query->max_num_pages ); ?></h4>
</div>
<div class="clear"></div>
<?php endif; ?>
<?php while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php get_template_part( 'content', get_post_format() ); ?>
</div> <!-- /post -->
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="archive-nav">
<?php echo get_next_posts_link( '« ' . __('Posts Antigos', 'radcliffe')); ?>
<?php echo get_previous_posts_link( __('Posts Recentes', 'radcliffe') . ' »'); ?>
<div class="clear"></div>
</div> <!-- /post-nav archive-nav -->
<?php endif; ?>
<?php endif; ?>
</div> </div> <!-- /posts -->
If you are trying to display category by ID , Then
global $post;
$args = array( 'category' => '12' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Note: In $args = array( 'category' => '12' ); 12 is the ID of
the category
But if you want to display category by Name, Then
global $post;
$args = array( 'category_name' => 'uncatogerized' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Here, uncategorized is a category name
Sorry, I am pretty new to WordPress coding and learning it from scratch. Please pardon me for my lack of experience.
I have a custom post type called 'Products' and want to display only them as a date based archive in homepage. But, I Don't want to display the post titles or any other content from the posts except the featured from first three or four posts in that date. Something like this:
I am trying the following code, but it returns the posts as the normal loop.
<?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
<?php if ( $query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
$archive_year = get_the_time('Y');
$archive_month = get_the_time('m');
$archive_day = get_the_time('d');
?>
<div class="idpostdate">
<?php the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?>
</div>
<div class="thumbnail">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('homet'); } ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
Any guidance?
Functions like the_ID() and the_post_thumbnail() run on the main query. If you want to use their equivalents in your code, you'll need to prepend $query-> to them.
Untested, but I think it'll do what you want it to:
<?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
<?php if ( $query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php $query->the_ID(); ?>" <?php $query->post_class(); ?>>
<?php
$archive_year = $query->get_the_time('Y');
$archive_month = $query->get_the_time('m');
$archive_day = $query->get_the_time('d');
?>
<div class="idpostdate">
<?php $query->the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?>
</div>
<div class="thumbnail">
<?php if ( $query->has_post_thumbnail() ) { $query->the_post_thumbnail('homet'); } ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
I have been working on this for a while basically i am trying to fetch the top three stick posts from a specific category in WordPress and display them only. I have some code below however this is fetching all the posts not just the specific ones marked as sticky in that category.
<?php $sticky=get_option('sticky_posts');
$query_args=array(
'post__in' => $sticky,
'category__in'=>array($category)
);
$the_query = new WP_Query($query_args); ?>
<?php $count = 0; ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
<div class="featurethumb"><?php the_post_thumbnail(array(306,306), array ('class' => 'featurethumb')); ?>
<div class="featuretitle-bg"><div class="featuretitle"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></div>
<div class="featured-desc"><?php the_excerpt(__('(more…)')); ?></div></div>
</div>
<?php elseif ($count == 2) : ?>
<div class="index-thumb"><?php the_post_thumbnail(array(100,100), array ('class' => 'alignleft1')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<?php the_excerpt(__('(more…)')); ?>
<?php else : ?>
<div class="index-thumb"><?php the_post_thumbnail(array(100,100), array ('class' => 'alignleft2')); ?></div>
<div class="indexblog-title"><a title="<?php the_title(); ?>" href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></div>
<?php the_excerpt(__('(more…)')); ?>
<?php endif; ?>
<?php endwhile; ?>
I'm assuming that you defined your $category somewhere as RST mentioned?
<?php
/* Get all sticky posts */
$sticky = get_option( 'sticky_posts' );
/* Sort the stickies with the newest ones at the top */
rsort( $sticky );
/* Get the 5 newest stickies (change 5 for a different number) */
$sticky = array_slice( $sticky, 0, 5 );
/* Query sticky posts */
$query_args = array(
'post__in' => $sticky,
'category__in'=>array($category)
);
?>
$my_query = new WP_Query($query_args); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID; ?>