Join our new community forum for support & discussion
Join NowHome › Support › Themes › ListingHive › Use attributes with php code
Use attributes with php code
-
AuthorPosts
-
tempo 11 months, 1 week ago
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.
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 usehivepress()->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 listingtempo 11 months, 1 week agocan 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?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 11 months, 1 week agothanks 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).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 contextlisting
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 attributeVerified
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 withecho
php function or some another wayYou 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 11 months agosorry 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?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
hookThere is tutorial how to customise templates https://youtu.be/LkojYp-8uwY
tempo 11 months agowhere 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.
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-8uwYAlso 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 -
AuthorPosts
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.