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 .' ...
Related
i'm trying to pull in 2 seperate feeds onto my wordpress homepage (recent portfolio work & recent blog posts). I want both sets to sit within the same ul. I've managed to pull in the portfolio feed, but i'm having trouble with the blog feed.
<ul>
<?php
$args = array( 'post_type' => 'work', 'posts_per_page' => 4 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >';
echo '<div class="recent-thumb">'; the_post_thumbnail(); echo '</div>';
echo '<div class="recent-title"><span>'; the_title(); echo '</span></div>';
echo '</a></li>';
endwhile;
?>
<?php
$args = array( 'post_type' => 'post', 'posts_per_page'=> 4 );
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >';
echo '<div class="home-blog-category">'; user_the_categories(); echo '</div>';
echo '<div class="home-blog-title">'; the_title(); echo '</div>';
echo '<div class="home-blog-excerpt">'; echo excerpt(27); echo '</div>';
echo '<span class="home-blog-plus"><span class="home-blog-plus-wrap"><span class="horizontal"></span><span class="vertical"></span></span></span>';
echo '</a></li>';
endwhile;
?>
</ul>
Can anyone help with getting the post feed to pull? I'm sure this isn't the most efficient way of doing it, so if anyone has a better way of combining the two, that would also be appreciated! My knowledge of PHP is very limited :)
Cheers!
Use array_combine function in php to combine two results( $the_query & $loop ) and use a single while loop.
http://www.w3schools.com/php/func_array_combine.asp
I have a code in my page.php file that creates a list of child pages. I want every li to have a background-image added by Featured image function. Here is the entire code I have
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php endwhile; // end of the loop. ?>
<?php
if (is_page('eventsphotography')) {
$query = new WP_query('pagename=eventsphotography');
$eventsphotography_id = $query->queried_object->ID;
//The loop
if($query->have_posts() ) {
while($query->have_posts() ) {
$query->the_post();
the_content();
}
}
/* Get the children of the eventsphotography page */
$args = array (
'post_parent' => $thePostID,
'post_parent' => $eventsphotography_id,
'order' => 'ASC'
);
$eventsphotography_query = new WP_query($args);
//The Loop
if($eventsphotography_query->have_posts() ) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() ){
$eventsphotography_query->the_post();
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
echo '<div class="events-centered">';
echo '<a href="' . get_permalink() . '">';
echo '<h4>' . get_the_title() . '</h4>';
echo '</a>';
echo '<div class="view-events-details">';
echo '<a href="' . get_permalink() . '">';
echo '<h5>View Images</h5>';
echo '</a>';
echo '</div>';
echo '</div>'; /* end of events-centered */
echo '</li>';
}
echo'</ul>';
}
}
?>
I only need help for these lines:
$background = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
AND
echo '<li style="background:url(' . $background[0] . '); background-repeat:no-repeat; background-size:cover;">';
Here's the screenshot of the result of my code:
http://oi68.tinypic.com/10xzdhl.jpg
I marked the first <li> with a red rectangle. As I said before, I want URL of the featured image to be placed in <li style="background:url(URL of the featured image)">
I have found a solution. First, I created a new WP_Query:
$subs = new WP_Query( array( 'post_parent' => $post->ID, 'post_type' => 'page', 'meta_key' => '_thumbnail_id'));
Then in my loop I added this lines:
if($eventsphotography_query->have_posts() && $subs->have_posts()) {
echo '<ul class="events-list">';
while($eventsphotography_query->have_posts() && $subs->have_posts()){
$eventsphotography_query->the_post();
$subs->the_post();
echo '<li>';
echo get_the_post_thumbnail($post->ID);
...rest of the code...
I am trying to display recent post in staic page home-content.php so i have added this code in function.php
here it is a code
function my_recent_posts_shortcode($atts){
$q = new WP_Query(
array( 'orderby' => 'date')
);
$list ="";
while($q->have_posts()) : $q->the_post();
echo '<div class="item">';
$title=get_the_title();
if ( has_post_thumbnail() ) {
echo '<a class="single-image link-icon" href="' . get_permalink() . '">';
$list .=the_post_thumbnail(array(300,200),array('alt' =>$title));
echo '</a>';
}
echo '<h6 class="title"><span>'.$title.'</span></h6>';
echo '<div class="entry-body">';
$list .= wpe_excerpt('wpe_excerptlength_index', '');
echo '<a class="button default color" href="' . get_permalink() . '">Read More</a>';
echo '</div>';
echo '</div>';
endwhile;
wp_reset_postdata();
return $list ;
}
add_shortcode('recent-posts', 'my_recent_posts_shortcode');
[recent-posts] this is a shortcode for display recent post
and home-content.php for showing post
<?php
$post_id = 7;
$queried_post = get_post($post_id);
?>
<p><?php $check=$queried_post->post_content; ?></p>
<?php echo do_shortcode('["'.$check.'"]');?>
All the recent post display on home page of my custom theme http://templategraphy.com/wp-demo/businessguru/
but the problem is theme structure is not properly shown i
want this type of structure is shown http://templategraphy.com/demo/businessguru/
suggest some solution where i am doing wrong.
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'];
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