Advertisement
Guest User

Untitled

a guest
Aug 16th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.90 KB | None | 0 0
  1. /**
  2. * Tests if any of a post's assigned categories are descendants of target categories
  3. *
  4. * @param int|array $cats The target categories. Integer ID or array of integer IDs
  5. * @param int|object $_post The post. Omit to test the current post in the Loop or main query
  6. * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
  7. * @see get_term_by() You can get a category by name or slug, then pass ID to this function
  8. * @uses get_term_children() Passes $cats
  9. * @uses in_category() Passes $_post (can be empty)
  10. * @version 2.7+
  11. * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
  12. */
  13. function test_post_is_in_descendant_category( $cats, $_post = null )
  14. {
  15. foreach ( (array) $cats as $cat ) {
  16. // get_term_children() accepts integer ID only
  17. $descendants = get_term_children( (int) $cat, 'category');
  18. if ( $descendants && in_category( $descendants, $_post ) )
  19. return true;
  20. }
  21. return false;
  22. }
  23.  
  24. /**
  25. * Tests if any of a post's assigned categories are in the target categories or in any of the descendants
  26. *
  27. * @param int|array $cats The target categories. Integer ID or array of integer IDs
  28. * @param int|object $_post The post. Omit to test the current post in the Loop or main query
  29. * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
  30. * @see get_term_by() You can get a category by name or slug, then pass ID to this function
  31. * @uses get_term_children() Passes $cats
  32. * @uses in_category() Passes $_post (can be empty)
  33. * @version 2.7+
  34. */
  35. function post_is_in_category_or_descendants( $cats, $_post = null )
  36. {
  37. if( in_category( $cats, $_post = null ) || test_post_is_in_descendant_category( $cats, $_post = null ) ) {
  38. return true;
  39. }
  40. return false;
  41. }
  42.  
  43.  
  44.  
  45. /**
  46. * This function is used to generate custom breadcrumbs for single posts view. Portfolio section or regular Blog is considered
  47. * when generating the link structure.
  48. */
  49. function get_category_parents_for_breadcrumbs( $id, $link = false, $separator = '/' ) {
  50. global $sq_options, $portfolio_pages_array;
  51. $portfolio_cats_array = explode( ',', $sq_options['portfolio_categories'] );
  52. if ( post_is_in_category_or_descendants($portfolio_cats_array) ) { // if the current post belongs to any Porfolio category
  53. foreach ( $portfolio_pages_array as $portfolio_page_obj ) {
  54. $port_page_ID = $portfolio_page_obj->ID;
  55. if ( post_is_in_category_or_descendants( $sq_options['portfolio_cat_for_page_'.$port_page_ID] ) ) {
  56. echo get_category_parents_for_portfolio_page( $id, $link, $separator, FALSE , $port_page_ID );
  57. break;
  58. }
  59. }
  60. } else { // if the current category is a regular blog category
  61. echo get_category_parents( $id, $link, $separator, FALSE );
  62. }
  63. }
  64. /**
  65. * This is the modified version of the "get_category_parents()" WP function
  66. * Retrieve category parents with separator for use in the Portfolio section to generate proper breadcrumb links.
  67. * The new parameter added is $portfolio_page_id which is the id of the page assigned with the Porfolio page template.
  68. * @since 1.2.0
  69. * @param int $id Category ID.
  70. * @param bool $link Optional, default is false. Whether to format with link.
  71. * @param string $separator Optional, default is '/'. How to separate categories.
  72. * @param bool $nicename Optional, default is false. Whether to use nice name for display.
  73. * @param string $portfolio_page_id Optional. Already linked to categories to prevent duplicates.
  74. * @param array $visited Optional. Already linked to categories to prevent duplicates.
  75. * @return string
  76. */
  77. function get_category_parents_for_portfolio_page( $id, $link = false, $separator = '/', $nicename = false, $portfolio_page_id='', $visited = array() ) {
  78. global $sq_options;
  79. $chain = '';
  80. $parent = &get_category( $id );
  81. if ( is_wp_error( $parent ) ) return $parent;
  82. $name = ( $nicename ) ? $parent->slug : $parent->cat_name;
  83. if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
  84. $visited[] = $parent->parent;
  85. $chain .= '<a href="'.get_permalink( $portfolio_page_id ).'" title="'.esc_attr( sprintf( __( "Go back to %s", 'sq' ), get_the_title($portfolio_page_id) ) ).'">'.get_the_title($portfolio_page_id).'</a>' . $separator . ' ';
  86. }
  87. if ( $link ) { // generate comma separated list of categories' links that the current single post has been assigned to
  88. $query_string_prefix = ( get_option('permalink_structure') != '' ) ? '?' : '&amp;';
  89. $categories_names_array = array();
  90. foreach((get_the_category()) as $category) {
  91. if ( ( cat_is_ancestor_of( $sq_options['portfolio_cat_for_page_'.$portfolio_page_id], $category->term_id ) ||
  92. $sq_options['portfolio_cat_for_page_'.$portfolio_page_id] == $category->term_id ) ) { // belongs to a category associated with the current portfolio page
  93. $curr_cat_link = '<a href="'.get_permalink($portfolio_page_id).$query_string_prefix.'cat=' . ( $category->term_id ) . '" title="' . esc_attr( sprintf( __( "Go back to %s", 'sq' ), $category->cat_name ) ) . '">'.$category->cat_name.'</a>';
  94. array_push( $categories_names_array, $curr_cat_link );
  95. }
  96. }
  97. $chain .= implode( ", ", $categories_names_array ) . $separator;
  98. } else { // generate comma separated list of categories' names that the current single post has been assigned to
  99. $categories_names_array = array();
  100. foreach((get_the_category()) as $category) {
  101. if ( ( cat_is_ancestor_of( $sq_options['portfolio_cat_for_page_'.$portfolio_page_id], $category->term_id ) ||
  102. $sq_options['portfolio_cat_for_page_'.$portfolio_page_id] == $category->term_id ) ) { // belongs to a category associated with the current portfolio page
  103. array_push( $categories_names_array, $category->cat_name );
  104. }
  105. }
  106. $chain .= implode( ", ", $categories_names_array ) . $separator;
  107. }
  108. return $chain;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement