• 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?

Viewing 1 replies (of 1 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    Try it with this in your theme’s functions.php: https://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:
    https://www.mysite/resultspageslug/a
    https://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:
    https://www.mysite/articles/a
    https://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:
    https://www.mysite/articles/letter-a
    https://www.mysite/articles/letter-b
    etc …

Viewing 1 replies (of 1 total)
  • The topic ‘A – B User search’ is closed to new replies.