wordpress get recent posts with description - php

I use Wordpress and I want to get recent posts with thumbnail and description.
So I use wp_get_recent_posts.
<?php $args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
<?php foreach( $recent_posts as $recent ){?>
<li>
<?php echo '<div class="textoverlay">
<h1>' . $recent["post_title"].'</h1>
<p>'.get_the_excerpt().'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>
I can get post title and link, but I can't get description:
<p>'.get_the_excerpt().'</p>

You have just missing of post_id in description without post_id it can't display anything.
<?php $args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts($args);?>
<ul class="main-slider">
<?php foreach( $recent_posts as $recent ){?>
<li>
<?php echo '<div class="textoverlay">
<h1>' . $recent["post_title"].'</h1>
<p>'.get_the_content($recent["ID"]).'</p>
</div> ';
if ( has_post_thumbnail($recent["ID"]) ) {
echo get_the_post_thumbnail($recent["ID"],'thumbnail');
} ?>
</li>
<?php
}?>
</ul>

echo wpautop( $recent['post_content'] );

Related

post time don't change in loop - wordpress

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

display latest post with thumbnail wordpress

I would like to display the 3 latest posts of my Wordpress blog using a shortcode. I wrote this php code. It works but display 3 times each posts. Do you know how to fix it please ?
$counter = 3;
$recentPosts = new WP_Query();
$recentPosts->query('showposts=3');
$recent_posts = wp_get_recent_posts(3);
while ($recentPosts->have_posts()) : $recentPosts->the_post();
foreach( $recent_posts as $recent ){
echo '<li class="box'.$counter--.'">';
the_post_thumbnail();
echo '' . $recent["post_title"].'';
echo '</li>';
}
endwhile;
Please using wordpress default function wp_get_recent_posts use this code
<h2>Latest Posts</h2>
<ul>
<?php
$args = array(
'numberposts' => 3,
);
$recent_posts = wp_get_recent_posts($args, $output = ARRAY_A);
foreach( $recent_posts as $recent ){
echo get_the_post_thumbnail( $recent["ID"], 'thumbnail' );
echo '<li>' . $recent["post_title"].' </li> ';
}
?>
</ul>
Also adjust your code where you want display.
Please try below code.
<?php
$postslist = get_posts('numberposts=3&order=DESC&orderby=date');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div class="entry">
<h3><?php the_title(); ?></h3>
<?php the_time(get_option('date_format')) ?>
<?php the_excerpt(); ?>
</div>
<?php endforeach; ?>
Please try this code.
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>' . $recent["post_title"].' </li> ';
}
?>

Get categories for a single post in a custom post type

I'm trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. This list will eventually serve as a class for a div ($CATEGORYSLUGSWILLEVENTUALLYGOHERE).
I've found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. Here's what I have so far:
<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>
<?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>
<div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">
<div class="port-item-home">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
And here's what I've tried so far to get the category list:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'taxonomy' => 'project-type'
);
$categories = get_categories( $args );
echo '<p> '.print_r(array_values($categories)).'something</p>'
?>
I have it returning the array - but the array is showing that it'll display all categories instead of the ones pertaining to that specific post.
I also tried:
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'project-type';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?>
And that also displays all categories instead of the ones pertaining to the post.
Any suggestions??
Got it! Found this article that helped me out:
https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type
<!-- The Query -->
<?php
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'menu_order' );
$custom_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
$terms_slugs_string = '';
$terms = get_the_terms( $post->ID, 'my_post_type' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term_slugs_array = array();
foreach ( $terms as $term ) {
$term_slugs_array[] = $term->slug;
}
$terms_slugs_string = join( " ", $term_slugs_array );
}
?>
<div class="box<?php echo $terms_slugs_string ?>">
<div class="port-item-home">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
</a>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; ?>

php pagination full list of pages instead of small list with next and prev

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

Capitalise portfolio category string in Wordpress

I'm trying to capitalise the Wordpress portfolio category string of "Corporate identity".
I have tried using this post: Capitalise every word of a string in PHP?, but have had no luck.
URL: http://utopia.gerandeklerk.com/
On the home page, hover over the first portfolio item (Steam Africa 2014), you will notice on the hover-state it lists the portfolio categories "Collateral" and "Corporate identity". The client wants wants it to display "Corporate Identity", as it does in the category filter menu above it.
I added the following string - can someone tell me where I went wrong?
$portfolio_category[] = ucwords(strtolower($term->slug)); // Capitalise Portfolio Category String
Below is the code from portfolio.php
<div class="filter-container">
<ul data-drop-content class="filter-list">
<li>All</li>
<?php
$terms = get_terms("portfolio_category");
foreach ( $terms as $term ) {
echo "<li><a href='#' data-filter='" . "." . strtolower($term->slug) . "'>" . $term->name . "</a></li>";
};
?>
</ul>
<span>| More filters</span> <img src="<?php echo get_template_directory_uri(); ?>/images/down-arrow.png">
<?php
$desired_width = 440;
$desired_height = 440;
if(is_tax()) { // is category page
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array( 'post_type' => 'portfolio', 'portfolio_category' => $term -> slug, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order'=>'ASC' );
}
else { // is main portfolio page
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => -1, 'orderby' => 'menu_order','order'=>'ASC' );
}
$loop = new WP_Query( $args );
if($loop->have_posts()) {
?>
<div class="portfolio-gallery group isotope">
<?php
//output the latest projects from the 'my_portfolio' custom post type
while ($loop->have_posts()) : $loop->the_post();
?>
<?php
$terms = get_the_terms( $post->ID, 'portfolio_category' );
if ( $terms && ! is_wp_error( $terms ) ) {
$portfolio_category = array();
foreach ( $terms as $term ) {
$portfolio_category[] = ucwords(strtolower($term->slug)); // Capitalise Portfolio Category String
}
$the_tax = join( " ", $portfolio_category );
} else {
$the_tax = "";
};
?>
<div class="portfolio-item <?php echo $the_tax; ?> portfolio-item-<?php the_ID(); ?>">
<a class="project-link" href="<?php the_permalink(); ?>">
<div class="thumb-container">
<div class="portfolio-thumb">
<?php if( get_field('gray_scale_image') ): ?>
<img src="<?php the_field('gray_scale_image'); ?>" class="grey-scale-image" />
<?php endif; ?>
<?php if( get_field('gray_scale_image') ): ?>
<img src="<?php the_field('colour_image'); ?>" class="colour-image" />
<?php endif; ?>
</div>
</div><!-- /.thumb-container -->
</a><!-- /.project-link -->
<h3 class="portfolio-category"><?php echo str_replace('-',' ',ucwords(join(", ", explode(" ", $the_tax))) ); ?></h3>
<h3 class="portfolio-title"><?php the_title(); ?></h3>
</div><!-- /.portfolio-item -->
<?php endwhile; ?>
</div><!-- .portfolio-gallery -->
<?php } // end if ?>
It goes wrong on this line
<h3 class="portfolio-category"><?php echo str_replace('-',' ',ucwords(join(", ", explode(" ", $the_tax))) ); ?></h3>
On this line you can try the following function as you mentioned earlier
$bar = ucwords(strtolower($bar)); // Hello World!
I Didn't tested the code

Categories