I want a simple loop to get latest five posts, which only consist of post title and time i wrote the loop below and the title generates fine, however the time doesn't change. as it get the first post in the loop's time for other posts as well. so all the post time is same.
Please advise why time don't loop ?
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){ ?>
<li class="orbit-slide">
<div>
<a href="<?php echo get_permalink($recent["ID"]); ?>" class="ticker-a">
<span><?php echo get_the_time($recent["g:i a"]); ?> </span>
<?php echo $recent["post_title"]; ?>
</a>
</div>
</li>
<?php }
wp_reset_query();
?>
Try this one
echo get_the_time('', $recent["ID"]);
On a side note. This is a much more efficient piece of code and it is easier to output content.
<?php
$args = array( 'posts_per_page' => '5', 'orderby' => 'most_recent' );
$recent_posts = new WP_Query( $args );
if ( $recent_posts->have_posts() ) : while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="orbit-slide">
<div>
<a href="<?php the_permalink(); ?>" class="ticker-a">
<span><?php the_time("g:i a"); ?> </span>
<?php the_title(); ?>
</a>
</div>
</li>
<?php
endwhile; endif;
wp_reset_query();
?>
Thanks "Aron" for answer 1 :
echo get_the_time('', $recent["ID"]);
And Thanks "WizardCoder" for the other loop solution:
<?php
$args = array( 'posts_per_page' => '5', 'orderby' => 'most_recent' );
$recent_posts = new WP_Query( $args );
if ( $recent_posts->have_posts() ) : while ( $recent_posts->have_posts() ) : $recent_posts->the_post(); ?>
<li class="orbit-slide">
<div>
<a href="<?php the_permalink(); ?>" class="ticker-a">
<span><?php the_time("g:i a"); ?> </span>
<?php the_title(); ?>
</a>
</div>
</li>
<?php
endwhile; endif;
wp_reset_query();
?>
Related
I am working on a services cpt and would like to display a list of other posts, within the same cpt on the single post.
The code I am using is:
<?php
$loop = new WP_Query( array(
'post_type' => 'services',
'posts_per_page' => 6
));
?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
Now my question is, is there a way to add a class to the current post? The intention being to style it different to the other posts in the list.
This is very easy you can always add the class in before and after
Read this documentation https://developer.wordpress.org/reference/functions/the_title/
<?php the_title( '<div class="wrapper">', '</div>' ); ?>
Yes First get current post id and match that id with loop id and if it is same than just add class whereever you want just use below code
<?php
$current_post_id = get_the_ID();
$loop = new WP_Query( array(
'post_type' => 'services',
'posts_per_page' => 6, ));
?>
<ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
$class = ($current_post_id == $loop->ID) ? "current-post" : '';
?>
<li class="<?php echo $class; ?>">
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
It will add class "current-post" to only current post and in other posts it will add nothing.
style using
<style>
li{
/*all posts*/
}
li.current-post{
/*specific for the current post*/
}
</style>
Using function
<?php post_class(); ?>
E.g:
<div <?php post_class(); ?>>
<?php
$loop = new WP_Query( array(
'post_type' => 'services',
'posts_per_page' => 6
));
?>
<ul>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li <?php post_class(); ?>>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<h4><?php the_title(); ?></h4>
</a>
</li>
<?php endwhile; wp_reset_query(); ?>
</ul>
i'm building a recent posts function into a wordpress site, i've got it to call up two different featured images but they are both linking to the same post, can anyone see where i am going wrong?
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$post = get_posts( $args );
if($post) {
$post_id = $post[0]->ID;
if(has_post_thumbnail($post_id)){
?>
<div class="grid_24">
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail($page->ID, 'medium');
?>
</a>
</div>
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail( $post_id,'medium');
?>
</a>
</div>
</div>
<?php
}
}
?>
you can use echo get_the_permalink($post->ID) to get the uri for the posts
So it looks like in your case you'd need
echo get_the_permalink($post[0]->ID);
and
echo get_the_permalink($post[1]->ID);
in the href
However you're probably better off creating a foreach loop to go through the posts from the get_posts function
https://developer.wordpress.org/reference/functions/get_the_permalink/
https://developer.wordpress.org/reference/functions/get_posts/
Okay, first of all, you are not looping the query you have made ( e.g $posts = get_posts( $args ); ) you are just displaying the 1st post's thumbnail and the thumbnail of the current page.
You need to loop the post like this :
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$posts = get_posts( $args );
?>
<?php if ( !empty( $posts ) ) :?>
<div class="grid_24">
<?php foreach ( $posts as $post ) : ?>\
<?php if( has_post_thumbnail( $post->ID ) ) ?>
<div class="grid_12">
<a href="<?php echo esc_url( get_permalink( $post->ID ) ) ?>">
<?php echo get_the_post_thumbnail( $post->ID, 'size_here'); ?>
</a>
</div>
<?php endif; ?>
<?php endforeach?>
</div>
<?php endif;
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 want to add a Wordpress loop for a specific category in a post template that exculdes the current post.
I was suggested to use:
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
But I'm having trouble getting it to work.
My loops currently looks like this.
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Try this code.
$postid = get_the_ID();
$args=array(
'post__not_in'=> array($postid),
'post_type' => 'post',
'category_name'=>'video',
'post_status' => 'publish',
'posts_per_page' => 4
);
<div class="video">
<?php
$catquery = new WP_Query( $args );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Use
'post__not_in' => array($post->ID)
The two code blocks are using two different techniques for a Wordpress custom loop... the first modifies the global query, and the second creates a new custom query. I've outlined both below with your loop template.
Example with suggested code, global query:
Loop through the global $wp_query object in the loop code:
<div class="video">
<?php
global $wp_query;
$cat_ID = get_the_category($post->ID);
$cat_ID = $cat_ID[0]->cat_ID;
$this_post = $post->ID;
query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
?>
<!-- use the global loop here -->
<?php while ( have_posts() ) : the_post(); ?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p
</div>
Example with original code, custom query:
Loop through the custom query, adding 'post__not_in':
<div class="video">
<?php
$catquery = new WP_Query( 'category_name=video&posts_per_page=4&post__not_in=' . $post->ID );
while($catquery->have_posts()) : $catquery->the_post();
?>
<div>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<h2><?php the_title(); ?></h2>
</a>
</div>
<?php endwhile; ?>
<p class="more">M<br>O<br>R<br>E</p>
</div>
Sorry if my original answer was unclear, I initially thought you were combining the two code blocks.
My pagination code shows a full list of pages like 1,2,3,4,5,6,7,8,9,10 but i want it to show Prev,1,2,3,4,5,Next instead how can i do this with this code
if($tot>$perq) {
$output.="<div class='pagenation'>";
for($i=0;$i<($tot/$perq);$i++) {
$j=$i+1;
if($pg==$i)
$output.="<a href='".get_permalink()."?pg=$i' class='button active'>".$j."</a>";
else
$output.="<a href='".get_permalink()."?pg=$i' class='button'>".$j."</a>";
}
$output.="</div>";
}
Here is an example from a site I am working on. The keywords are 'previous_posts_link' and 'next_posts_link', which together with the $arg 'posts_per_page' will paginate for you.
Hope it helps.
<?php
the_content();
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'bonsai','posts_per_page' =>12,'paged' => $paged);
$myposts = query_posts($args);
?>
<span id="tree-list-span">
<fieldset>
<legend>The Bonsai</legend>
<ul>
<?php
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li class="main_bonsai_list_item">
<a href="<?php echo get_permalink($post->id);?>">
<span class="grower_name"><?php the_title();?></span><br/>
<span class="grower_name">
<?php
$attr = array(
'id' => "link-".$post->ID,
'style' => 'max-width: 120px',
);
echo get_the_post_thumbnail($id,'bonsai-list-thumb',$attr);
?>
</span><br/>
<?php $custom = get_post_custom($post->ID);?>
<?php $grower = $custom["tree_grower"][0];?>
<span class="grower_name"><?php echo $grower;?></span>
</a>
</li>
<?php endforeach;
?>
</ul>
<span id="page_links">
<?php
previous_posts_link( '«' );
next_posts_link( '»', $myposts->max_num_pages );
wp_reset_postdata();
?>
</span>
</fieldset>
<?php
?>