[Resolved] Get the element ID in functions.php

Home Forums Support [Resolved] Get the element ID in functions.php

Home Forums Support Get the element ID in functions.php

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2040864
    Denis

    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.

    #2040956
    Elvin
    Staff
    Customer Support

    Hi 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 the Name 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.

    A wise man once said:
    "Have you cleared your cache?"

    #2041686
    Denis

    Hi 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.

    #2041907
    Elvin
    Staff
    Customer Support

    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.

    	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.

    A wise man once said:
    "Have you cleared your cache?"

    #2042005
    Denis

    OK, thank you. It has helped me a lot.

    #2042007
    Elvin
    Staff
    Customer Support

    No problem. 🙂

    A wise man once said:
    "Have you cleared your cache?"

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.