If you want to show a cover image for each Docs category, here’s one way to do it with the code below. This method will grab the first image from the first Docs post in a top-level category.
Note: the first post does not necessarily mean the first post that you created. It refers to the first post according to your sort order, depending on whether you use a custom sort order, whether by number, or alphabetical, or whatever. The first post is the one that appears first in the list of the Table of Contents, and on the “Top level item page.” The “Top level item page” is the page that lists all of the docs for one main category of docs.
So, it will grab the first image from that first post. That image can be displayed in any of these 5 locations:
- On the “Table of Contents” page (also known as the “Top level item page” or the “Table of Contents page”), above the menu bar. The image is shown here by default with the code below. If you don’t want to show the image here, comment out line 17 in the code.
- On the “Table of Contents” page (also known as the “Top level item page” or the “Table of Contents page”), below the menu bar. To show the image here, uncomment line 20 in the code below.
- On each single Doc, above the menu bar. To show the image here, uncomment line 23 in the code below.
- On each single Docs, above the Table of Contents Sidebar Widget. The image is shown here by default with the code below. If you don’t want to show the image here, comment out line 29 in the code.
You don’t have to choose only 1 of the 5 locations above. You can choose to show it in any or all of those locations.
If you haven’t added an image to that first post, then this function will not show any image.
The image will be shown with a maximum width of 150 pixels. To change the width, you can change “150” to your desired width on line 123.
If you prefer to show the image with its original width and height, comment out line 123.
/** * Add a cover image to Docs categories. * * This adds a cover image to any of five places: * * To the Docs Table of Contents page, either above the menu bar * or under it. * To single Docs, either above the menu bar, under the menu bar, * or above the sidebar Table of Contents widget. */ add_action( 'init', 'organized_docs_add_cover_image', 999 ); function organized_docs_add_cover_image() { // Show thumbnail on Table of Contents page, above the menu bar add_action( 'organized_docs_tax_top', 'my_organized_docs_cover_image' ); // Show thumbnail on Table of Contents page, below the menu bar // add_action('organized_docs_content_after_nav', 'my_organized_docs_cover_image'); // Show thumbnail on single Docs, above the menu bar // add_action( 'organized_docs_single_top', 'my_organized_docs_cover_image' ); // Show thumbnail above the Table of Contents Sidebar Widget add_action('organized_docs_before_widget', 'my_organized_docs_cover_image'); } /** * Pull the first image from the first post in a Docs category * and display that image on the "Table of Contents" page for that * Category (Top level Docs item) */ function my_organized_docs_cover_image() { if ( is_tax( 'isa_docs_category' ) ) { global $Isa_Organized_Docs; // Get current term id on docs category taxonomy page $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $curr_termID = $term->term_id; // Check if This is a Top Level Docs page (a Table of Contents page), rather than just a sub-category page // get term children $termchildren = get_term_children( $curr_termID, 'isa_docs_category' ); } elseif ( is_singular( 'isa_docs' ) ) { global $Isa_Organized_Docs; $doc_categories = wp_get_object_terms( get_the_id(), 'isa_docs_category' ); if ( ! $doc_categories ) { // cat has not been assigned yet return; } $first_term = $doc_categories[0]; $curr_term_id = $first_term->term_id; $top_level_parent_term_id = $Isa_Organized_Docs->isa_term_top_parent_id( $curr_term_id ); $termchildren = get_term_children( $top_level_parent_term_id, 'isa_docs_category' ); } if ( ! empty($termchildren) ) { // Yes, this is the Table of Contents page... // sort $termchildren terms into custom sort_order $single_sort_by = get_option('od_single_sort_by'); $orderby_order = get_option('od_single_sort_order'); if ( 'date' == $single_sort_by ) { $orderby = 'date'; } elseif ( 'title - alphabetical' == $single_sort_by ) { $orderby = 'title'; } else { $orderby = 'meta_value_num'; } $sorted_termchildren = $Isa_Organized_Docs->sort_terms_custom( $termchildren, 'subheading_sort_order' ); // get the id of only the 1st term reset($sorted_termchildren); $first_term_id = key($sorted_termchildren); $args = array( 'post_type' => 'isa_docs', 'posts_per_page' => 1, 'tax_query' => array( array( 'taxonomy' => 'isa_docs_category', 'field' => 'id', 'terms' => $first_term_id ) ), 'orderby' => $orderby, 'meta_key' => '_odocs_meta_sortorder_key', 'order' => $orderby_order ); $postlist = get_posts( $args ); $content = $postlist[0]->post_content; // extract first image from post content $imgBeg = strpos($content, '<img', 0); if ( $imgBeg ) { $post = substr($content, $imgBeg); $imgEnd = strpos($post, '>'); $doc_image = substr($post, 0, $imgEnd+1); // Optionally resize the image in case it is too large. // You can change "150" below to your desired width. // If you prefer the image with its original width, comment out the next line. $doc_image = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', 'width="150"',$doc_image); echo $doc_image; } } }
Questions and Comments are Welcome