I'm building a custom WordPress widget/plugin to return the title and excerpt of a post within a permalink to the article. I am almost there but am struggling with getting the excerpt to show. Here is my current code:
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
$categories = get_the_category($recent["ID"]);
$excerpt = apply_filters('get_the_excerpt', $recent->post_excerpt);
echo '<a class="m-item diet-and-nutrition" href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" > <div class="pix"></div><div class="eyebrow"> <b>' . $categories[0]->name . '</b> / '. $recent["post_date"].'</div> <figure>' . get_the_post_thumbnail($recent["ID"], 'full') . '<figcaption class="center"> <span> <h4>'. $recent["post_title"].'</h4> </span> <span> <p>'. $excerpt .'</p> </span> </figcaption> </figure> </a>';
}
?>
It's an array so:
$recent->post_excerpt
should be:
$recent['post_excerpt'];
Related
I am trying to create news paper web site and need help to show the content of recent post and title of old posts only... here is my code
<?php
$args = array(
'orderby' => 'id',
'hide_empty' => 1,
'order' => 'ASC'
);
$categories = get_categories($args);
foreach($categories as $category) { ?>
<div class="newsdiv">
<?php
echo '<center><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></center> <br/> ';
$post_args = array(
'numberposts' => 3,
'category' => $category->term_id
);
$posts = get_posts($post_args);
foreach($posts as $post) {?>
<?php echo"*";the_title(); ?>
<div class="entry">
<?php the_content(); ?>
</div>
<?php
}
echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>Read more </a></dd>';
echo '</dl>';
?>
</div>
<?php
}
?>
you can try the codex previous post
<?php previous_post_link( $format, $link, $in_same_cat = false, $excluded_terms = '', $taxonomy = 'category' ); ?>
you can use it in simpler and/or complex way something like this.
<?php previous_post_link(); ?>
for more info you can go through with codex http://codex.wordpress.org/Function_Reference/previous_post_link
I have an area of my index.php file that I'm customizing in a theme whereby I would like to display the most recent post and some additional meta. I was able to pull the featured image, post title (and link), & the date; but not the author. I've tried various examples from the WP boards with no luck. The latest attempt below:
<?php
$args = array( 'numberposts' => '1');
$recent_posts = wp_get_recent_posts( $args );
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
//$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
foreach( $recent_posts as $recent ){
$post_author = get_user_by( 'id', $recent->post_author );
echo '<div class="full-width" id="featured-post" style="background-image: url('. $feat_image .')">';
echo '<div class="row featured-post-meta"><div class="small-8 columns">';
echo '<h2><a href="' . get_permalink($recent["ID"]) . '" title="Read: '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a></h2>';
echo '<p>'. $post_author->display_name .' | '. get_the_time('F jS, Y') .'</p>';
echo '<a class="read-post" href="'. get_permalink($recent["ID"]) .'">Read the post</a>';
echo '</div></div></div>';
}
?>
Inside your foreach loop:
$post_author = get_user_by( 'id', $recent['post_author'] );
This gets you the user object which looks like what you were trying to do with $curauth.
You could then output $post_author->display_name in place of $curauth.
echo '<p>'. $post_author->display_name .' ...
I'm using the carousel that comes with bootstrap. This I'll be using in WordPress. I'm querying two recent posts with a foreach loop but for the carousel to work correctly I need the newest post to have an extra 'active' class. I found some solutions here on stackoverflow but it were all whileloops, I really need it for this foreach loop. This is my code:
<div id="NewsCarousel" class="carousel slide">
<div class="carousel-inner">
<?php
$args = array( 'numberposts' => '2', 'category' => 5 );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<div class="item"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], array(200,200)) .$recent["post_title"].'</a> </div> ';
}
?>
</div>
</div>
You can add a counter like so $count = 0; outside of the foreach loop. And then inside the foreach loop you tell it to increment like so $count++;
You then check if the count is equal to 1 like this: if($count == 1){//do this}
So in your case lets do it like this:
<div id="NewsCarousel" class="carousel slide">
<div class="carousel-inner">
<?php
$args = array( 'numberposts' => '2', 'category' => 5 );
$recent_posts = wp_get_recent_posts( $args );
$count = 0;
foreach( $recent_posts as $recent ){
$count++;
echo '<div class="item'; if($count == 1){echo ' active';}"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], array(200,200)) .$recent["post_title"].'</a> </div> ';
}
?>
</div>
</div>
Try that, it should do the trick. I just used this method on a project of which I'm dealing with currently.
You can use a boolean variable to determine if it is a first loop or not. Initial value is true, once it loops, the value is set to false.
<div id="NewsCarousel" class="carousel slide">
<div class="carousel-inner">
<?php
$args = array( 'numberposts' => '2', 'category' => 5 );
$recent_posts = wp_get_recent_posts( $args );
$isFirst = true;
foreach( $recent_posts as $recent ){
echo '<div class="item' . $isFirst ? ' active' : '' . '"><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . get_the_post_thumbnail($recent["ID"], array(200,200)) .$recent["post_title"].'</a> </div> ';
$isFirst = false;
}
?>
</div>
</div>
I have this code for displaying a shortcode.
<?php
function recent_posts_function() {
$mypost = array( 'post_type' => 'gallery_pictures', );
$loop = new WP_Query( $mypost );
?>
<div id="boxhover">
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<!--Fade-->
<?php $ddd = '<div class="mosaic-block fade">
<a href="'. $image[0] . '" data-fancybox-group="gallery" target="_blank" class="mosaic-overlay preview fancybox" title="' . the_title . '">
<div class="details">
<h4>' . the_title() . '</h4>
<p>' . the_content_rss('', TRUE, '', 30) . '</p>
<br/>
<div class="btt">VIEW</div>
</div>
</a>
<div class="mosaic-backdrop"><img src="' . $image[0] . '" alt="gallery thumbnail" /></div>
</div>';
endwhile; ?>
</div>
<?php
return $ddd;
}
function register_shortcodes(){
add_shortcode('gallery', 'recent_posts_function');
}
add_action( 'init', 'register_shortcodes');
as you can see from the above codes, the 'return $ddd' should return all the output from the loops that the 'while' process done but its display only one.
Im currently looking for a solution and would love to hear any suggestion, recommendations and ideas on how to do it. Thank in advance.
You need to add a [dot] before your = [equal] on the loop while.
This will cause each loop add content current with the previous.
<?php function recent_posts_function() {
$ddd = ''; //First declare the string var ?>
...
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<?php $ddd .= '<div class="mosaic-block fade">'; // Put a [dot] before sign symbol ?>
<?php endwhile; ?>
...
return $ddd;
...
<? php } ?>
I'm trying to create a small wp plugin for my blog, but I've got the following problem.
The post image, isn't displaying in the right spot.
This is the proper HTML
<li>
<div class="projects">
<ul class="projects sticker">
<li><h2><?php the_title(); ?></h2></li>
<li><p>details</p></li>
</ul>
<img src="" />
</div>
</li>
This is how it's displaying now
<li>
<div class="projects">
<ul class="projects sticker">
<li><h2><?php the_title(); ?></h2></li>
<li><p>details</p></li>
</ul>
</div>
</li>
<img src="" />
Basically i have to put the img tag inside the list and div
Here is my code so far
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>'
. '<div class="projects">'
. '<ul class="projects sticker">'
. '<li>'
. '<h2>'
. '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'
. $recent["post_title"]
. '</a>'
. '</h2>'
. '</li>'
. '<li><p>details</p></li>'
. '</ul>'
. '<img src="'.the_post_thumbnail('thumbnail').'" />'
. '</div>'
. '</a>';
use this code, you have used extra <li></li>
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<a href="' . get_permalink($recent["ID"]) .
'" title="Look '.esc_attr($recent["post_title"]).'" >'
.'<div class="projects">' .'<ul class="projects sticker">'
.'<li>' .'<h2>' . $recent["post_title"] .'</h2>' .'</li>'
.'<li><p>details</p></li></ul>'
.'<img src="'.the_post_thumbnail('thumbnail').'" />'
.'</div>' .'</a>';
}
You have an extra closing <li> at the end and the placement of the closing tag of the first <li> is improperly nested and <a href> opening & closing tag is misplaced as well. Also you could have solved this problem easier—possibly by yourself—if you format the code so humans it can more easily be read. Piling on a stack of instructions on one line like that will only cause confusion:
$args = array( 'numberposts' => '3','category' => $cat_id );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li>'
. '<div class="projects">'
. '<ul class="projects sticker">'
. '<li>'
. '<h2>'
. '<a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >'
. $recent["post_title"]
. '</a>'
. '</h2>'
. '</li>'
. '<li><p>details</p></li>'
. '</ul>'
. '<img src="'.the_post_thumbnail('thumbnail').'" />'
. '</div>'
. '</a>'
;