WordPress.org

Forums

Twenty Twelve
Glossary index for custom post type with alphabet navigation (2 posts)

  1. zephyri
    Member
    Posted 1 year ago #

    I am using WordPress version 3.7.1 and I am using a child theme for twenty twelve.

    I have a glossary type page that lists posts alphabetically by title. There is a list of the letters of the alphabet at the top of the page, and each letter links to a page that displays all the posts with titles that start with that letter.

    Here is a link to the live page: http://www.centerforadventistresearch.org/manuscripts/

    I want to change my code so that only the posts from my custom post type are display, and also so that they are ordered alphabetically by the post name aka slug.

    My custom post type is: manuscript.

    This is the code I have in my functions.php

    <?php
    /** add custom taxonomy car-manuscript-letter */
    
        $labels = array(
            'name' 							=> 'Letters',
            'singular_name' 				=> 'Letter',
            'search_items' 					=> 'Search Letters',
            'popular_items' 				=> 'Popular Letters',
            'all_items' 					=> 'All Letters',
            'parent_item' 					=> 'Parent Letter',
            'parent_item_colon' 			=> 'Parent Letter:',
            'edit_item' 					=> 'Edit Letter',
            'update_item' 					=> 'Update Letter',
            'add_new_item' 					=> 'Add New Letter',
            'new_item_name' 				=> 'New Letter',
            'separate_items_with_commas' 	=> 'Separate letters with commas',
            'add_or_remove_items' 			=> 'Add or remove Letters',
            'choose_from_most_used' 		=> 'Choose from most used Letters',
            'menu_name' 					=> 'Letters',
        );
    
        $args = array(
    		'label'							=> 'Letters',
    		'labels' 						=> $labels,
            'public' 						=> true,
            'hierarchical' 					=> true,
            'show_ui' 						=> true,
            'show_in_nav_menus' 			=> true,
    		'args'							=> array( 'orderby' => 'term_order' ),
            'rewrite' 						=> array( 'slug' => 'manuscript/letters', 'with_front' => false ),
            'show_tagcloud' 				=> true,
            'show_admin_column' 			=> false,
            'query_var' 					=> true
        );
    
        register_taxonomy( 'carmanuletter', array('manuscript'), $args );
    
    /** add custom post type manuscripts */
    
    register_post_type( 'manuscript',
    		array(
    			'labels'                => array(
                'name'              	=> __( 'Manuscripts' ),
                'singular_name'     	=> __( 'Manuscript' )
                ),
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'supports'              => array( 'title', 'editor', 'thumbnail' ),
            'rewrite'               => array( 'slug' => 'manuscripts', 'with_front' => false ),
            'has_archive'           => true
        )
    );
    
    /** Add 140x140 image size */
     add_image_size('excerpt-thumbnail', 140, 140, true);
    
    /** Remove the ... from excerpt and change the text */
    function change_excerpt_more()
    {
      function new_excerpt_more($more)
        {
    
    /** Use .read-more to style the link */
          return '<span class="continue-reading"> <a href="' . get_permalink() . '">Continue Reading &raquo;</a></span>';
        }
      add_filter('excerpt_more', 'new_excerpt_more');
    }
    add_action('after_setup_theme', 'change_excerpt_more');
    
    /*
     * Function Create Array of Letters that have post titles (for archive) by kia
     */
    
    // Add new taxonomy, NOT hierarchical (like tags)
    function kia_create_glossary_taxonomy(){
        if(!taxonomy_exists('glossary')){
            register_taxonomy('glossary',array('post'),array('show_ui' => false));
         }
    }
    add_action('init','kia_create_glossary_taxonomy');
    
    /* When the post is saved, saves our custom data */
    function kia_save_first_letter( $post_id ) {
    // verify if this is an auto save routine.
    // If it is our form has not been submitted, so we dont want to do anything
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
    return;
    
    //check location (only run for posts)
    $limitPostTypes = array('post');
    if (!in_array($_POST['post_type'], $limitPostTypes)) return;
    
    // Check permissions
    if ( !current_user_can( 'edit_post', $post_id ) )
    return;
    
    // OK, we're authenticated: we need to find and save the data
    $taxonomy = 'glossary';
    
    //set term as first letter of post name aka slug, lower case
    wp_set_post_terms( $post_id, strtolower(substr($_POST['post_title'], 0, 1)), $taxonomy );
    
    //delete the transient that is storing the alphabet letters
    delete_transient( 'kia_archive_alphabet');
    }
    add_action( 'save_post', 'kia_save_first_letter' );
    
    //create array from existing posts
    function kia_run_once(){
    
        if ( false === get_transient( 'kia_run_once' ) ) {
    
            $taxonomy = 'glossary';
            $alphabet = array();
    
            $posts = get_posts(array('numberposts' => -1 ) );
    
            foreach( $posts as $p ) :
                //set term as first letter of post title, lower case
                wp_set_post_terms( $p->ID, strtolower(substr($p->post_title, 0, 1)), $taxonomy );
            endforeach;
    
            set_transient( 'kia_run_once', 'true' );
    
        }
    
    }
    add_action('init','kia_run_once');

    This is the code I have in my archive-manuscript.php

    <?php get_header(); ?>
    
    <div id="container">
        <div id="content" role="main">
            <h1 class="archive-title">Manuscripts</h1>
    		<p>The Center holds over 300 collections of personal papers of individuals prominent in the Seventh-day Adventist Church and its work. These collections often include correspondence, topical files, lecture and sermon notes, research material, publications, photographs, reports, and many other types of material. All of the collections available for research use are shown below. All have a printed register, which is an information and user guide to the collections.</p>
    		<?php get_template_part( 'content', 'manuscript' ); ?>
        </div><!-- #content -->
    </div><!-- #container -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    This is the code I have on my content-manuscript.php

    <?php get_header(); ?>
    
    <div id="container">
        <div id="content" role="main">
            <!-- kia alphabetical menu for list of custom post type manuscript -->
    	<?php $taxonomy = 'glossary';
    
    // save the terms that have posts in an array as a transient
    if ( false === ( $alphabet = get_transient( 'kia_archive_alphabet' ) ) ) {
        // It wasn't there, so regenerate the data and save the transient
        $terms = get_terms($taxonomy);
    
        $alphabet = array();
        if($terms){
            foreach ($terms as $term){
                $alphabet[] = $term->slug;
            }
        }
         set_transient( 'car_archive_alphabet', $alphabet );
    }
    
    ?>
    
    	<div id="archive-menu" class="menu"> <!-- alphabetical menu for list of collections -->
        <ul id="alphabet-menu">
    		<?php     foreach(range('a', 'z') as $i) :
    					$current = ($i == get_query_var($taxonomy)) ? "current-menu-item" : "menu-item";
    					if (in_array( $i, $alphabet )){
    					printf( '<li class="az-char %s"><a href="%s">%s</a></li>',
    					$current, get_term_link( $i, $taxonomy ), strtoupper($i) );
    					} else {
    					printf( '<li class="az-char %s">%s</li>', $current, strtoupper($i) );
    				}     endforeach;
    		?>
        </ul>
    	</div>
    	<!-- end of div for kia alphabetical menu for list of custom post type manuscript -->
    		<h1>try try try trying to add the alphabet menu</h1>
    
    		<?php
                            $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                            $args= array(
                                    'post_type' => 'manuscript',
    								'orderby' 	=>	'name',
    								'order'		=>	'ASC',
    								'posts_per_page'	=>	'10',
                                    'paged'		=>	$paged
    );
                            query_posts($args);
                            if( have_posts() ) :?>
    
                            <?php while ( have_posts() ) : the_post(); ?>
                                    <?php get_template_part( 'content', get_post_format() ); ?>
                                    <?php comments_template( '', true ); ?>
                            <?php endwhile; // end of the loop. ?>
    
                            <?php twentytwelve_content_nav( 'nav-below' ); ?>
    
                            <?php else : ?>
                            <article id="post-0" class="post no-results not-found">
                                    <header class="entry-header">
                                            <h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
                                    </header>
                                    <div class="entry-content">
                                            <p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
                                            <?php get_search_form(); ?>
                                    </div><!-- .entry-content -->
                            </article><!-- #post-0 -->
    
                            <?php endif; wp_reset_query(); ?>
    
        </div><!-- #content -->
    </div><!-- #container -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    The code for the glossary I am adapting from this website: http://www.kathyisawesome.com/424/alphabetical-posts-glossary/

    However, when I change the areas I think would affect the post type, the alphabet links to the list of posts give back an error that “Nothing Found. Apologies, but no results were found. Perhaps searching will help find a related post.”

    What do I need to change so only the custom post type posts are included in the glossary instead of all posts? And also, what do I need to change so that they are ordered alphabetically by slug?

  2. helgatheviking
    Member
    Posted 1 year ago #

    Verify that you've updated my code to save only on your custom post type. It is possible that it isn't firing for manuscripts and so you don't have any posts found for a particular letter.

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic