LocalBusiness Schema Snippet

Add this PHP code to your child theme’s functions.php or via Code Snippets plugin:

add_action('wp_head', function() {
    if (!is_singular('hp_listing')) return;
    global $post;
    $listing_id = $post->ID;
    $title = get_the_title($listing_id);
    $desc = wp_strip_all_tags(get_the_excerpt($listing_id));
    if (!$desc) $desc = wp_strip_all_tags(substr(get_the_content(null, false, $post), 0, 200));
    $address = get_post_meta($listing_id, 'hp_location', true);
    if (!$address) $address = get_post_meta($listing_id, 'hp_address', true);
    $phone = get_post_meta($listing_id, 'hp_phone', true);
    $website = get_post_meta($listing_id, 'hp_website', true);
    $image = get_the_post_thumbnail_url($listing_id, 'large');
    $terms = get_the_terms($listing_id, 'hp_listing_category');
    $category = ($terms && !is_wp_error($terms)) ? $terms[0]->name : 'Local Business';
    $schema = [
        '@context' => 'https://schema.org',
        '@type' => 'LocalBusiness',
        'name' => $title,
        'description' => $desc,
        'address' => [
            '@type' => 'PostalAddress',
            'streetAddress' => $address,
            'addressLocality' => 'Anchorage',
            'addressRegion' => 'AK',
            'addressCountry' => 'US'
        ],
    ];
    if ($phone) $schema['telephone'] = $phone;
    if ($website) $schema['url'] = $website;
    if ($image) $schema['image'] = $image;
    echo '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '<\/script>' . "\n";
});

// BreadcrumbList on taxonomy pages
add_action('wp_head', function() {
    if (!is_tax('hp_listing_category')) return;
    $term = get_queried_object();
    if (!$term) return;
    $schema = [
        '@context' => 'https://schema.org',
        '@type' => 'BreadcrumbList',
        'itemListElement' => [
            ['@type' => 'ListItem', 'position' => 1, 'name' => 'Home', 'item' => home_url('/')],
            ['@type' => 'ListItem', 'position' => 2, 'name' => 'Listings', 'item' => home_url('/listings/')],
            ['@type' => 'ListItem', 'position' => 3, 'name' => $term->name, 'item' => get_term_link($term)],
        ]
    ];
    echo '<script type="application/ld+json">' . json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . '<\/script>' . "\n";
});

Sign In

Register

Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.