Tag Info

Hot answers tagged

17

Taxonomies, as previously described are a collective noun for the following category post_tag post_format link_category custom taxonomy The first four are built-in taxonomies, while custom taxonomies are taxonomies that are manually created by the user with register_taxonomy. Custom Taxonomies can be hierarchical (like the build-in taxonomy category) or ...


6

The Automatic Sponsor Posts Injector: Here's one idea based on my answer for the question: How to show Y number of custom posts after every X normal posts? I hopefully made it a little bit more useful here on Github, but it may be refined much more (future work). The SponsorPostsInjector class will help you to automatically inject the sponsor posts into ...


6

EDIT REVISIT NO 2 I have never touched the Transient API, until today when I saw @MikeSchinkel answer in this post. This inspired me to revisit this post once again. After some testing, I came up with the following: Time to execute went down from ~0.07 seconds to ~0.002seconds Database query time went down by about halve With the transient, only 2 db ...


5

Here's a simplified combination of both @PieterGoosen and @ialocin fine answers by using the loop_start hook: add_action( 'loop_start', function( $q ) { if( $q->is_main_query() && $q->is_category( 'car' ) ) usort( $q->posts, function( $a, $b ){ return -1 * has_tag( 'bmw', $a ) + 1 * has_tag( 'bmw', $b ); ...


5

Category, Tag, Link Category and Post Formats are built in taxonomies. From Codex Basically, a taxonomy is a way to group things together. The names for the different groupings in a taxonomy are called terms. Using groupings of animals as an example, we might call one group "birds", and another group "fish". "Fish" and "birds" are terms in our ...


4

You can use the category__in argument: // use get_the_category( $post_id ) if you are outside the loop $categories = get_the_category(); if( $categories ){ $related_posts = get_posts( array( 'category__in' => $categories ) ); }


4

Excluding categories with pre_get_posts() I found that excluding a category via pre_get_posts() and set_query_var() would work fine except for widgets. The Recent Post Widget would only exclude the category when using $query->set() instead. <?php /** * Does NOT apply to the Recent Posts widget. */ function glck1403271109_exclude_categories( $query ...


4

Your terminology is a bit scrambled here. A custom taxonomy is a taxonomy which you manually register through register_taxonomy. In your case that is ait-dir-item-category. Any "category" (as you call it) you create in your custom taxonomy is called a term. In this case events. To create a custom page for events, you need to name your template ...


4

This should hopefully do the trick: /** * Append the image categories to the current image class. * * @see http://wordpress.stackexchange.com/a/156576/26350 */ add_filter( 'get_image_tag_class', function( $class, $id, $align, $size ) { foreach( (array) get_the_category( $id ) as $cat ) { $class .= ' category-' . ...


4

Here is the code to get category based previous and next links on posts $post_id = $post->ID; // current post id $cat = get_the_category(); $current_cat_id = $cat[0]->cat_ID; // current category Id $args = array('category'=>$current_cat_id,'orderby'=>'post_date','order'=> 'DESC'); $posts = get_posts($args); // get ids of posts retrieved ...


4

You can use get_categories() function. <?php $cats = get_categories(); $count = 15; // Total Categorie $linetotal=round($count/3); ?> // Part of Categorie <div class="category_list"> <ul><?php $totalcount = 1; $totalpart = 1; foreach($cats as $cat): ?> <li><?php echo $cat->name; ...


3

You can make use of the hide_empty argument of get_terms(). It's default value is set to true. Do it somewhat like this: $args = array( 'hide_empty' => false ); $terms = get_terms( 'wpsc_product_category', $args );


3

You cannot do this type of sorting inside the loop. You can however be done by writing your own function Here is my solution: PS! Needs PHP 5.4+ STEP 1 Create a function called wpse161553_loop_sort($tag=''). This will work with a single tag with the main query. STEP 2 You need to get the post objects from the main query. They are already available and ...


3

You have a couple of flaws here and also a few places where you can optimize the code Instead of using get_the_category(), use wp_get_post_terms(). It is a bit faster, and you have the option to just get the term ID's from the post categories. This is one place where you can optimize your code ID is not a valid property of get_the_category(), it should be ...


3

A lot of time since this quesiton and answer were posted. Since then things has changed a lot. The typical recommendation about disallow crawlers to access wp-content/themes, wp-content/plugins, wp-content/cache, wp-includes, and any other directory that contains CSS or js files needed in the site, are no longer valid. For example, lets talk about Google. ...


2

You can access to default category ID via default_category option: $default_category = get_option('default_category'); Now you can use selected() function to generate the selected attribute: <?php $args = array( 'type' => 'post', 'hide_empty' => 0 ); $categories = get_categories( $args ); $default_category = ...


2

This same exact question was asked earlier this week or over the weekend, and it had me thinking. Here is the idea that I came up with. If you look at the source code of the WP_Query class, you will see that sticky posts is only added to the first page of the home page. There is also no filter supplied to change this behavior in order to set the required ...


2

If you look at wp_get_archives() you will notice that the link is generated by get_archives_link(). That function supplies a filter that will allow you to replace the parens. This is fairly crude but does work. function archive_link_wpse_183665($link) { $pat = '|\(([^)])\)</li>|'; // preg_match($pat,$link,$matches); // var_dump($matches); ...


2

Use the terms_clauses filter, which passes all the various components of the query (fields, join, where, orderby, order & limits), and implement your own "search" argument: function wpse_178511_get_terms_fields( $clauses, $taxonomies, $args ) { if ( ! empty( $args['surname'] ) ) { global $wpdb; $surname_like = $wpdb->esc_like( ...


2

I would recommend using the category_template filter - just check if the current category is an ancestor of 67: function wpse_179617_category_template( $template ) { if ( cat_is_ancestor_of( 67, get_queried_object_id() /* The current category ID */ ) ) $template = locate_template( 'category-slider.php' ); return $template; } add_filter( ...


2

Use the hooks added_term_relationship and deleted_term_relationships. These only fire when the relationship changes, as opposed to set_object_terms which always fires: function wpse_181090_object_terms_updated( $object_id ) { static $did = array(); // This function might fire multiple times for the same object, ensure it only runs once if ( ! ...


2

I have recently written a widget that Gets all the terms from the build in taxonomy category and adds them in dropdown in the widget Stores the selected category and uses that category in a custom WP_Query to get and display posts from the selected category You can look through it and adjust as needed: (Required PHP 5.4+) (EDIT: Thanks @birgire for ...


2

I think you have misinterpreted custom post-types and custom taxonomies, somewhat. Theory Custom Post Types I consider the purpose of a custom post-type to be format of data distinct in it's own right - it describes a type of content, but not the content itself. Take the in-built page, post, and attachment post-types, for example: The page post-type ...


2

You can just grab the first post from the main query and see which post type it is: if ( have_posts() ) { $post_type = $wp_query->posts[0]->post_type; } If you run this code directly in a main template file you should be fine, but if it's in a function you will need to call global $wp_query; first.


2

// Getting Parent ID $parent = get_term_by( 'name', 'Session 2015', 'category'); // Checking if SubCategory Exist. $term = term_exists('Photos', 'category', $parent->term_id); //if Exist Do the query if ($term !== 0 && $term !== null) { $query = new WP_Query( 'cat='.$term['term_id'] ); } References: term_exists get_term_by


2

As documented in the codex you could use the following $query = new WP_Query( array( 'category__and' => array( 2, 6 ) ) ); //post has to be in category with ID 2 AND 6


2

As I already stated, straight from the codex exclude (string) Excludes one or more categories from the list generated by wp_list_categories. This parameter takes a comma-separated list of categories by unique ID, in ascending order As you have stated, you have to use the category slug. To make this possible and dynamic, I think the best will be to ...


2

I'm a bit unsure of you are actually asking, so apologies if I understood it wrong. You can use get_category() to get the category object and then just simply echo the $count property value $cat_count = get_category( 'ID OR ROW OBJECT' ); echo $cat_count->count;


2

Fist error I see is a syntax error: depth => 0 should be "depth" => 0 Second error is that you are using a category object like a post object. None of these lines will work: There is no post data to setup. Remove this line: setup_postdata($category); A category object has no category_modified property. Maybe you need the date of the last ...


2

Before I start again, I just want to reinforce the fact you should not run a custom query in place of the main query on your archive pages. Please see this post to learn when you should use a custom query and where not. I you do not switch back to the default loop, then my solution will not work In the previous post we have already solved the issue to only ...



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