Site logo

Archived topic

Grid/gallery of category images for Blog Page

7 replies · Started by Fergal on June 4, 2021

Viewing posts 1–8 of 8

Hey there,

I’m trying to do something similar to what was done here https://generatepress.com/forums/topic/grid-gallery-of-category-images/#post-1810852, but I want to display my top-level categories on my Blog page. I added images to categories using https://wordpress.org/plugins/categories-images/ and added the shortcode you typed out above to the “code snippets” plugin. I then tried to add the short code to the blog page using [category_grid id=”6″], but it isn’t working. Can you please tell me what I’m doing wrong or is there a better way to do this now?

Thanks,
Fergal

Hi there,

couple of things:

1. The shortcode doesn't support arguments so it has to be: [category_grid]
To limit it to the just the parent categories, you would edit Tom's snippet and include the 'parent' => 0, arg ie.

$args = array(
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => 1,
    'parent' => 0,
);

2. How are you adding the shortcode to the Blog Page ? As it would need to be hooked in.

Hey David,

I made the adjustments you mentioned in 1.

2. Clearly I have a lot to learn about shortcodes and hooks as I was trying to just insert the shortcode into the blog page via Gutenberg. Now, I created a hook element where hook is "generate_before_main_content" and display location is "Blog". Then checked the execute shortcodes box and inserted [category_grid] into the editor. It works like a charm.

Thanks so much!
Fergal

Glad to be of help :)

Hey David,

Can we please modify the php to only show this category grid on the first page of the blog page?

I don't want the category grid showing up on /blog/page/2, /blog/page/3, etc. only on /blog/.

Thanks,
Fergal

Hi Fergal,

I don't think that's doable in PHP without breaking the pagination linking. The navigation would break because the links won't work (all will point to /blog).

If you simply want to display /blog on the address bar of the browser, you can do that with JS but that customization is out of our scope.

Hey Elvin,

I think there was some confusion as to what I was looking for. I was able to find a condition that worked for this task:
if ( !is_paged())

and now the categories only show up on the first page of the blog posts page.

Regards,
Fergal

Nice one. Glad you got it sorted. Thanks for letting us know. :)

This archived topic is closed to new replies.