Only show category to certain user levels without plugin

If I understand you have some categories, eg: ‘reserved’, ‘people’, ‘landscapes’, ‘personal’ and so on. Now you want that subscribers can see posts in, e.g. ‘people’, ‘landscapes’ categories but not posts in ‘reserved’ and ‘personal’ categories. This is relatively easy, just hook into pre_get_posts and if the request is for a post having that terms … Read more

Dynamically add sub-categories to any category in the menu

you can add them with the filter wp_get_nav_menu_items add_filter(“wp_get_nav_menu_items”, function ($items, $menu, $args) { // don’t add child categories in administration of menus if (is_admin()) { return $items; } foreach ($items as $index => $i) { if (“category” !== $i->object) { continue; } $term_children = get_term_children($i->object_id, “category”); // add child categories foreach ($term_children as $index2 … Read more

disable comments if array exists

Assuming you’re properly using comment_form() to output the comment reply form: if( ! array_key_exists( ‘comments’, $wp_query->query_vars ) ) { // Output the comment form comment_form(); } Edit Based on your comments.php code: // If the user isn’t logged in, don’t display comment form if ( is_user_logged_in() ) { global $wp_query; // If the post type … Read more

class=”parent” for wp_list_pages?

Phew! Bit more complex than I would’ve liked – if only the filter page_css_class passed back $args it would save a lot of effort (may submit this to trac). Since 3.3 it’s as easy as this! function add_parent_class( $css_class, $page, $depth, $args ) { if ( ! empty( $args[‘has_children’] ) ) $css_class[] = ‘parent’; return … Read more

How to activate “Description” metabox for menu item programmatically?

Remove the description from the user meta value managenav-menuscolumnshidden: add_filter( ‘manage_nav-menus_columns’, ‘enable_nav_menu_description_by_default’ ); function enable_nav_menu_description_by_default( $columns ) { $desc_key = ‘managenav-menuscolumnshidden’; $hidden = get_user_option( $desc_key ); $user_id = wp_get_current_user()->ID; if ( ! $hidden ) { update_user_option( $user_id, $desc_key, array ( 0 => ‘link-target’, 1 => ‘css-classes’, 2 => ‘xfn’ ) ); } elseif ( FALSE … Read more

Counting number of posts in a category and its sub categories and displaying result using shortcode

The shortcode // Add Shortcode to show posts count inside a category function category_post_count( $atts ) { $atts = shortcode_atts( array( ‘category’ => null ), $atts ); // get the category by slug. $term = get_term_by( ‘slug’, $atts[‘category’], ‘category’); return ( isset( $term->count ) ) ? $term->count : 0; } add_shortcode( ‘category_post_count’, ‘category_post_count’ ); Usage … Read more