Forums

[resolved] Posts listing on starting letter (alphabetical) (14 posts)

  1. tjalling474
    Member
    Posted 1 year ago #

    What I wanted to make was a page in which you have a list with all your posts, filtered on letter, with on the top shortcuts to the letters.
    So like this:
    A - b - c - d -... til Z
    A:
    [all posts which start with an a in a list]

    B:
    [all posts which start with an b in a list]
    etc.

    When you press the A on the top: A - b - c. You jump to the part of the page with all posts that start with an A. The same for b.. etc.
    I used some html for the shortcuts:

    <a href="#a">A</a> - <a href="#b">B</a> - etc.. 
    
    <h2><a name="a">A</a></h2>
    <h2><a name="b">B</a></h2>
    etc.

    Now I only need to display the posts that start with that letter in a list behind that letter.
    So I was thinking, maybe you could make a shortcode for each of the posts that start with a letter. So a shortcode for all posts that start with an a. one for all posts that start with a B. etc.

    Has anyone done something similar, or does someone know how to solve this problem?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Here is a link to code for a template to list posts by the first letter of the title. It does not put in the 'anchors' for the jump links, but that should be easy to add.

  3. tjalling474
    Member
    Posted 1 year ago #

    Thanks for the reply, looks like I can use that :D
    But were should I put that code..?

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    You put this code in your theme's page.php or make custom page template for it.
    If you want to show this on the front page you put it in your theme's index.php
    Change the code so it follows your theme's structure.

  5. tjalling474
    Member
    Posted 1 year ago #

    Hey, I made a custom page template, put the code in it and edited so it follows my theme's structure. If also put the functions in my functions.php. But when I want to view a page which uses this template I get the following error:
    Parse error: syntax error, unexpected T_VARIABLE, expecting ')' in /home/animesum/public_html/wp-content/themes/arras/A-Z Pagestest.php on line 39

    On line 39 there is this: $in_this_row = 0;
    There is a function that Ive put in my functions.php:

    function start_new_row() {
    global $in_this_row;
    $in_this_row = 0;
    echo "\t<div class='row-cells'>\n";
    }

    So I don't understand why I got the error. 0_o

  6. ucfknight10
    Member
    Posted 1 year ago #

    the error occurs on "A-Z Pagestest.php", not functions.php

  7. tjalling474
    Member
    Posted 1 year ago #

    Okay, well I it is finally up and running.
    The only thing left is to add the anchors.
    So these: <a name="A">A</a>
    How should I insert this in the code:

    <?php
             $posts = (get_query_var('posts')) ? get_query_var('posts') : 1;
             $args = array (
                'posts_per_page' => $posts_per_page,
                'post_type' => 'post',
                'orderby' => 'title',
                'order' => 'ASC',
                'post' => $post
             );
             query_posts($args);
             if ( have_posts() ) {
                $in_this_row = 0;
                while ( have_posts() ) {
                   the_post();
                   $first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
                   if ($first_letter != $curr_letter) {
                      if (++$post_count > 1) {
                         end_prev_letter();
                      }
                      start_new_letter($first_letter);
                      $curr_letter = $first_letter;
                   }
                   if (++$in_this_row > $posts_per_row) {
                      end_prev_row();
                      start_new_row();
                      ++$in_this_row;  // Account for this first post
                   } ?>
                   <div class="title-cell"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></div>
                <?php }
                end_prev_letter();
                ?>

    I really appreciate the help. :D

  8. ucfknight10
    Member
    Posted 1 year ago #

    after

    $curr_letter = $first_letter;

    do a new line and type

    echo '<a name="' . $curr_letter . '">' . ucfirst($curr_letter) . '</a>';

  9. tjalling474
    Member
    Posted 1 year ago #

    Thanks! It's working now :D

  10. vtxyzzy
    Member
    Posted 1 year ago #

    @ucfknight10, I think that is not correct.

    The change needs to be made in the function start_new_letter, because that function puts things in the proper divs:

    function start_new_letter($letter) {
       echo "<div class='letter-group'>\n";
       echo "\t<div class='letter-cell'><a name='$letter'>$letter</a></div>\n";
       start_new_row($letter);
  11. ucfknight10
    Member
    Posted 1 year ago #

    well, it's not incorrect. putting it in either place should work fine. just a matter of preferred organizational style.

  12. vtxyzzy
    Member
    Posted 1 year ago #

    OK - but the function also displays the letter. So, if you put it where you said, you should remove it from the function.

  13. tjalling474
    Member
    Posted 1 year ago #

    yeah, you're right ;)
    the letters were being displayed twice,
    that's why edited de code to:
    echo '<a name="' . $curr_letter . '"></a>';
    that way it works fine.

    thanks for all the help! :D

  14. ucfknight10
    Member
    Posted 1 year ago #

    welcome *mark as resolved*

Topic Closed

This topic has been closed to new replies.

About this Topic