Tag Info

Hot answers tagged

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


1

This code could work, I'm using the setup_postdata to create a post object similar to what the function the_post() does, so now you can use the functions you have in a Loop. $recent_posts = wp_get_recent_posts( array( 'numberposts' => 3, 'orderby' => 'post_date', 'order' => ...


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

Per @TheDeadMedic, I have adjusted my code. Hopefully it will now work on all non-admin queries. add_action('pre_get_posts', 'wpa_136017' ); function wpa_136017( $wp_query ) { //$wp_query is passed by reference. we don't need to return anything. whatever changes made inside this function will automatically effect the global variable $excluded = ...


1

This is what i would use to exclude categories from the categories widget function widget_categories_args_filter( $cat_args ) { $cat_args['exclude'] = array(1,2,3); return $cat_args; } add_filter( 'widget_categories_args', 'widget_categories_args_filter', 10, 1 ); There are NO filters to exclude categories from the recent posts or recent comments ...


1

Here is a start. This is how I display related posts from the category the post are in. You will just need to add your own loop and CSS Create a template called content-related.php Inside that paste the following code <?php /** * The default template for displaying realted posts * * @package WordPress * @subpackage Pieter Goosen * @since ...


1

I'm thinking you are just missing a wp_reset_postdata() call after you foreach loop. Take a look at get_posts(), which is used by wp_get_recent_posts(), to see an example usage. Besides that you do not need the if (have_posts()) and the_post(); in your widget code. But should make use of setup_postdata(). Edit: Example: $argum = array( ...


1

I would tend not to go with wp_get_recent_posts or even get_posts for custom queries like this. By default, template tags like the_excerpt() is not available to these functions, and you have to make use of setup_postdata($post) to have access to these template tags. I would personally use WP_Query in a case like this which is more flexible, specially when ...


1

You have to reset every instance of a custom query, otherwise you will get unexpected output from any other query there after Simply use wp_reset_postdata() after every custom query. Example <?php $args = array( 'numberposts' => '1', 'meta_key'=>'_thumbnail_id' ); $recent_posts = wp_get_recent_posts( $args ); foreach($recent_posts as ...



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