Tag Info

Hot answers tagged

10

Have a look in wp-includes\default-widgets.php. /** * Recent_Posts widget class * * @since 2.8.0 */ class WP_Widget_Recent_Posts extends WP_Widget { function __construct() { $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") ); ...


6

The default Recent Posts Widget code is in includes/default-widgets.php but you should not be hacking Core code. Copy that function to your theme's functions.php, rename it, and create your own customized widget.


4

http://codex.wordpress.org/Function_Reference/get_comments#Parameters your problem is using author_email, you need user_id i just use similar script. <?php $args = array( 'user_id' => $user->ID, 'number' => 10, // how many comments to retrieve 'status' => 'approve' ); $comments = get_comments( $args ...


4

Don't use the 'helper' methods, they tend to cause more trouble than they're worth. Any time you want to grab posts, be it the latest, the oldest, in a category etc, use a WP_Query loop, here is its standard form: $query = new WP_Query( $args ); if ( $query->have_posts() ) { while ( $query->have_posts() ) { $query->the_post(); ...


3

Give it a try works perfect for me what it is doing query get the all the posts with a left jon with comments table so when a post has comment then it also has the comment_date if no comments posted on the post then in result set it will be null so i have merged the comment_date with post_date so which post has the greater date (for comment_date or ...


3

The original author isn't quite right in saying "which is merely the more elegant way to write". set_query_var() will always override the main query, whereas if you actually use: $wp_query->set( 'category__not_in', $excluded ); ... it will work for any instance of query_posts(), such as the recent posts widget.


3

I would take a different approach to this using a direct database query to get a list of author IDs ordered by recent posts. global $wpdb; $user_ids = $wpdb->get_results( " SELECT DISTINCT post_author FROM $wpdb->posts ORDER BY post_date DESC " ); if ( $user_ids ) { foreach ( $user_ids as $user_id ) { $user = ...


3

Your problem is your custom query. Do not use variables that is used by default by Wordpress. $posts and $post is Wordpress reserved global variables. Change $post to a unique variable, something like $posts_query. When using get_posts and you need to setup postdata to make use of template tags, you have no choiche but to use $post as setup_postdata() ...


3

Try: $postslist = get_posts( 'orderby=ID&numberposts=15' ); Here, we're ordering by the ID, which should relate to when the posts were added to the database.


3

You need to get the post ids from the original main query and exclude them. Then you should remove your offset from your custom query. That should do the trick. Random ordering basically ignores the offset parameter, so you need to explicitely remove the posts from the query to exclude them You can use wp_list_pluck() to get an array of post ids from the ...


3

You could use your list of blog ids in this way ... $posts = array(); foreach ( $your_list_of_blog_ids as $blog_id ) { switch_to_blog( $blog_id ); $query = new WP_Query( array( 'post_type' => 'any', 'posts_per_page' => 10, ) ); while ( $query->have_posts() ) { $query->next_post(); ...


3

Inside your post loop: <div class="recent-post"> <div class="thumbnail"> <?php if(has_post_thumbnail()) { the_post_thumbnail(); } else { // show a default image if no featured image is specified echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />'; ...


3

First of all learn WP_Query class. Answering on questions: (this question) How do I grab the nth element of a content type? eg. always getting the 1st or 5th most recent element from the db. $query = new WP_Query( 'post_type=special_stuff&posts_per_page=1&paged=5' ); List item // The Query $the_query = new WP_Query( $args ); // The ...


3

There are a few ways to do this, my first thought is to do: $query = new WP_Query(array( 'posts_per_page' => 1, 'paged' => 5, 'post_type' => '[your_content_type]', )); while ($query->have_posts()) : $query->the_post(); // loop stuff endwhile;


3

The problem was that the SQL query code was getting the term_taxonomy_id, not the actual tag ID. I added an additional INNER JOIN using the term_taxonomy table to get the term_id. This seems to work, but if a mod can improve this, please do! <ul id="footer-tags"> <?php $wpdb->show_errors(); ?> <?php global $wpdb; $term_ids = ...


2

The mistake is in your first block of code: <?php if (have_posts()): the_post(); ?> <h3> <?php _e('All posts by'); ?> <?php echo get_the_author(); ?> <span class="arrows">&raquo;</span> </h3> <?php while (have_posts()) : the_post();?> You call the_post() to populate the regular ...


2

You'll have to create your own but you can copy the code and customize from /wp-includes/default-widgets.php If you edit the defaults they'll screw up when you update WP.


2

I wasn't sure if i would answer or simply vote to close this topic, i feel i sufficiently asnwered the original topic you're referring to, i'd only be repeating myself here. That said, here's an upto date version you can use(only took me 2 mins), you only need implement your own code to truncate the titles in the widget, i've marked the area of code for ...


2

It kind of depends on what you're going for. If you want to do a "page of posts" -- other words, create a new page template file -- you can create a secondary loop on that page. The codex has an example of this and here's another, very stripped down example. <?php /* Template Name: Page of Posts */ get_header(); ?> <?php while( have_posts() ): ...


2

You can exclude posts from the recent posts widget via the widget_posts_args filter: add_filter( 'widget_posts_args', 'exclude_posts_wpse_103570'); function exclude_posts_wpse_103570( $args ){ // post ID's to exclude: $args['post__not_in'] = array( 123, 234, 345 ); return $args; }


2

You can use the post__not_in parameter of WP_Query to exclude an array of post IDs from the query. get_the_category() returns an array of term objects, so the get_term_by is redundant. Also is_single() will always be true in content-single.php (not strictly speaking true but only not so if you are interferring with the template selection). (Finally, in ...


2

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> <?php the_excerpt() ?> <?php endwhile; else: ?> <p>Nothing to see here.</p> <?php endif; ?> This is the right syntax to show title and the excerpt on home ...


2

This code uses snippet from @Milo to suit your requirement. Replace the code you've provided in question with this - <li> <h2><a href="http://growingedgecoaching.com/blog">Growing Edge Blog</a></h2> <?php $latest = new WP_Query( array( 'posts_per_page' => 1 ) ); while( $latest->have_posts() ) : ...


2

In the admin panel, Go to Settings > Reading and set Blog pages show at most to 1 post.


2

Compare the current time with the UNIX time stamp of the post date: // now minus one day in seconds if ( ( time() - 86400 ) < get_the_date( 'U' ) ) { echo '<span class="new">New!</span>'; }


2

SOLUTION: I added $titles = array(); at the top (outside the loop). Then, within the loop, I used array_push( $titles, get_the_title( $ID ) ); to collect the titles. Then I just checked the array for a matching title before displaying, like this: if ( !in_array(get_the_title( $ID ), $titles ) ) ...then do this And it works!


2

The post_excerpt value is empty because you have no explicit excerpts for your posts. While the_excerpt() does generate an excerpt from the posts content if the post excerpt is empty, the function wp_get_recent_posts(), which is basically a wrapper for get_posts(), doesn't.


2

First, ensure that your template file is named home.php. Second, there's no need to use a custom query loop in this context. If you only want to display 3 posts on the blog posts index (i.e. the "homepage"), then filter the main loop query via pre_get_posts: function wpse83660_filter_pre_get_posts( $query ) { if ( is_home() && ...


2

You can see everything returned via php's var_dump or print_r: $recent_posts = wp_get_recent_posts(); echo '<pre>'; print_r( $recent_posts ); echo '</pre>'; This will reveal that the date is stored in post_date: foreach( $recent_posts as $recent ): echo $recent['post_date']; endforeach; Or to format the date, use php's date and ...


1

You should use posts_per_page=4 instead of showposts=4. Reference: WP_Query Pagination Parameters



Only top voted, non community-wiki answers of a minimum length are eligible