Tag Info

Hot answers tagged

4

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") ); ...


4

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.


3

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


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

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


2

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

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

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


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

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


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


1

In the function widget( $args, $instance ), which generates the widget output, add the following code before the echo $after_widget; line: // The Query $the_query = new WP_Query( array( 'post_type' => 'page', 'posts_per_page' => '3' ) ); // The Loop while ( $the_query->have_posts() ) : $the_query->the_post(); echo '<li>' . ...


1

Assuming this is the default main query, see the example for pre_get_posts, which shows how to exclude a category from the main query: function wpa78465_exclude_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-1,-1347' ); // IDs of categories to exclude } } add_action( ...


1

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() && ...


1

The problem looks to me to be this line: $latest_items = WP_query( 'post_type' = 'ait-dir-item', 'cat=0&posts_per_page=' . $number ); That is very odd. First, you are missing the new keyword. Without it you get an "undefined function" error. Second, with WP_Query, and a number of other functions, you can use a query-string-like function argument, or ...


1

Neither of those are complete loops. That is, in both you have the start of an if but not the end of it, and the start of a while loop, but no the end of it. Also, you shouldn't be using query_posts either, as it over writes the main query and is rarely the right function for pulling posts. get_posts would be better. I would do this, using one as an ...


1

If you look at the source of setup_postdata() you'll find that it requires an object ($post), to be passed, not an array. wp_get_recent_posts() (source), by default (for pre 3.1 backwards compatibilty) returns each post as an array. The second, optional argument, that can be passed to wp_get_recent_posts() can prevent this: $posts = wp_get_recent_posts( ...


1

First, don't use query_posts(). Just don't: It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats ...


1

You can use the wp_trim_excerpt() to get the excerpt of the content. You can use it inside your loop to get the excerpt of each post the string provided to a maximum of 55 words if it is more then a [...] will be added to the end of the string. <?php $recent_posts = wp_get_recent_posts(array('post_type'=>'jokes')); foreach( $recent_posts ...


1

There's a guide for this precise case at the wordpress codex. See it here: I paste the code here because it's quite short, for more information go to the wordpress.org site. <?php $args = array( 'numberposts' => 10, 'order'=> 'ASC', 'orderby' => 'title' ); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ...


1

To get the terms (from a custom taxonomy called 'my-taxonomy-name') associated with to a post with ID $post_id: $terms = get_the_terms( $post_id, 'my-taxonomy-name' ); That returns an array of term objects. (see Codex) Pick the first one, say: $term-slug = $terms[0]->slug; And then query using get_posts, it accepts our taxonomy as a key (see $args = ...


1

Have you tried using get_the_terms()? Quick-and-dirty, from your code example: <?php global $post; $terms = get_the_terms( $post->ID, 'some-term' ); foreach ($terms as $category) : ?> <h3>More News From This Category</h3> <ul> <?php $posts = get_posts('numberposts=20&category='. $category->term_id); foreach($posts as ...


1

Do a WP_User_Query and then go through the results: $wp_user_search = new WP_User_Query( array( 'role' => 'author', 'fields' => 'all_with_meta' ) ); $editors = $wp_user_search->get_results(); echo '<pre>'; print_r( $editors ); echo '</pre>';



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