Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Tests if any of a post's assigned categories are descendants of target categories
- *
- * @param int|array $cats The target categories. Integer ID or array of integer IDs
- * @param int|object $_post The post. Omit to test the current post in the Loop or main query
- * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
- * @see get_term_by() You can get a category by name or slug, then pass ID to this function
- * @uses get_term_children() Passes $cats
- * @uses in_category() Passes $_post (can be empty)
- * @version 2.7+
- * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
- */
- function test_post_is_in_descendant_category( $cats, $_post = null )
- {
- foreach ( (array) $cats as $cat ) {
- // get_term_children() accepts integer ID only
- $descendants = get_term_children( (int) $cat, 'category');
- if ( $descendants && in_category( $descendants, $_post ) )
- return true;
- }
- return false;
- }
- /**
- * Tests if any of a post's assigned categories are in the target categories or in any of the descendants
- *
- * @param int|array $cats The target categories. Integer ID or array of integer IDs
- * @param int|object $_post The post. Omit to test the current post in the Loop or main query
- * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
- * @see get_term_by() You can get a category by name or slug, then pass ID to this function
- * @uses get_term_children() Passes $cats
- * @uses in_category() Passes $_post (can be empty)
- * @version 2.7+
- */
- function post_is_in_category_or_descendants( $cats, $_post = null )
- {
- if( in_category( $cats, $_post = null ) || test_post_is_in_descendant_category( $cats, $_post = null ) ) {
- return true;
- }
- return false;
- }
- /**
- * This function is used to generate custom breadcrumbs for single posts view. Portfolio section or regular Blog is considered
- * when generating the link structure.
- */
- function get_category_parents_for_breadcrumbs( $id, $link = false, $separator = '/' ) {
- global $sq_options, $portfolio_pages_array;
- $portfolio_cats_array = explode( ',', $sq_options['portfolio_categories'] );
- if ( post_is_in_category_or_descendants($portfolio_cats_array) ) { // if the current post belongs to any Porfolio category
- foreach ( $portfolio_pages_array as $portfolio_page_obj ) {
- $port_page_ID = $portfolio_page_obj->ID;
- if ( post_is_in_category_or_descendants( $sq_options['portfolio_cat_for_page_'.$port_page_ID] ) ) {
- echo get_category_parents_for_portfolio_page( $id, $link, $separator, FALSE , $port_page_ID );
- break;
- }
- }
- } else { // if the current category is a regular blog category
- echo get_category_parents( $id, $link, $separator, FALSE );
- }
- }
- /**
- * This is the modified version of the "get_category_parents()" WP function
- * Retrieve category parents with separator for use in the Portfolio section to generate proper breadcrumb links.
- * The new parameter added is $portfolio_page_id which is the id of the page assigned with the Porfolio page template.
- * @since 1.2.0
- * @param int $id Category ID.
- * @param bool $link Optional, default is false. Whether to format with link.
- * @param string $separator Optional, default is '/'. How to separate categories.
- * @param bool $nicename Optional, default is false. Whether to use nice name for display.
- * @param string $portfolio_page_id Optional. Already linked to categories to prevent duplicates.
- * @param array $visited Optional. Already linked to categories to prevent duplicates.
- * @return string
- */
- function get_category_parents_for_portfolio_page( $id, $link = false, $separator = '/', $nicename = false, $portfolio_page_id='', $visited = array() ) {
- global $sq_options;
- $chain = '';
- $parent = &get_category( $id );
- if ( is_wp_error( $parent ) ) return $parent;
- $name = ( $nicename ) ? $parent->slug : $parent->cat_name;
- if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
- $visited[] = $parent->parent;
- $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 . ' ';
- }
- if ( $link ) { // generate comma separated list of categories' links that the current single post has been assigned to
- $query_string_prefix = ( get_option('permalink_structure') != '' ) ? '?' : '&';
- $categories_names_array = array();
- foreach((get_the_category()) as $category) {
- if ( ( cat_is_ancestor_of( $sq_options['portfolio_cat_for_page_'.$portfolio_page_id], $category->term_id ) ||
- $sq_options['portfolio_cat_for_page_'.$portfolio_page_id] == $category->term_id ) ) { // belongs to a category associated with the current portfolio page
- $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>';
- array_push( $categories_names_array, $curr_cat_link );
- }
- }
- $chain .= implode( ", ", $categories_names_array ) . $separator;
- } else { // generate comma separated list of categories' names that the current single post has been assigned to
- $categories_names_array = array();
- foreach((get_the_category()) as $category) {
- if ( ( cat_is_ancestor_of( $sq_options['portfolio_cat_for_page_'.$portfolio_page_id], $category->term_id ) ||
- $sq_options['portfolio_cat_for_page_'.$portfolio_page_id] == $category->term_id ) ) { // belongs to a category associated with the current portfolio page
- array_push( $categories_names_array, $category->cat_name );
- }
- }
- $chain .= implode( ", ", $categories_names_array ) . $separator;
- }
- return $chain;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement