max_num_pages; if ( is_object( $query ) && null != $query ) { $total_pages = $query->max_num_pages; } if ( $total_pages > 1 ) { $url_base = $wp_rewrite->pagination_base; $big = 999999999; // Sets the paginate_links arguments. $arguments = apply_filters( 'odin_pagination_args', array( 'base' => esc_url_raw( str_replace( $big, '%#%', get_pagenum_link( $big, false ) ) ), 'format' => '', 'current' => max( 1, get_query_var( 'paged' ) ), 'total' => $total_pages, 'show_all' => $show, 'end_size' => $end, 'mid_size' => $mid, 'type' => 'list', 'prev_text' => __( '« Previous', 'avalon-b' ), 'next_text' => __( 'Next »', 'avalon-b' ), ) ); $pagination = '
' . paginate_links( $arguments ) . '
'; // Prevents duplicate bars in the middle of the url. if ( $url_base ) { $pagination = str_replace( '//' . $url_base . '/', '/' . $url_base . '/', $pagination ); } return $pagination; } } } /** * Related Posts. * * Usage: * To show related by categories: * Add in single.php * To show related by tags: * Add in single.php * * @since 2.2.0 * * @global array $post WP global post. * * @param string $display Set category or tag. * @param int $qty Number of posts to be displayed (default 5). * @param string $title Set the widget title. * @param bool $thumb Enable or disable displaying images. * @param string $post_type Post type. * * @return string Related Posts. */ function odin_related_posts( $display = 'category', $qty = 4, $title = '', $thumb = true, $post_type = 'post' ) { global $post; $show = false; $post_qty = (int) $qty; ! empty( $title ) || $title = __( 'Related Posts', 'avalon-b' ); // Creates arguments for WP_Query. switch ( $display ) { case 'tag': $tags = wp_get_post_tags( $post->ID ); if ( $tags ) { // Enables the display. $show = true; $tag_ids = array(); foreach ( $tags as $individual_tag ) { $tag_ids[] = $individual_tag->term_id; } $args = array( 'tag__in' => $tag_ids, 'post__not_in' => array( $post->ID ), 'posts_per_page' => $post_qty, 'post_type' => $post_type, 'ignore_sticky_posts' => 1 ); } break; default : $categories = get_the_category( $post->ID ); if ( $categories ) { // Enables the display. $show = true; $category_ids = array(); foreach ( $categories as $individual_category ) { $category_ids[] = $individual_category->term_id; } $args = array( 'category__in' => $category_ids, 'post__not_in' => array( $post->ID ), 'showposts' => $post_qty, 'post_type' => $post_type, 'ignore_sticky_posts' => 1, ); } break; } if ( $show ) { $related = new WP_Query( $args ); if ( $related->have_posts() ) { $layout = '
'; $layout .= '

' . esc_attr( $title ) . '

'; $layout .= ( $thumb ) ? '
' : '
' : ''; $layout .= '
'; echo $layout; } wp_reset_postdata(); } } /** * Custom excerpt for content or title. * * Usage: * Place: * * @since 2.2.0 * * @param string $type Sets excerpt or title. * @param int $limit Sets the length of excerpt. * * @return string Return the excerpt. */ function odin_excerpt( $type = 'excerpt', $limit = 40 ) { $limit = (int) $limit; // Set excerpt type. switch ( $type ) { case 'title': $excerpt = get_the_title(); break; default : $excerpt = get_the_excerpt(); break; } return wp_trim_words( $excerpt, $limit ); } /** * Breadcrumbs. * * @since 2.2.0 * * @param string $homepage Homepage name. * * @return string HTML of breadcrumbs. */ function odin_breadcrumbs( $homepage = '' ) { global $wp_query, $post, $author; ! empty( $homepage ) || $homepage = __( 'Home', 'avalon-b' ); // Default html. $current_before = '
  • '; $current_after = '
  • '; if ( ! is_home() && ! is_front_page() || is_paged() ) { // First level. echo ''; } } /** * Get a image URL. * * @param int $id Image ID. * @param int $width Image width. * @param int $height Image height. * @param boolean $crop Image crop. * @param boolean $upscale Force the resize. * * @return string */ function odin_get_image_url( $id, $width, $height, $crop = true, $upscale = false ) { $resizer = Odin_Thumbnail_Resizer::get_instance(); $origin_url = wp_get_attachment_url( $id ); $url = $resizer->process( $origin_url, $width, $height, $crop, $upscale ); if ( $url ) { return $url; } else { return $origin_url; } } /** * Custom post thumbnail. * * @since 2.2.0 * * @param int $width Width of the image. * @param int $height Height of the image. * @param string $class Class attribute of the image. * @param string $alt Alt attribute of the image. * @param boolean $crop Image crop. * @param string $class Custom HTML classes. * @param boolean $upscale Force the resize. * * @return string Return the post thumbnail. */ function odin_thumbnail( $width, $height, $alt, $crop = true, $class = '', $upscale = false ) { if ( ! class_exists( 'Odin_Thumbnail_Resizer' ) ) { return; } $thumb = get_post_thumbnail_id(); if ( $thumb ) { $image = odin_get_image_url( $thumb, $width, $height, $crop, $upscale ); $html = '' . esc_attr( $alt ) . ''; return apply_filters( 'odin_thumbnail_html', $html ); } } /** * Automatically sets the post thumbnail. * * Use: * add_action( 'the_post', 'odin_autoset_featured' ); * add_action( 'save_post', 'odin_autoset_featured' ); * add_action( 'draft_to_publish', 'odin_autoset_featured' ); * add_action( 'new_to_publish', 'odin_autoset_featured' ); * add_action( 'pending_to_publish', 'odin_autoset_featured' ); * add_action( 'future_to_publish', 'odin_autoset_featured' ); * * @since 2.2.0 * * @global array $post WP post object. */ function odin_autoset_featured() { global $post; if ( isset( $post->ID ) ) { $already_has_thumb = has_post_thumbnail( $post->ID ); if ( ! $already_has_thumb ) { $attached_image = get_children( 'post_parent=' . $post->ID . '&post_type=attachment&post_mime_type=image&numberposts=1' ); if ( $attached_image ) { foreach ( $attached_image as $attachment_id => $attachment ) { set_post_thumbnail( $post->ID, $attachment_id ); } } } } } /** * Debug variables. * * @since 2.2.0 * * @param mixed $variable Object or Array for debug. * * @return string Human-readable information. */ function odin_debug( $variable ) { echo '
    ' . print_r( $variable, true ) . '
    '; } /** * Get term meta fields * * Usage: * * * @since 2.2.7 * * @param int $term_id Term ID * @param string $field Field slug * * @return string Field value */ function odin_get_term_meta( $term_id, $field ) { $option = sprintf( 'odin_term_meta_%s_%s', $term_id, $field ); return get_option( $option ); }