• Resolved formica

    (@formica)


    Hi There

    I’m attempting to list posts alphabetically from a cat page, which I’ve got working using MichaelH’s great piece of code from here:

    https://www.ads-software.com/support/topic/first-letter-posts

    And using this code above `
    $alphabet = range(‘A’, ‘Z’);
    foreach ($alphabet as $letter) {
    echo ‘<a href=”?letter=’ . $letter . ‘”>’ . $letter . ‘</a>’;
    }
    $_GET[“letter”];`

    to output an a-z list which links to the same page telling Michaels code which letter to display.

    My question is instead of outputting a full alphabetical list is it possible to list it as such ‘A – I’ ‘J – Q’ ‘R – Z’ ?

    Any pointers appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter formica

    (@formica)

    Hmmm… still scratching my head with this one. Let me narrow the question, is it possible to “order by” ‘range’?:

    $postids=$wpdb->get_col($wpdb->prepare("
    SELECT      ID
    FROM        $wpdb->posts
    WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
    ORDER BY    $wpdb->posts.post_title",range ('A' , 'I')));
    Thread Starter formica

    (@formica)

    Ok getting nearer. I’ve found a way to display all post titles with multiple first letters a-i.

    I just need to create three links and pass that ‘variable’ so it displays posts with either ‘a-i’, j-q’, ‘r-z’ and can’t quite get it but feel i’m really close. Any help really appreciated.

    <a href="?range=a_i">A-I</a>
    <a href="?range=j_q">J-Q</a>
    <a href="?range=r_z">R-Z</a>
    
    <?php
    $a_i = "'A','B','C','D','E','F','G','H','I'";
    $j_q = "'J','K','L','M','N','O','P','Q'";
    $r_z = "'R','S','T','U','V','W','X','Y','Z'";
    
    $range=$_GET['range'];
    
    $postids=$wpdb->get_col($wpdb->prepare("
    SELECT      ID
    FROM        $wpdb->posts
    WHERE       SUBSTR($wpdb->posts.post_title,1,1) IN ($range)
    ORDER BY    $wpdb->posts.post_title"));
    Thread Starter formica

    (@formica)

    Ahhh….. Gave up and used ‘if else’

    <a href="?range=a_i">A-I</a>
    <a href="?range=j_q">J-Q</a>
    <a href="?range=r_z">R-Z</a>
    
    <?php $range = $_GET['range'] ?>
    
    <?php if ($range == 'a_i') { ?>
    
    // show list  A - I
    
    <?php } elseif ($range == 'j_q') { ?>
    
    // show list  A - I

    So ugly it needs a bag but my brain hurts.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Alphabetical post list’ is closed to new replies.