Site logo

[Support request] Best way to make a glossary

Home Forums Support [Support request] Best way to make a glossary

Home Forums Support Best way to make a glossary

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #2525030
    Robert

    I am trying to make a glossary just like this one:

    https://ahrefs.com/seo/glossary

    What’s the best way or the recommended way to do this with generatepress?

    #2525059
    Ying
    Staff
    Customer Support

    Hi Robert,

    You can use GenerateBlocks’s Query loop block to output a certain category of posts.

    The homepage would be a static page, then you can add Query loop blocks by category.

    #2525083
    Robert

    So, I’d need to make every individual page manually, and then I can link them all up on a /glossary page using a Query loop?

    #2525107
    Ying
    Staff
    Customer Support

    I’d need to make every individual page manually

    Do you mean a page like this one? https://ahrefs.com/seo/glossary/10x-content

    If so, you can create a block element - content template to assign a certain layout for all those individual pages, then in the page editor, you only need to add the actual content.

    https://docs.generatepress.com/article/block-element-content-template/

    #2526419
    Robert

    Okay, so I would 1) make the block element content template, 2) create the individual pages for each entry and apply the block element content template to those pages, and 3) link them all up on a /glossary page using a Query loop?

    Is that right? Anything else?

    #2526593
    Ying
    Staff
    Customer Support

    That is correct 🙂

    #2527002
    Robert

    Is there a way to sort the pages alphabetically using the query loop?

    #2527064
    David
    Staff
    Customer Support

    Hi there,

    select the Query Loop block, and in Parameters Add 2 x new Parameters:

    1. Order: ASC
    2. Order by: Title

    #2527069
    Robert

    Will that enable me to create sections for each glossary term, where each section is a letter? For example, A, B, C, etc.

    #2527406
    David
    Staff
    Customer Support

    Aah, ok.
    Thats a lot more tricky, and for now i would not use a Query Loop for that.
    Instead you can create your own custom loop shortcode with this PHP Snippet:

    function post_glossary() {
        global $post;
        // Set query args
        $args = array(
            'post_type' => 'post',
            'posts_per_page' => '-1',
            'orderby' => 'title',
            'order' => 'ASC',
            'ignore_sticky_posts' => 1
        );
    
        $glossary = new WP_Query($args);
        // Output loop
        if ( $glossary->have_posts() ) {
    		ob_start();
            $curr_letter = ''; 
            echo '<div class="post-glossary">';     
            while ($glossary->have_posts()) : $glossary->the_post();
                $title = get_the_title();
                $this_letter = $title[0];
                if ( $this_letter != $curr_letter ) {
                    if ( '' != $curr_letter ) {
                        echo'</div></section>';
                    }
                    echo '<section>';
                    echo '<h2 class="section-title">' . $this_letter . '</h2>';
                    echo '<div class="posts-wrapper">';
                }
                $curr_letter = $this_letter;
    
                do_action('db_glossary_content');
    		
            endwhile;
            wp_reset_postdata();
            return ob_get_clean();
        }
    }
    add_shortcode('glossary', 'post_glossary');

    Then on your glossary page, add [glossary]

    This will create the Loop, that will create a section for each letter, and inside that loop is a custom hook: db_glossary_content which we can use to display each post.

    Now create a Block Element – Content Template
    Add your content template, for displaying the title, exerpt etc.
    But change the Element Type from Content Template to Hook.
    Set the Hook to Custom, and in the field provided add: db_glossary_content
    Set the Display Rules to Entire Site

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.