Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Edit snippet to use rank math primary category for if there is no primary?

  • Hello,

    I found the following snippet in another post here to use the Rank Math primary category to show the primary category instead of all categories on some query loop blocks I set up:

    /*add primary category for buttons for posts*/
    add_filter('render_block', function ($block_content, $block) {
        if (
            !is_admin() &&
            !empty($block['attrs']['className']) &&
            strpos($block['attrs']['className'], 'placeholder-for-primary-cat') !== false
        ) {
            $cat = false;
            $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
    
            if ($primary_cat_id) {
                $cat = get_term($primary_cat_id, 'category');
                if (!is_wp_error($cat)) {
                    $primary_cat = $cat->name;
                    $primary_term_url = get_term_link($cat);
                    $primary_term_output = '<a class="primary-cat-' . esc_attr($primary_cat) . '" href="' . esc_url($primary_term_url) . '">' . esc_html($primary_cat) . '</a>';
                    $block_content = str_replace('primary-category-placeholder', $primary_term_output, $block_content);
                }
            } else {
                $categories = get_the_category();
                $first_category = '';
    
                if (!empty($categories)) {
                    $first_category = $categories[0]->name;
                    $first_category_url = get_term_link($categories[0]);
                    $first_category_output = '<a class="primary-cat-' . esc_attr($first_category) . '" href="' . esc_url($first_category_url) . '">' . esc_html($first_category) . '</a>';
                    $block_content = str_replace('primary-category-placeholder', $first_category_output, $block_content);
                }
            }
        }
        return $block_content;
    }, 10, 2);

    …it looked great. However, I found that, as I had implemented Rank Math recently, some older posts did not have a primary category yet and these posts all had a critical error until I removed the snippet.

    Is there any way to edit this so that it will show either nothing or one category and not a critical error until I can get around to adding a primary category to all posts?

    Thanks

  • Hi there,

    Try this one. I’ve made a slight adjustment to the existing code:

    add_filter('render_block', function ($block_content, $block) {
        if (
            !is_admin() &&
            !empty($block['attrs']['className']) &&
            strpos($block['attrs']['className'], 'placeholder-for-primary-cat') !== false
        ) {
            $cat = false;
            $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_category', true );
    
            if ($primary_cat_id) {
                $cat = get_term($primary_cat_id, 'category');
    
                if (is_wp_error($cat)) return;
    
                $primary_cat = $cat->name;
                $primary_term_url = get_term_link($cat);
                $primary_term_output = '<a class="primary-cat-' . esc_attr($primary_cat) . '" href="' . esc_url($primary_term_url) . '">' . esc_html($primary_cat) . '</a>';
                $block_content = str_replace('primary-category-placeholder', $primary_term_output, $block_content);
            } else {
                $categories = get_the_category();
                $first_category = '';
    
                if (!empty($categories)) {
                    $first_category = $categories[0]->name;
                    $first_category_url = get_term_link($categories[0]);
                    $first_category_output = '<a class="primary-cat-' . esc_attr($first_category) . '" href="' . esc_url($first_category_url) . '">' . esc_html($first_category) . '</a>';
                    $block_content = str_replace('primary-category-placeholder', $first_category_output, $block_content);
                }
            }
        }
        return $block_content;
    }, 10, 2);
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.