Tag Info

New answers tagged

0

Function for get all category and their post for a particuler parent category function get_cat_subcat_posts($catid){ $categories = get_categories('child_of='.$catid); if(!empty($categories)): foreach ($categories as $category) { $category_id = $category->term_id; ...


1

You can use pre_get_posts action to exclude categories from search query. function wcs_exclude_category_search( $query ) { if ( is_admin() || ! $query->is_main_query() ) return; if ( $query->is_search ) { $query->set( 'cat', '-22, -21' ); } } add_action( 'pre_get_posts', 'wcs_exclude_category_search', 1 ); You should paste this ...


1

There is: the_category(), which not only seems to be all to fittingly named. Aside from that, don't use query_posts, as for the why take a look at those two threads: When to use WP_query(), query_posts() and pre_get_posts When should you use WP_Query vs query_posts() vs get_posts()?


0

You can use pre_get_posts action to exclude categories from homepage. function wcs_exclude_cat( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-23' ); } } add_action( 'pre_get_posts', 'wcs_exclude_cat', 1 ); Change featured category id in above code (with minus sign). And to exclude ...


0

You have a problems here using get_the_time. When using get_the_time( $d, $post ) outside of the loop, you should specify the post ID I would probably make use of the_title filter to dynamically change the_title() dynamically. This way you would not need to make changes to template files. You can try something like this: Please Note: This is untested ...


0

get_the_time('U') returns $string. You cannot subtract a string from an integer function new_badge(){ if ( (time() - strtotime ( get_the_time( 'U' ) ) ) <= (3*86400)) { echo '<div class="new"></div>'; } }


3

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


0

Atlast done it fully $categories = get_the_category($post->ID); foreach($categories as $category) : $children = get_categories( array ('parent' => $category->term_id )); $category_link = get_category_link($children); $has_children = count($children); if ( $has_children == 0 ) { $name=$category->name; ...


2

Just a quick outlined draft of an idea, so beware I haven't thought and tested this through entirely. I'm certain though that you can perform sorting actions this way. Ok, what I was thinking is, why not hooking into the_posts and use usort to do the sorting. All the rest, especially the conditionals are just to make sure that this isn't performed on every ...


0

you can use n2br for that echo n2br(category_description( $category_id ));


0

Well, that was interesting and unexplained. I went to wp-admin/edit-tags.php?taxonomy=category and added a new dummy subcategory under a top level category, and then deleted it. Lo and behold, both of my problems are gone. Visit www.whatsthatbug.com and see for yourself if you wish. I have no idea what caused the problem or why that would have fixed it, but ...


0

From what I understand, there is a list of parent categories and the subcategories, which are like a menu. For your first screenshot: Cat1 -Subcat1 -Subcat2 -Subcat3 Cat2 -Subcat1 -Subcat2 -Subcat3 Assuming you only have two levels of categories, you would use this to display your categories. Note that you can use this outside the loop: <ul ...


2

WordPress' terms and taxonomies are spread over three tables in the database. Those tables are wp_terms, wp_term_relationships and wp_term_taxonomy - see the codex article on Database Description for more details. The parent relationship is part of the wp_term_taxonomy table, so you should inspect the differences between your environments in this table. ...


0

Here's what I used to disable the parent categories and only allow one category per post. This code changes the quick edit categories, too. I welcome someone to clean it up, I'm not confident in javascript. I got the first part here: https://wordpress.org/support/topic/making-category-selection-radio-buttons /* ONLY ALLOW ONE CATEGORY PER POST */ ...


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


1

There's no WordPress-specific way of doing this - unless you have Stream installed. If you don't, you might want to think about installing it.


0

Figured out the answer! It turns out that in ACF, if you're using radio buttons and have key set for each value, the keys CAN in fact contain spaces! By matching the keys to the values (case-insensitive) I was able to get the conferences to display on the single-team posts.


0

If you store your last post ID in a variable from the first loop you can use post__not_in in the second loop to exclude the Id. Example adapted from Wordpres Class Reference/WP Query $query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( $excluded_post_id ) ) ); You can exclude any number of posts by ...


0

The proper control structure would be a switch case, it makes for more readable and maintainable code. Here is an example within a loop. <ol class="news-list"> <?php while (have_posts()) : the_post(); ?> <li> <a href="<?php the_permalink(); ?>"> <span class="post-date"> ...


0

You can make use of the conditional tag has_catgeory. You can do something like this if(has_category( 'cars' ) || has_category( 'uncategorized' )) { //display cars image }elseif(has_category( 'banana' )) { //display banana image } From your comments, here is your code <?php if(has_category( 'Update' ) || has_category( 'uncategorized' )) { ...


0

For those looking for an answer: My meta fields id's in functions.php had a variable @prefix applied before the name. I added that prefix on the template and, voile! Still unsure why my homepage template was working correctly with the wrong meta id's, but an answer nonetheless.


0

You have to use this function: get_post_custom_values The first parameter is the name of the key, arata_in_listare, and the second the post id, depending on where are you making the call you might not need to pass it. In this community we don't give support for plugins, so I'm showing you the "wordpress" way of doing this.


2

You can make use of the hide_empty argument of get_terms(), it defaults to true. Do it somewhat like this: $args = array( 'hide_emty' => false ); $terms = get_terms( 'wpsc_product_category', $args );


0

I have used ACF - Advanced Custom Fields for this multiple times ( http://www.advancedcustomfields.com/resources/how-to-get-values-from-a-taxonomy-term/ ). The problem with taxonomy images & other custom fields is that the values are stored in wp_options table. In bigger sites this will populate the wp_options table a lot and is probably not the most ...


-1

I've used this plugin https://wordpress.org/plugins/categories-images a number of times and its great for adding images to categories. Hope this does what you need! When the plugin is activated on your website and you've added in images for your categories use the following in category.php to output the category images: <ul> <?php foreach ...


0

Thanks to @Layka above, I tweaked the code slightly and this does what I needed. It could perhaps be any filter called around the same time - not sure. /** * Redirect 'category/news-articles' category to 'News page' ( at http://www.mysite.com/news' ) * */ add_filter('term_link', 'term_link_filter', 10, 3); function term_link_filter( $url, $term, ...


1

add_filter('term_link', 'term_link_filter', 10, 3); function term_link_filter( $url, $term, $taxonomy ) { if ( is_category( 'news-article', $term ) ) { wp_safe_redirect( 'http://mysite.com/news', 301 ); exit; } return $url; } This should work.


1

You'll need to do at least two things (possibly more, I haven't thought it all the way through), in it's simplest form: Prevent category output lists from creating links: You'd need to emulate get_the_category_list() and build your own category list without anchors, or you could probably just filter out the <a> tags: add_filter( 'the_category', ...


0

You can add something like this in your category.php. if ( ! is_admin() ) { wp_redirect( home_url() ); exit; } This will redirect viewers to website homepage but category pages will be active and accessable to admins only.


-2

<?php $related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 5,'post_type' => 'news', 'post__not_in' => array($post->ID) ) ); if( $related ) foreach( $related as $post ) { setup_postdata($post); ?> <li> <?php the_post_thumbnail(); ?> <a ...


0

get_the_category returns an array with all the categories $categories = get_the_category(); foreach ($categories as $category){ echo '<a class="post-item" href="' . get_category_link($category->term_id) . '">' . $category->cat_name . '</a>'; } Tha code should work, you can modify it to add the title Also, calling the_category echoes ...


1

I believe you are using the following code from this pastebin link function posts_by_year() { // array to use for results $years = array(); // get posts from WP $posts = get_posts(array( 'numberposts' => -1, 'orderby' => 'post_date', 'order' => 'ASC', 'post_type' => 'my-custom-post-type', 'post_status' => ...


0

When trying to link to one category archive, you could use get_category_link(). Example: // Use the ID: echo get_category_link( $category_ID ); // or: Category Object: echo get_category_link( $category_object ); The result will be either an string or an empty string. So if you are planning to wrap this in some HTML MarkUp, better make sure to check for ! ...


1

To get direct descendants (i.e. children only) of the category, you need to make use of the parent parameter in get_categories() The reason why you might not see your other category is that it does not have any posts assigned to it. To display empty categories, you need to set the hide_empty parameter to 0 Otherwise I do not see any problems with your ...


-1

use it for comment number <?php comments_number( 'no responses', 'one response', '% responses' ); ?>


0

You will have to use this function in loop. "Displays the total number of comments, Trackbacks, and Pingbacks for the current post. This tag must be within The Loop." And also you will need to use this. <?php comments_number( '0', '1', '%' ); ?> Replace it with yours <?php comments_number( $zero, $one, $more ); ?>


0

The primary thing you'll need to use here is the save_post hook in WordPress to execute your code right after a post is saved. Read up on the save_post hook on the WordPress codex first and then come back here. The next part is assigning the post to a taxonomy term. This can be done easily with wp_set_post_terms() function. You'll end up with something ...


0

Do not use query_posts, and also do not replace the main query with a custom query. This causes more issues than solving the one issue you had before with the main query. I've done a complete post on these issues recently that you can check out here According to your query, the only two things you need to change is posts_per_page and order to do this, you ...


0

It's more readable and just generally more WordPress-y to use the built-in term_description() function! <?php echo term_description( {term_id}, '{taxonomy}' ); ?>


1

There is no default way to sort terms randomly. There are ways to do this using php. First, you'll need to remove the number argument from get_terms. As your code currently stands, you are getting 5 terms and shuffling them around. For this to work, you'll need to retrieve all the terms from your taxonomy, shuffle that returned array randomly with the php ...


1

From your functions file function remove_home_category( $query ) { if ( $query->is_home() && $query->is_main_query() ) { $query->set( 'cat', '-4' ); } } add_action( 'pre_get_posts', 'remove_home_category' ); This code alters the query before the actual query is run so is the most efficient hook to modify the loop in this ...


2

You should check for category posts to modify content. Specify category ID (integer), name or slug (string), or an array of these in in_category check. function my_image_tag( $html, $id , $alt, $title ) { if ( in_category( '1' ) ) { $html = "<div class='my-class'>" . $html . "</div>"; } return $html; } add_filter( ...


0

Two things! 1) Don't use query_posts()! Instead, for situations like this, use the pre_get_posts action to modify the query. 2) I don't think you actually want to be using a loop here at all (so neither query_posts() nor pre_get_posts or even WP_Query). Rather, I think you're looking for wp_list_categories. In your template, you'd probably do something ...


1

Create a page template and use wp_query to target the category you want to display $args = array('category_name' => 'your category name',); WP_Query will help you with all sorts of options for what posts are returned and get displayed by your loop. Here is an example of a page template, (this is based on the _s theme) <?php /** * Template ...


0

For the extract, add the category parameter: extract( shortcode_atts( array( 'limit' => 7, 'type' => 'g1_event', 'category' => '', ), $atts)); In your WP_Query add: 'category_name' = $category For your shortcode use: [recent-events category="slugName"]


1

The code you need will vary slightly depending on whether the Conference field is storing a text string (like "Big Ten") vs. the ID of the Conference post. I'll include both: Text String Example // place this code inside the loop of your single-team template $conference_name = get_post_meta( get_the_id(), 'conference', true ); $conference = ...


0

You dont need to install plugins to do this. Go to Settings-->Permalinks-->Custom Structure--> And now set this custom structure www.example.com/%category%/%postname%/ It will do all the work of redirecting unless your htaccess has the write permissions.


1

This can be done easily with $wpdb, here's my approach: function GetTop2CategoryByUser($user_id, $taxonomy){ global $wpdb; $results=$wpdb->get_results( $wpdb->prepare( " SELECT tt.term_id as category, COUNT(p.ID) as count FROM $wpdb->posts p JOIN $wpdb->term_relationships tr ...


0

<?php $key = 'user_top_categories'; // User meta key for the top categories $single = false; // Array or String $top_categories = get_user_meta( $user_id, $key, $single ); foreach($top_categories as $top_cat){ $term = get_term_by('name', $top_cat, 'category'); $term_link = get_term_link( $term ); echo '<li><a href="' . ...


0

The correct way to do this is to make use of pre_get_posts which change the query vars just before the main query is executed. Here is an example from the codex to exclude categories from the home page/blog page. For a full list of available parameters and values that you can use with pre_get_posts, visit the WP_Query codex page function exclude_category( ...



Top 50 recent answers are included