• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2784

Original Genesis Tutorials & 6000+ Guaranteed Code

Snippets

  • Consultation
  • Full Access
  • Log in

Show Related Posts by Primary Category Only

This tutorial includes the code which enables you to display related posts in the same category when that category is set as your primary category using the Primary Category setting in the category box on single post edit screens.

Note : Requires the installation of the Yoast SEO plugin to add the primary category option.

This primary category setting is used when you assign a post multiple categories and want to set a primary.

Uses the WPSEO_Primary_Term class in Yoast SEO.

Demo Video

Tested using the genesis Sample child them by StudioPress however will work in any Genesis child theme.

Installation Steps

  1. # Copy and paste the PHP code ( without the opening PHP tag ) to the end of your child themes functions file.
  2.  

  3. # Copy & paste the CSS from the style.css file to the end of your child themes style sheet and clear caching.
  4.  

  5. # Install the Yoast SEO plugin and select the primary category for posts assigned multiple categories.
  6.  

Download Folder

Related Posts

  • Genesis Custom Block for Related Posts
  • How To Add Related Posts Between Paragraph Content

Taxonomy Terms

Reader Interactions

Comments

  1. Jonathon Fowler says

    December 14, 2021 at 2:22 pm

    One more question please Brad…if I view the post on a moblie device, the thumbnail featured images don’t display, only the link anchor text appears for each related post. How do I get the thumbnail to remain, even on mobile devices?

    Log in to Reply
    • Brad Dalton says

      December 14, 2021 at 2:34 pm

      Remove this CSS rule from your style.css file.

      .grid-image {
          display: none;
      }
      Log in to Reply
      • Jonathon Fowler says

        December 14, 2021 at 7:07 pm

        Great, that worked perfectly. Thanks man!

        Log in to Reply
  2. Jonathon Fowler says

    December 14, 2021 at 2:11 pm

    Hi Brad, I installed this and it works, thank you.

    How do I get it to only display the three latest posts within the Primary category? At the moment it displays 3 random posts that change each time you refresh the page.

    Thanks, Jonathon

    Log in to Reply
    • Jonathon Fowler says

      December 14, 2021 at 2:14 pm

      Sorted, I changed the ‘rand’ within the functions file to ‘date’. Thanks, works great!

      Log in to Reply
  3. Jonathon Fowler says

    November 3, 2021 at 12:07 pm

    I have a few different categories on my blog.

    For each blog post, I only use one category.

    Will this enable displaying related posts only from the same category?

    At the moment my blog shows the 3 most recent posts regardless of category.

    Log in to Reply
    • Brad Dalton says

      November 3, 2021 at 12:10 pm

      It will and you can also use this code https://wpsites.net/web-design/how-to-code-a-custom-block-for-a-related-posts-query-in-genesis/

      Or this https://wpsites.net/web-design/related-posts-with-images/

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags
 

Loading Comments...
 

You must be logged in to post a comment.