WordPress.org

Forums

Posts alphabetically in a list with clickable links (5 posts)

  1. Paweł Patyk
    Member
    Posted 4 years ago #

    Hi,
    I want to have a page that says:

    A
    Angel
    Angel 2

    B
    Book
    Brother

    C...

    etc.

    All those name are post type 'authors'.
    I like to have a starting capital letter before the list (see above).
    I found this code on the web wordpress.org/support/topic/show-category-with-sort-alphabetical-title by dlocc:

    <?php
    
    				$list = '';
    				$cats = get_terms('category');
    				$groups = array();
    				if( $cats && is_array( $cats ) ) {
    					foreach( $cats as $cat ) {
    						$first_letter = strtoupper( $cat->name[0] );
    						$groups[ $first_letter ][] = $cat;
    					}
    						if( !empty( $groups ) ) {{
    						$index_row .='<ul class="topindex">';
    					 foreach ($groups as $letter => $cats) {
    						$index_row .= '<li><h4><a href="#' . $letter . '" title="' . $letter . '">' . apply_filters( 'the_title', $letter ) . '</a></h4></li>';
    					}
    							$index_row .='</ul><br class="clear" />';} 
    
    							 $list .= '<ul class="index">';
    						 foreach( $groups as $letter => $cats ) {
    						$list .= '<li><a name="' . $letter . '"></a><h5><a href="#cats_top" title="back to top">' . apply_filters( 'the_title', $letter ) . '</a></h5>';
    							$list .= '<ul class="links">';
    							foreach( $cats as $cat ) {
    						   $url = attribute_escape( get_category_link( $cat->term_id ) );
    								$name = apply_filters( 'the_title', $cat->name );
    						   $list .= '<li><a title="' . $name . '" href="' . $url . '">' . $name . '</a></li>';
    
    				} 		 $list .= '</ul></li>';
    						} 	$list .= '</ul>';
    					}
    				}else $list .= '<p>Sorry, but no cats were found</p>';
    
    				?>
    			<a name="cats_top"></a>
    			<?php print $index_row; ?>
    			<?php print $list; ?>

    This works fine, but concerns a category, rather than post type...
    How do I edit the code to display the posts of the post type 'author' instead of category?

    Please help.

  2. Curtiss Grymala
    Member
    Posted 4 years ago #

    Use the get_posts() function instead of get_terms(). You will probably want to use it something like:
    $cats = get_posts( array( 'post_type' => 'author', 'orderby' => 'title', 'order' => ASC ) );

  3. Paweł Patyk
    Member
    Posted 4 years ago #

    Displays:
    Catchable fatal error: Object of class WP_Error could not be converted to string in /beta/wp-includes/formatting.php on line 433

    Maybe must you change this line?:
    $url = attribute_escape( get_category_link( $cat->term_id ) );

    and:

    $name = apply_filters( 'the_title', $cat->name );

  4. Curtiss Grymala
    Member
    Posted 4 years ago #

    Well, the loop will need to be changed to work with the properties that should be returned by the get_posts() function instead of the properties that were returned by the get_terms() function.

    Therefore, things like $cat->name[0] or $cat->name would become $cat->title[0] or $cat->title, etc. In addition, rather than trying to use get_category_link( $cat->term_id ), you should use get_permalink( $cat->ID ).

  5. Paweł Patyk
    Member
    Posted 4 years ago #

    Nothing is displayed...

    Code:

    <?php
    		$list = '';
    		$cats = get_posts( array( 'post_type' => 'autorzy', 'orderby' => 'title', 'order' => 'ASC' ) );
    		$groups = array();
    		if( $cats && is_array( $cats ) ) {
    			foreach( $cats as $cat ) {
    				$first_letter = strtoupper( $cat->title[0] );
    				$groups[ $first_letter ][] = $cat;
    			}
    			if( !empty( $groups ) ) {
    				{
    					$index_row .='<ul class="topindex">';
    					foreach ($groups as $letter => $cats) {
    						$index_row .= '<li><h4><a href="#' . $letter . '" title="' . $letter . '">' . apply_filters( 'the_title', $letter ) . '</a></h4></li>';
    					}
    					$index_row .='</ul><br class="clear" />';
    				} 
    
    				$list .= '<ul class="index">';
    				foreach( $groups as $letter => $cats ) {
    					$list .= '<li><a name="' . $letter . '"></a><h5>' . apply_filters( 'the_title', $letter ) . '</h5>';
    					$list .= '<ul class="links">';
    					foreach( $cats as $cat ) {
    						$url = attribute_escape( get_permalink( $cat->ID ) );
    						$name = apply_filters( 'the_title', $cat->title );
    						$list .= '<li><a title="' . $name . '" href="' . $url . '">' . $name . '</a></li>';
    					} $list .= '</ul></li>';
    				} $list .= '</ul>';
    			}
    		} else $list .= '<p>Brak autorów.</p>'; ?>
    
    		<a name="cats_top"></a>
    		<?php print $index_row; ?>
    		<?php print $list; ?>

    Post type:

    add_action('init', 'my_custom_init');
    
    function my_custom_init() {
    
    	$labels = array(
    
    		'name' => 'Autorzy',
    
    		'singular_name' => 'Autorzy',
    
    		'add_new' => 'Dodaj autora',
    
    		'add_new_item' => 'Dodaj nowego autora',
    
    		'edit_item' => 'Edytuj autora',
    
    		'new_item' => 'Nowy autor',
    
    		'view_item' => 'Zobacz autora',
    
    		'search_items' => 'Szukaj autora',
    
    		'not_found' => 'Brak autora',
    
    		'not_found_in_trash' => 'Brak autora w Koszu', 
    
    		'parent_item_colon' => ''
    
    	);
    
    	$args = array(
    
    		'labels' => $labels,
    
    		'public' => true,
    
    		'publicly_queryable' => true,
    
    		'show_ui' => true,
    
    		'query_var' => true,
    
    		'rewrite' => array('slug' => 'autor'),
    
    		'capability_type' => 'post',
    
    		'hierarchical' => false,
    
    		'menu_position' => 5,
    
    		'menu_icon' => 'http://wp.pabloart.pl/bwa/wp-content/themes/bwa/img/autorzy.png',
    
    		'taxonomies' => array('category'),
    
    		'supports' => array('title', 'editor')
    
    	);
    
    	register_post_type('autorzy', $args);
    
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.