- This topic has 5 replies, 2 voices, and was last updated 2 years, 3 months ago by Elvin.
-
AuthorPosts
-
December 7, 2021 at 1:39 am #2040864Denis
Hello everyone,
I am using the elements feature and want to include a dynamic CSS. So I need the wordpress post ID of the element. I need the ID in functions.php.
If I user “get_the_ID()” I get the ID of the post ID. But I need the included elements post ID.
I hope you can help me.
Thank you,
best regards.December 7, 2021 at 3:44 am #2040956ElvinStaffCustomer SupportHi Denis,
This is quite tricky to do but here’s a PHP function that may work for what you’re trying to do:
function get_gp_element_ID_of_post($name){ global $generate_elements; $GP_elememt_name_check = $name; $list_gp_elements = array(); foreach ( (array) $generate_elements as $key => $data ) { $type = esc_html( GeneratePress_Elements_Helper::get_element_type_label( $data['type'] ) ); $list_gp_elements[] = array( 'type' => $type, 'name' => get_the_title( $data['id'] ), 'url' => get_edit_post_link( $data['id'] ), 'element_id' => $data['id'], ); } foreach ( (array) $list_gp_elements as $gp_element_item => $gp_element_item_info ) { if($gp_element_item_info['name'] !== $GP_elememt_name_check ){ $el_id = 'Error: This is not an active GP Element on the current page'; } else { $el_id = $gp_element_item_info['element_id']; } } return $el_id; }
And the usage would be
get_gp_element_ID_of_post('Name of GP element');
which will return the ID of theName of GP element
.The function only checks for currently active elements on a post. If the name indicated on the function is not an active element on the current page, it will return
Error: This is not an active GP Element on the current page
.December 7, 2021 at 1:10 pm #2041686DenisHi Elvin,
thank you for your answer. But now I have two functions in my functions.php which are nearly the same.
It’s also possible that I get a list of active GP elements (the ID’s) from that page. I think this is the better solution.
Background: I use PostX and this seems so be not working with GP elements. The inline-styles will not be included.
December 7, 2021 at 8:10 pm #2041907ElvinStaffCustomer SupportIt’s also possible that I get a list of active GP elements (the ID’s) from that page. I think this is the better solution.
global $generate_elements; $list_gp_elements = array(); foreach ( (array) $generate_elements as $key => $data ) { $type = esc_html( GeneratePress_Elements_Helper::get_element_type_label( $data['type'] ) ); $list_gp_elements[] = array( 'type' => $type, 'name' => get_the_title( $data['id'] ), 'url' => get_edit_post_link( $data['id'] ), 'element_id' => $data['id'], ); }
This chunk of code from the function I’ve provided actually gets the active GP elements. If you
var_dump($list_gp_elements);
, you’ll see an array of the GP elements active on the page.Background: I use PostX and this seems so be not working with GP elements. The inline-styles will not be included.
Not exactly sure what you mean. Were you trying to hook things into the html structure rendered by PostX? If so, you’ll have to be sure w/ the hooks it uses.
December 7, 2021 at 11:07 pm #2042005DenisOK, thank you. It has helped me a lot.
December 7, 2021 at 11:10 pm #2042007ElvinStaffCustomer SupportNo problem. 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.