Sometimes as I code a page I find it necessary to use some specific html to get the job done. the problem is that when I hand the site over to the client and they start editing in the Visual editor there is a danger that they will inadvertently over-write my html (which isn’t visible […]
Display Posts by Category [shortcode]
Suppose we want to display a list of post links in the sidebar that are in “child” categories of the “News” category (ie “Boating News” followed by its posts, “Fishing News” followed by its posts, in this case from the last 90 days because of the filter). We could include this code in functions.php:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
add_filter('widget_text', 'do_shortcode');//FOR WIDGET SHORTCODE function filter_where($where = '') { //posts in the last 90 days $where .= " AND post_date > '" . date('Y-m-d', strtotime('-90 days')) . "'"; return $where; } function jma_news_posts($atts){ ob_start(); extract( shortcode_atts( array( 'parent_cat_id' => 3, 'number_displayed' => 3, ), $atts ) ); add_filter('posts_where', 'filter_where'); $children = get_categories( array('child_of' => $parent_cat_id) ); foreach ($children as $child) { echo '<h3 class="news-title"><a href="'; echo get_category_link( $child->cat_ID ); echo '">'; echo $child->cat_name; echo '</a></h3>'; $x = new WP_Query('category_name=' . $child->slug . '&posts_per_page=' . $number_displayed); echo '<ul class="news-bar">'; while ( $x->have_posts() ) : $x->the_post(); echo '<li class="sidebar-recent-posts">'; echo '<a class="side-post-title" href="'; the_permalink(); echo '">'; the_title(); echo '</a></li>'; endwhile; echo '</ul>'; } // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); $x = ob_get_contents(); ob_end_clean(); return $x; } add_shortcode('add_news_with_kids','jma_news_posts'); |
[…]
Add Hooks to Alyeska (all Theme Blvd themes) Pages
I love the Theme Blvd Themes. Plenty of user friendly features, like drag and drop templates and easy to use sliders. One thing I was missing was action hooks at the top and bottom of the page content. There is a themeblvd_content_top hook, but it is outside of entry-content. I wanted something closer in. Here […]
CSS3 Drop Shadows
This is simple stuff, but good code to keep handy. This covers all current major browsers and add backward compatibility for ie. The first number is horizontal shift (right is positive), 2nd number is verticle shift (down is positive), 3rd is radial blur and forth is, in this case hexadecimal color (#3d424d is a dark […]
Register Custom Post Type and Taxonomy
This is the standard code to register (create) a custom post type. For more detailed information on these and other options check out the WordPress Codex page.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
function register_post_types() { $labels = array( 'name' => _x( 'Practitioners', 'themeblvd' ), 'singular_name' => _x( 'Practitioner', 'themeblvd' ), 'add_new' => _x( 'Add New', 'themeblvd' ), 'add_new_item' => _x( 'Add New Practitioner', 'themeblvd' ), 'edit_item' => _x( 'Edit Practitioner', 'themeblvd' ), 'new_item' => _x( 'New Practitioner', 'themeblvd' ), 'view_item' => _x( 'View Practitioner', 'themeblvd' ), 'search_items' => _x( 'Search Practitioners', 'themeblvd' ), 'not_found' => _x( 'No practitioners found', 'themeblvd' ), 'not_found_in_trash' => _x( 'No practitioners found in Trash', 'themeblvd' ), 'parent_item_colon' => _x( 'Parent Practitioner:', 'themeblvd' ), 'menu_name' => _x( 'Practitioners', 'themeblvd' ), ); $args = array( 'labels' => $labels, 'hierarchical' => false, 'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail' ), 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'rewrite' => array( 'slug' => 'practitioners'), 'capability_type' => 'post' ); register_post_type( 'practitioner', $args ); } function register_custom_taxonomies() { $labels = array( 'name' => _x( 'State Name', 'themeblvd' ), 'singular_name' => _x( 'State', 'themeblvd' ), 'search_items' => _x( 'Search States', 'themeblvd' ), 'popular_items' => _x( 'Popular States', 'themeblvd' ), 'all_items' => _x( 'All States', 'themeblvd' ), 'parent_item' => _x( 'Parent State', 'themeblvd' ), 'parent_item_colon' => _x( 'Parent State:', 'themeblvd' ), 'edit_item' => _x( 'Edit State', 'themeblvd' ), 'update_item' => _x( 'Update State', 'themeblvd' ), 'add_new_item' => _x( 'Add New State', 'themeblvd' ), 'new_item_name' => _x( 'New State', 'themeblvd' ), 'add_or_remove_items' => _x( 'Add or remove States', 'themeblvd' ), 'choose_from_most_used' => _x( 'Choose from most used States', 'themeblvd' ), 'menu_name' => _x( 'States', 'themeblvd' ), ); $args = array( 'labels' => $labels, 'public' => true, 'show_in_nav_menus' => true, 'show_ui' => true, 'show_tagcloud' => true, 'hierarchical' => true, 'rewrite' => array( 'slug' => 'states'), 'query_var' => 'state' ); register_taxonomy( 'state', array('practitioner'), $args ); $labels = array( 'name' => _x( 'Certification Name', 'themeblvd' ), 'singular_name' => _x( 'Certification', 'themeblvd' ), 'search_items' => _x( 'Search Certifications', 'themeblvd' ), 'popular_items' => _x( 'Popular Certifications', 'themeblvd' ), 'all_items' => _x( 'All Certifications', 'themeblvd' ), 'parent_item' => _x( 'Parent Certification', 'themeblvd' ), 'parent_item_colon' => _x( 'Parent Certification:', 'themeblvd' ), 'edit_item' => _x( 'Edit Certification', 'themeblvd' ), 'update_item' => _x( 'Update Certification', 'themeblvd' ), 'add_new_item' => _x( 'Add New Certification', 'themeblvd' ), 'new_item_name' => _x( 'New Certification', 'themeblvd' ), 'add_or_remove_items' => _x( 'Add or remove Certifications', 'themeblvd' ), 'choose_from_most_used' => _x( 'Choose from most used Certifications', 'themeblvd' ), 'menu_name' => _x( 'Certifications', 'themeblvd' ), ); $args = array( 'labels' => $labels, 'public' => true, 'show_in_nav_menus' => true, 'show_ui' => true, 'show_tagcloud' => true, 'hierarchical' => true, 'rewrite' => array( 'slug' => 'certifications'), 'query_var' => 'certification' ); register_taxonomy( 'certification', array('practitioner'), $args ); } function my_cpt_init() { register_post_types(); register_custom_taxonomies(); } add_action( 'init', 'my_cpt_init' ); function my_rewrite_flush() { flush_rewrite_rules(); } add_action( 'after_switch_theme', 'my_rewrite_flush' ); |
Notice “hierarchical” is set to true. This will cause the taxonomy to behave like a category. False or omitted causes the taxonomy to behave like a tag.
Sticky Posts in Category Pages
How do we get sticky posts to “stick” to category pages? The answer to this question turned out to be much more involved than I had imaged. After scouring the internet/codex I managed to found this solution.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/* stickies to top for news category (34) */ function jma_child_sticky_posts_to_top($posts, $q) { //check to see if it's the news cat //for specific cat $query_vars['cat'] !== 34 //for all cat use (!isset($query_vars['cat'])) $query_vars = $q->query_vars; if (!isset($query_vars['cat'])) { return $posts; } $stickies = array(); foreach ($posts as $i => $post) { if (is_sticky($post->ID)) { $stickies[] = $post; unset($posts[$i]); } } return array_merge($stickies, $posts); } add_filter('the_posts', 'jma_child_sticky_posts_to_top', 10, 2); |
Check for Children
Want to be able to use one condition which includes or excludes pages based on weather they are children of a specific page? Add this code to functions.php:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function is_kid( $pid,$includeparent = 0 ) { // $pid = The ID of the page we're looking for pages underneath //$includeparent = 1 to include the parent page, 0 to exclude global $post; // load details about this page if ( is_page($pid) ) return $includeparent; // we're at the page $anc = get_post_ancestors( $post->ID ); foreach ( $anc as $ancestor ) { if( is_page() && $ancestor == $pid ) { return true; } } return false; // we aren't at the page, and the page is not an ancestor } |
then use this in your page template:
0 1 2 3 4 5 |
if (is_kid($page_id,1)) //1 to include the parent page, 0 to exclude { code here } |