Site logo

Archived topic

Best way to make a glossary

9 replies · Started by Robert on February 7, 2023

Viewing posts 1–10 of 10

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?

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.

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?

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?

That is correct :)

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

Hi there,

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

1. Order: ASC
2. Order by: Title

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

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

This archived topic is closed to new replies.