/** * Filter AudioTheme archive requests. * * Set the number of posts per archive page. * * @since 1.4.2 * @todo Refactor to make it easier to retrieve settings and to define defaults in a single location. * @todo Implement a "rows" setting for calculating "posts_per_archive_page". * * @param object $query The main WP_Query object. Passed by reference. */ function audiotheme_archive_query($query) { if (is_admin() || !$query->is_main_query() || !(is_post_type_archive() || is_tax())) { return; } $post_type = get_audiotheme_current_archive_post_type(); if (empty($post_type)) { return; } // Determine if the 'posts_per_archive_page' setting is active for the current post type. $fields = apply_filters('audiotheme_archive_settings_fields', array(), $post_type); $columns = 1; if (!empty($fields['columns']) && $fields['columns']) { $default = empty($fields['columns']['default']) ? 4 : absint($fields['columns']['default']); $columns = get_audiotheme_archive_meta('columns', true, $default, $post_type); } if (!empty($fields['posts_per_archive_page']) && $fields['posts_per_archive_page']) { // Get the number of posts to display for this post type. $posts_per_archive_page = get_audiotheme_archive_meta('posts_per_archive_page', true, '', $post_type); if (!empty($posts_per_archive_page)) { $query->set('posts_per_archive_page', intval($posts_per_archive_page)); } } if (empty($posts_per_archive_page) && $columns > 1) { // Default to three even rows. $query->set('posts_per_archive_page', intval($columns * 3)); } }
/** * Add fields to the archive settings meta box. * * @since 1.4.2 * * @param WP_Post $post Archive post. */ function audiotheme_archive_settings_meta_box_fields($post, $post_type, $fields = array()) { if (empty($fields)) { return; } if (!empty($fields['posts_per_archive_page']) && $fields['posts_per_archive_page']) { $default = empty($fields['posts_per_archive_page']['default']) ? '' : absint($default); $value = get_audiotheme_archive_meta('posts_per_archive_page', true, $default, $post_type); ?> <p> <label for="audiotheme-posts-per-archive-page"><?php _e('Posts per page:', 'audiotheme'); ?> </label> <input type="text" name="posts_per_archive_page" id="audiotheme-posts-per-archive-page" value="<?php echo esc_attr($value); ?> " class="small-text"> </p> <?php } if (!empty($fields['columns']) && $fields['columns']) { $default = empty($fields['columns']['default']) ? 4 : absint($default); $value = get_audiotheme_archive_meta('columns', true, $default, $post_type); $choices = range(3, 5); if (!empty($fields['columns']['choices']) && is_array($fields['columns']['choices'])) { $choices = array_map('absint', $fields['columns']['choices']); } ?> <p> <label for="audiotheme-columns"><?php _e('Columns:', 'audiotheme'); ?> </label> <select name="columns" id="audiotheme-columns"> <?php foreach ($choices as $number) { printf('<option value="%1$d"%2$s>%1$d</option>', $number, selected($number, $value, false)); } ?> </select> </p> <?php } }
/** * Add classes to video posts on the archive page. * * Classes serve as helpful hooks to aid in styling across various browsers. * * - Adds nth-child classes to video posts. * * @since 1.2.0 * * @param array $classes Default post classes. * @return array */ function audiotheme_video_archive_post_class($classes) { global $wp_query; if ($wp_query->is_main_query() && is_post_type_archive('audiotheme_video')) { $nth_child_classes = audiotheme_nth_child_classes(array('current' => $wp_query->current_post + 1, 'max' => get_audiotheme_archive_meta('columns', true, 4))); $classes = array_merge($classes, $nth_child_classes); } return $classes; }
/** * Add an orderby setting to the record archive. * * Allows for changing the sort order of records. Custom would require a plugin * like Simple Page Ordering. * * @since 1.3.0 * * @param WP_Post $post Post object. */ function audiotheme_record_archive_settings($post) { $post_type = is_audiotheme_post_type_archive_id($post->ID); if ('audiotheme_record' !== $post_type) { return; } $options = array('release_year' => __('Release Year', 'audiotheme'), 'title' => __('Title', 'audiotheme'), 'custom' => __('Custom', 'audiotheme')); $orderby = get_audiotheme_archive_meta('orderby', true, 'release_year', 'audiotheme_record'); ?> <p> <label for="audiotheme-orderby"><?php _e('Order by:', 'audiotheme'); ?> </label> <select name="audiotheme_orderby" id="audiotheme-orderby"> <?php foreach ($options as $id => $value) { printf('<option value="%s"%s>%s</option>', esc_attr($id), selected($id, $orderby, false), esc_html($value)); } ?> </select> </p> <?php }
/** * Load the featured video template part. * * @since 1.0.0 */ function huesos_audiotheme_template_featured_video() { $post_type = is_audiotheme_post_type_archive_id(get_post()->ID); if ('audiotheme_video' === $post_type || !($featured_video = get_audiotheme_archive_meta('huesos_featured_video', true, '', $post_type))) { return; } include locate_template('audiotheme/parts/featured-video.php'); }
/** * Retrieve an array of record posts. * * @since 1.0.0 * * @param array $custom_args Custom args to pass to get_posts(). * @return array */ protected function get_records($custom_args = array()) { $args = array('post_type' => 'audiotheme_record', 'post_status' => 'publish', 'meta_key' => '_audiotheme_release_year', 'orderby' => 'meta_value_num', 'order' => 'desc'); // Respect the archive order setting. $orderby = get_audiotheme_archive_meta('orderby', true, 'release_year', 'audiotheme_record'); switch ($orderby) { case 'custom': $args['orderby'] = 'menu_order'; $args['order'] = 'asc'; unset($args['meta_key']); break; case 'title': $args['orderby'] = 'title'; $args['order'] = 'asc'; unset($args['meta_key']); break; } $args = wp_parse_args($custom_args, $args); return get_posts($args); }