Site logo

Archived topic

Categories List not posts on Archive page

32 replies · Started by David on August 11, 2022

Viewing posts 1–15 of 33

Hello

How do I show categories in an archive page and not just posts?

I have set up categories that need to show first on the archive page, then once a category is clicked it shows the posts that are linked to that category.

See mock up below...

https://ibb.co/sKJZpPQ

So the Massey Ferguson 100 Series, Massey Ferguson 200 Series etc are categories listed on an archive page. How do I achieve this using generatepress, generateblocks.

Here is the website link...
https://staging.howardandsons.co.uk/massey-ferguson/

Thanks
Dave

Hi there,

just so i am clear you:

1. Have a Custom Taxonomy of: massey-ferguson-categories
2. You require an archive page with the slug /massey-ferguson-categories
3. This archive page will display a list of the custom terms.
4. Each term will link to relevant term archive eg. /massey-ferguson-categories/my-term

Is that it ?

Hi David

Yes that is correct :)

Thanks
Dave

Or is it possible to set up a page with a query loop that shows category image (ACF), category title, and links to the category page? So for example /massey-ferguson-categories/my-term

OK, first off if you have created a term eg. my-term in your custom taxonomy and there are posts in that, then visiting:

https://staging.howardandsons.co.uk/massey-ferguson-categories/my-term

Should automatically display all the posts within that specific tax term.

But there is no default taxonomy template for this https://staging.howardandsons.co.uk/massey-ferguson-categories/

For that you would need to create a Static Page with that same name.
And then we would need to create a custom shortcode to display the list of terms.

Couple of questions:

1. Are there many custom taxonomy pages like this?

2. The List of terms on the page, are there parent / child terms and do you want to display the post count ?

Ok, try adding this PHP Snippet to your site:

add_shortcode('custom-tax', function(){
	
    // get page title
    // page title must match the relevant taxonomy
    $tax = get_the_title();
    // get list of terms from custom taxonomy
    $terms = get_terms( $tax );
    $html = '<ul class="custom-term-list">';
    foreach($terms as $term) {
        $term_link = get_term_link( $term );
        $html .= '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
    }
    $html .= '</ul>';
    return $html;
	
});

This will create a shortcode: [custom-tax]

Note that Page Name must be identical to the Taxonomy you want to list.

You can't use a Query Loop at this time, as those are purely for getting Posts.
In the future we will be using Query Loop in GenerateBlocks for looping through other data like this.

For now it would require custom code.
First question though - do you have any terms with published posts in that taxonomy ?

So could I create a layout in the file taxonomy-massey-ferguson-categories.php

That would apply to the term archive eg.

https://staging.howardandsons.co.uk/massey-ferguson-categories/massey-ferguson-series-200/

What you're trying to do here is create what some may call a basic Silo page. Its not something that WP does out the box.
Try changing the snippet i provided to this:


add_shortcode('custom-tax', function(){
	
    // get page title
    // page title must match the relevant taxonomy
    $tax = get_the_title();
    // get list of terms from custom taxonomy
    $terms = get_terms( array(
        'taxonomy' => $tax,
        'hide_empty' => false,
    ) );
    $html = '<ul class="custom-term-list">';
    foreach($terms as $term){
        $term_link = get_term_link( $term );
        $html .= '<li><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></li>';
    }
    $html .= '</ul>';
    return $html;
	
});

Do you se the the list of terms ?

Ive updated the code, but it's not showing the list still.

Shall I create a hook that loads in to the page that way I can add some php and style it similar to this...
https://ibb.co/QMnV15s

Thanks David, I'll take a look. I didn't realise it was this hard to have a parent taxonomy category page.

Just looking for something similar too woocommerce where you can see categories first in archive pages, rather rather than straight to the products.

This archived topic is closed to new replies.