[Support request] Custom Blog Posts Page – Displaying Post Categories in Blocks?

Home Forums Support [Support request] Custom Blog Posts Page – Displaying Post Categories in Blocks?

Home Forums Support Custom Blog Posts Page – Displaying Post Categories in Blocks?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1862028
    StoicCorn

    Hello,

    I’ve tried tinkering with settings in GeneratePress/GenerateBlocks as well as searched the forum and I think I’ve come up with the answer but wanted to confirm to be sure.

    Is it not possible to have blocks with blog categories?

    I’d like my Blog Post Page to have blocks at the top with each category and then a list of all posts.

    As of now, I believe I can accomplish the bottom portion with WP Show Posts Pro but am having difficulty with achieving the first part.

    I’ve created this image as an example of what I’d like.

    Is there anyway to do this?

    #1862147
    Elvin
    Staff
    Customer Support

    Hi there,

    This should be possible with just WP Show Posts Pro but there’s a caveat.

    You can’t use pagination on “List of All Blog Posts” as pagination gets bugged when there are multiple WP_Query on the page.

    For the 3 Blog post categories, you can place a WPSP list on them as well w/ each WPSP list having its own specific category query set on WPSP Edit page’s Post tab.

    You then make sure all these categories don’t display anything other than the title, basically making it into a “list”.

    Alternative to making 3 separate lists, you can simply make 1 list and set the shortcode attribute change the query.

    Example:

    [wp_show_posts id="1234" settings="taxonomy=category&tax_term=cats"]
    [wp_show_posts id="1234" settings="taxonomy=category&tax_term=dogs"]
    [wp_show_posts id="1234" settings="taxonomy=category&tax_term=birds"]

    These 3 shortcodes will use the same WPSP list but will display different posts depending on th specified tax_term.

    As for the layout:

    The top row will be a grid block w/ 3 columns with each columns containing WPSP shortcodes of specified tax_terms.

    The bottom one will be a container block containing the WPSP shortcode for ALL the blog posts.

    #1863375
    StoicCorn

    Hi Elvin,

    Thank you for your reply.

    However, the top three blocks I would want as a link to a page that has the list of posts rather than having them displayed inside the block.

    So in your shortcode example, it would be Cats, Dogs, and Birds and when you click on Cats it takes you to a page with a list of all Cats posts.

    Is it possible to set it up that way?

    #1863769
    Elvin
    Staff
    Customer Support

    So in your shortcode example, it would be Cats, Dogs, and Birds and when you click on Cats it takes you to a page with a list of all Cats posts.

    If this is the case, I think there shouldn’t be any need for WPSP for this.

    I believe you can just place a GB Headline block and add a link pointing to the specified category archive page. Or perhaps I’m missing something?

    Let us know.

    #1865426
    StoicCorn

    Hi Elvin,

    That’s exactly what I’d like to have happen but was wondering if it was possible to generate those links manually. This way, when I write about something in a new category, a block automatically gets added to the page.

    #1865531
    Elvin
    Staff
    Customer Support

    Ah you mean the category dynamically changes as well to whatever category the latest post is under?

    If so, you may have to get a plugin that does that.

    Or, here’s one that’s a bit tricky.

    Create a shortcode that does a query for the latest post but instead of getting the rest of the post’s details like title, date, etc, just fetch the category.

    Example:

    add_shortcode( 'latest_post_cat', function($atts) {
        $atts = shortcode_atts(
            array(
                'offset' => '0',
                'posts_per_page' => '1',
        ), $atts, 'latest_post_cat' );
    	
        $args = array(
            'posts_per_page' => $atts['posts_per_page'], // we need only the latest post, so get that post only
            'offset' => $atts['offset'],
        );
    	
        $q = new WP_Query( $args);
    
    	ob_start();
    	
        if ( $q->have_posts() ) {
            while ( $q->have_posts() ) {
            $q->the_post(); 
                echo '<a href="' . esc_url( get_category_link( get_the_category()[0]->term_id ) ) . '"><h1 class="latest-post-cat-name">'.get_the_category()[0]->name.'</h1></a>';
            }
            wp_reset_postdata();
        }
        return ob_get_clean();
    } );

    Here’s a PHP snippet for a shortcode that displays the category link and category name of the latest post.

    Example usage/s: [latest_post_cat offset="1" posts_per_page="5"] or[latest_post_cat offset="2"]

    Where offset is the offset value from the latest page. post_per_page is optional. It sets the number of posts(category name of the post) to display.

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