/** * Filter the sidebar status on AudioTheme archives and singular posts. * * @since 1.0.0 * * @param bool $is_active_sidebar Whether the sidebar is active. * @param string $index The sidebar id. * @return bool */ function huesos_audiotheme_sidebar_status($is_active_sidebar, $index) { if ('sidebar-1' !== $index || !$is_active_sidebar) { return $is_active_sidebar; } $post_type = get_post_type(); if (is_audiotheme_post_type_archive()) { $archive_post_type = get_audiotheme_post_type_archive(); } elseif (is_singular() && 0 === strpos($post_type, 'audiotheme_')) { $archive_post_type = 'audiotheme_track' === $post_type ? 'audiotheme_record' : $post_type; } elseif (is_tax('audiotheme_record_type')) { $archive_post_type = 'audiotheme_record'; } elseif (is_tax('audiotheme_video_category')) { $archive_post_type = 'audiotheme_video'; } if (!empty($archive_post_type)) { $is_active_sidebar = 'enabled' === get_audiotheme_archive_meta('huesos_sidebar', true, 'enabled', $archive_post_type); } return $is_active_sidebar; }
/** * Create an archive post for a post type if one doesn't exist. * * The post type's plural label is used for the post title and the defined * rewrite slug is used for the postname. * * @since 1.0.0 * * @param string $post_type_name Post type slug. * @return int Post ID. */ function audiotheme_archives_create_archive($post_type) { $archive_id = get_audiotheme_post_type_archive($post_type); if ($archive_id) { return $archive_id; } // Search the inactive option before creating a new page. $inactive = get_option('audiotheme_archives_inactive'); if ($inactive && isset($inactive[$post_type]) && get_post($inactive[$post_type])) { return $inactive[$post_type]; } // Otherwise, create a new archive post. $post_type_object = get_post_type_object($post_type); $post = array('post_title' => $post_type_object->labels->name, 'post_name' => get_audiotheme_post_type_archive_slug($post_type), 'post_type' => 'audiotheme_archive', 'post_status' => 'publish'); return wp_insert_post($post); }
/** * Add helpful nav menu item classes. * * Adds class hooks to various nav menu items since child pseudo selectors * aren't supported in all browsers. * * @since 1.0.0 * * @param array $items List of menu items. * @param array $args Menu display args. * @return array */ function audiotheme_nav_menu_classes($items, $args) { global $wp; $classes = array(); $first_top = -1; $current_url = trailingslashit(home_url(add_query_arg(array(), $wp->request))); $blog_page_id = get_option('page_for_posts'); $is_blog_post = is_singular('post'); $is_audiotheme_post_type = is_singular(array('audiotheme_gig', 'audiotheme_record', 'audiotheme_track', 'audiotheme_video')); $post_type_archive_id = get_audiotheme_post_type_archive(get_post_type()); $post_type_archive_link = get_post_type_archive_link(get_post_type()); foreach ($items as $key => $item) { if (empty($item->menu_item_parent)) { $first_top = -1 === $first_top ? $key : $first_top; $last_top = $key; } else { if (!isset($classes['first-child-items'][$item->menu_item_parent])) { $classes['first-child-items'][$item->menu_item_parent] = $key; $items[$key]->classes[] = 'first-child-item'; } $classes['last-child-items'][$item->menu_item_parent] = $key; } if (!is_404() && !is_search()) { if ('audiotheme_archive' === $item->object && $post_type_archive_id === $item->object_id && trailingslashit($item->url) === $current_url) { $items[$key]->classes[] = 'current-menu-item'; } if ($is_blog_post && $blog_page_id === $item->object_id) { $items[$key]->classes[] = 'current-menu-parent'; } // Add 'current-menu-parent' class to CPT archive links when viewing a singular template. if ($is_audiotheme_post_type && $post_type_archive_link === $item->url) { $items[$key]->classes[] = 'current-menu-parent'; } } } $items[$first_top]->classes[] = 'first-item'; $items[$last_top]->classes[] = 'last-item'; if (isset($classes['last-child-items'])) { foreach ($classes['last-child-items'] as $item_id) { $items[$item_id]->classes[] = 'last-child-item'; } } return $items; }
/** * Display a post type archive description. * * @since 1.0.0 * * @param string $before Content to display before the description. * @param string $after Content to display after the description. */ function the_audiotheme_archive_description($before = '', $after = '') { if (is_post_type_archive()) { $post_type_object = get_queried_object(); if ($archive_id = get_audiotheme_post_type_archive($post_type_object->name)) { $archive = get_post($archive_id); if (!empty($archive->post_content)) { echo $before . apply_filters('the_content', $archive->post_content) . $after; } } } if (is_tax() && !empty(get_queried_object()->description)) { echo $before . apply_filters('the_content', term_description()) . $after; } }
/** * Provide an edit link for archives in the admin bar. * * @since 1.2.1 * * @param WP_Admin_Bar $wp_admin_bar Admin bar object instance. */ function audiotheme_archives_admin_bar_edit_menu($wp_admin_bar) { if (!is_admin() && is_audiotheme_post_type_archive()) { $id = get_audiotheme_post_type_archive(); $post_type_object = get_post_type_object(get_post_type($id)); if (empty($post_type_object)) { return; } $wp_admin_bar->add_menu(array('id' => 'edit', 'title' => $post_type_object->labels->edit_item, 'href' => get_edit_post_link($id))); } }