• Resolved buzzlyhan

    (@buzzlyhan)


    Hello,

    I am creating a block theme, but when moving to the next post (next post button), how do I display only the same category in the single-post.html template?

    • This topic was modified 1 year, 7 months ago by buzzlyhan.
Viewing 7 replies - 1 through 7 (of 7 total)
  • When working on a block theme in WordPress and aiming to display posts from the same category when moving to the next post using a “Next Post” button, you can achieve this by implementing the following steps:

    1. Retrieve Current Post’s Category:
      In your single-post.php template file, begin by retrieving the category of the current post:
      $current_post_categories = wp_get_post_categories(get_the_ID());
    2. Query for Next Post in the Same Category: Next, create a custom query to retrieve the next post within the same category: ( https://prnt.sc/-I9dicDoJ4vH )
    3. Display Next Post’s Content: Loop through the query results and display the content of the next post if it exists: ( https://prnt.sc/yU2HZboQTCtO )
    4. Implement the “Next Post” Button: Add a “Next Post” button that links to the next post within the same category: ( https://prnt.sc/b6Gpcgbbas6w )

    By following these steps, you can ensure that when users navigate to the next post, they will be presented with posts from the same category. This creates a seamless and coherent browsing experience within your block theme.

    Thread Starter buzzlyhan

    (@buzzlyhan)

    Thank you Md. Ibrahim Khalil!

    It’s a bit challenging, but I’ll give it a try.

    Thread Starter buzzlyhan

    (@buzzlyhan)


    Ah, so this is a PHP file for a classic theme. Do you know how to implement pagination for posts in the same category in a block theme?
    Thanks.

    Start by initializing a custom query to retrieve posts from the same category. You can use the paged parameter to control the pagination: with WP_Query();

    Hello @buzzlyhan,

    Have you tried implementing this yourself? If it’s successful, you can mark as resolved this thread! Thanks!

    Thread Starter buzzlyhan

    (@buzzlyhan)

    Hi, Ibrahim Khalil.
    Sorry for forgetting to reply.
    It’s possible with WP_Query(), isn’t it? However, I was looking for block HTML.
    It seems like there are plans to add new features to Block.

    https://github.com/WordPress/gutenberg/issues/47090

    Thank you!

    Yes, this is possible with WP_Query(), you can try.
    You are welcome! Have a good day!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How do I display only the same category’ is closed to new replies.