WordPress.org

Ready to get started?Download WordPress

Forums

A - B User search (2 posts)

  1. bremmerm
    Member
    Posted 1 year ago #

    Hi, I am looking to place a column on my home page that shows all the letters in the alphabet (A B C D...X Y Z) on my home and search pages. So all someone has to do is click "B" and they are taken to a page where it show's all posts that start with "B".

    I tried a couple different plugins but they did not work...any ideas on how to make this work?

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this in your theme's functions.php: http://pastebin.com/bhb9pf5S

    This allows you to have a query on a Page template for a single letter. Publish a Page where you want the letter results to be displayed and remember this Page slug. Query the loop like this on the Page template that is used for the published Page:

    $letter_post_ids = get_letter_ids();
    
    	if($letter_post_ids) {
    
    		$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    		$args = array(
    		  //'posts_per_page'      => 2,
    		  'post__in'            => $letter_post_ids,
    		  'ignore_sticky_posts' => 1,
    		  'post_type'           => 'post',
    		  'orderby'             => 'title',
    		  'order'               => 'asc',
    		  'paged'               => $paged
    		);
    		query_posts($args);
    	}
    
    // start of loop
    while ( have_posts() ) : the_post();
    ?>
    <!-- rest of loop -->

    Put this in the template files where you want the letters (links) to show up (index.php, search,php):

    <?php echo get_alphabet_links(); ?>

    This will result in an unordered list of letter links that you can style with CSS.

    The links will be going to urls like this but these pages will all use the same template (you published) for the results:
    http://www.mysite/resultspageslug/a
    http://www.mysite/resultspageslug/b
    etc...

    The only thing you have to change in the functions.php code for this to work is this:

    define('RESULTS_PAGE_SLUG', 'articles');
    define('LETTER_PREFIX', '');

    Put your published results Page slug in the RESULTS_PAGE_SLUG define. I published a Page with the 'articles' slug on my site, so I used 'articles in the difine'.
    This gave me letter links with urls like this:
    http://www.mysite/articles/a
    http://www.mysite/articles/b
    etc...

    If you would like to prefix the letter you can change the LETTER_PREFIX define.
    example:

    define('RESULTS_PAGE_SLUG', 'articles');
    define('LETTER_PREFIX', 'letter-');

    This will result in letter links to urls like this:
    http://www.mysite/articles/letter-a
    http://www.mysite/articles/letter-b
    etc ...

Topic Closed

This topic has been closed to new replies.

About this Topic