Join our new community forum for support & discussion

Join Now

Home Support Themes ListingHive Use attributes with php code

Use attributes with php code

  • Author
    Posts
  • tempo

    hi, i want to use attributes into a php code.
    How can i get attribute’s names and/or values?

    In case of custom files i use get_post_meta()

    but i don’t know how can i get attribute’s names and/or values.

    Thanks for the support and for the great theme.

    yevhen developer

    If you want to use get_post_meta then you can get attributes for some post by its id in this way. To get attributes names for some model you can use hivepress()->attribute->get_attributes('model_name') but it will contain only general information about attribute where you can get its name/label.

    But if you want to implement your custom code for example for listing page then please try to use HivePress hooks. For example you can use hivepress/v1/templates/listing_view_page/blocks to get context of current listing where you will find attributes names and their values for current listing

    tempo

    can i have an example?

    Suppose that i have attribute with name “option3” and i want to get the value.
    Can you give me the php code about this example?

    yevhen developer

    Please check this topic. There is example with checking/getting Verified attribute https://hivepress.io/support/topic/hide-reply-to-listing-on-unverified-listings/#post-27343

    tempo

    thanks but i think that the post you suggest is not what i want to do.
    I want a way to display an attribute value using php (if is possible).

    yevhen developer

    Here is code which I gave by link above

    add_filter(
    	'hivepress/v1/templates/listing_view_page/blocks',
    	function ($blocks, $template){
    		$listing = $template->get_context('listing');
    		
    		if( $listing && !$listing->get_verified() ){
    			$blocks = hivepress()->helper->merge_trees(
    					[ 'blocks' => $blocks ],
    					[
    						'blocks' => [
    							'message_send_link' => [
    								'type' => 'content',
    							],
    						],
    				]
    				)['blocks'];
    		}
    		
    		return $blocks;
    	},
    	1000,
    	2
    );

    As you see there is hook hivepress/v1/templates/listing_view_page/blocks which works on single listing page.
    Then there is parameter/variable $template which has context listing which contains current listing object. You get this context in this way $listing = $template->get_context('listing');
    Now you have variable $listing which has all values which belong to current listing.
    As in example code you want to get value of attribute Verified and you write this code $listing->get_verified(). In this way you get value of attribute ->get_attribute_name_here(). When you get value of attribute then you can assign it to some block/code or show with echo php function or some another way

    You can also check our short tutorials for developers https://youtu.be/LkojYp-8uwY
    There is a tutorial about customizing templates.

    Also we have code reference https://hivepress.github.io/code-reference/ where you find some useful information of how some parts of code works in HivePress

    tempo

    sorry but i can’t do the work.

    step_1) i add your’s code

    add_filter(
    	'hivepress/v1/templates/listing_view_page/blocks',
    ................
    ................
    1000,
    	2
    );

    using Code Snippets plugin ( https://wordpress.org/plugins/code-snippets/ )

    step_2) i add a php widget into Listing (sidebar) with this code

    <?php
    $listing->get_MyAttribute_name();
    echo $listing;
    ?>

    …nothing happen….i receive an error message.
    Do you know what i’m doing wrong?

    yevhen developer

    Unfortunately there is not possibility to get context of listing in widget. If you want to add some functionality to Listing sidebar you can try edit it with hivepress/v1/templates/listing_view_page/blocks hook

    There is tutorial how to customise templates https://youtu.be/LkojYp-8uwY

    tempo

    where can i add this code

    <?php
    $listing->get_MyAttribute_Name();
    echo $listing;
    ?>

    to display the attribute’s value?

    Edit the template file (is the only way)?

    What file i must edit to display the attribute’s value?

    Remember that i want to add the values into a listing page (and not on Listing sidebar).

    Once again, thanks for the support.

    yevhen developer

    If you want to make change on listing page then you can use hook hivepress/v1/templates/listing_view_page/blocks
    Listing sidebar is one of the part of listing page so you can also edit it with this hook. Please check this tutorial how to customise templates https://youtu.be/LkojYp-8uwY

    Also if you write listing_view_page in search field on our support forum then you can find similar topics where were code snippets to edit listing page. In this way you can do changes by your requirements

Viewing 10 posts - 1 through 10 (of 10 total)

New Reply

This forum has been archived and is no longer accepting new posts or replies. Please join our new community forum for support & discussion.