Add link to listing in attributes

Hello, I was wondering if it was possible to have the two custom attributes (highlighted in yellow in the screenshot) linked to the listing page? I’d like to people to be able to click on those two areas and be taken to the listing information in addition to clicking on either the graphic or listing title. Thanks!

Hello and welcome,

Here’s a possible solution :
<a href="%value%" target="_blank" rel="nofollow">sample text</a>

This has been discussed (and solved) many times over in this forum.
Please try to use the search feature next time.

Cheers

1 Like

I saw that as a solution if %value% = the URL when creating a custom attribute (ie: the attribute is a website address). I wanted the URL to be the listing’s URL, which I haven’t seen a solution for.

Hi,

Please clarify whether you want the URL attribute in this listing to display a link to this same listing. If not, please describe your use case in more detail, and we will try to provide a solution or workaround.

My goal isn’t to have a URL attribute, it’s to have a text attribute link to the listing itself.

In my example photo, I have to custom text attributes: 1) a Japanese translation of the title of the listing (in this case “Website Ad”) and an attribute that shows whether the item is available or unavailable.

I would like to be able to link those two attributes to the listing itself so that in addition to clicking on the photo or listing title, you can also click on the Japanese translation and the “Available” and be taken to the listing.

I tried to see if I could use a token on the backend (ie: <a href="%token that links to listing%">%value%</a>) but 1) I couldn’t figure out if there was a token that links to the listing and 2) it appears that only a limited number of tokens are available to use here.

Hopefully that clarifies what I’m looking for and thanks for any help that you can provide.

This is one good attribute, because sometimes people would not know how to see the whole listing on a page.
Second, an option to click something and directly BUY or BOOK could be also there in the Block. This saves the need to go to the Single Listing to do that.
Has anyone done that ?

This is untested code, but here’s AI’s attempt.

Use with caution.

Add it via the Code Snippets plugin and replace my_custom_attribute with your actual attribute’s slug name.

add_filter( 'hivepress/v1/templates/listing_view_block', 'custom_link_attribute', 10, 2 );

function custom_link_attribute( $template, $context ) {
    // Check if attributes are present in the context
    if ( isset( $context['attributes'] ) ) {
        foreach ( $context['attributes'] as $key => $attribute ) {
            // Replace 'my_custom_attribute' with your attribute's slug
            if ( $attribute->get_slug() === 'my_custom_attribute' ) {
                // Get the listing object
                $listing = $context['listing'];
                // Wrap the attribute display in a link
                $linked_attribute = '<a href="' . esc_url( get_permalink( $listing->get_id() ) ) . '">' . $attribute->display() . '</a>';
                // Replace the original attribute display
                $context['attributes'][ $key ] = $linked_attribute;
            }
        }
        // Update the template context with modified attributes
        $template['context']['attributes'] = $context['attributes'];
    }
    return $template;
}

I hope this works/helps!

Cheers,
Chris :victory_hand:

Hi,

I see, thanks for the details. Unfortunately, there is no such feature, it will require custom development. If customizations are required for your site, please try customizing it using the collection of code snippets Search · user:hivepress · GitHub and other developer resources, or consider hiring someone for custom work Customize your website | HivePress

fatal error when i tried this.
please suggest what is the right code