Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Dynamic Data

  • Hi,
    I manage a blog wich is set up with GP pro and GB pro. The blog represents the activities of 17 different organizations.
    I am the only “user” set up in WordPress. The different organizations have no access to the WP front end. They provide me with the text and the pictures for the blog articles and I put them online. So far so good.
    I am using Elements to set up SinglePost Hero and Single Post content. Headline, date and content is pulled in using Dynamic Data Option in GP pro.
    Now I need a field (taxonomy??) where I can put the name of the organization in the blog post and also pull it in using Dynamic Data.
    I understand that ACF doesn’t work with Dynamic Data and anyway I am totally unexperienced with ACF. Is there another option to solve this problem?

    I hope I expressed myself clearly….
    Many thanks
    Klaus

  • Hi there,

    ACF will work with Dynamic Data, but only for basic fields today, we have more planned for the future. It won’t handle repeater or relationships for example. But it can do a lot of things, and a lot more things with some code.

    So – a question – is it just the Organisation name that you need to appear on the post ?

    And could that just be Taxonomy Term ? such as a Tag or custom taxonomy ?

  • Hi David, thanks for the fast response. It is just the Organization Name. Basically it would replace the author name. Taxonomy Term would probaly do the job. I also thought about Tags but really didn’t want to use this in case I need this later.

  • Ok, so you can register a new taxonomy with a little PHP. Are you ok with WP creating archive pages for the new terms ?

  • yes I am. And I also know how to handle the php. My installation has a child theme.

  • Ok, so this site is handy for creating custom taxonomies and other stuff:

    https://generatewp.com

    I made a quick example here, with the Tax named: Organisations

    function db_custom_taxonomy() {
    
      $labels = array(
        'name'              => _x( 'Organisations', 'taxonomy general name' ),
        'singular_name'     => _x( 'Organisation', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Organisations' ),
        'all_items'         => __( 'All Organisations' ),
        'parent_item'       => null,
        'parent_item_colon' => null,
        'edit_item'         => __( 'Edit Organisation' ),
        'update_item'       => __( 'Update Organisation' ),
        'add_new_item'      => __( 'Add New Organisation' ),
        'new_item_name'     => __( 'New Organisation Name' ),
        'menu_name'         => __( 'Organisations' ),
      );
    
      $args = array(
        'hierarchical'      => false, // set to false for a non-hierarchical taxonomy
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'organisation' ), // change the slug to your preferred URL format
        'show_in_rest'      => true, // enable REST API support
      );
    
      register_taxonomy( 'organisation', array( 'post' ), $args );
    }
    add_action( 'init', 'db_custom_taxonomy' );
    

    Once thats added it will register the new tax to your single posts.
    And we can use dynamic data options to display those terms.

  • Wow cool.. This is exactly what I wanted. Many thanks David.

  • You’re welcome

  • David, sorry for bothering you again…

    I would like to make a kind of an author bio box for the organizations which I would like to link dynamically to the post.

    So my idea was to use ACF to make two Additional fields for the Organisation tax: Full Name and Homepage. This worked, when I work on the organizations I can add the relevant content for each Organisation.

    I want then to add a container into the Single Post Element which should pull in the Full Name content into a headline block, the Homepage as the link into a button and the Description (standard available in organization) into a headline (p) block.

    However: I can’t find the fields. Does that not work???

    Many thanks for your help.

    best regards
    Klaus

  • Ok, to get a taxonomy custom fields on a single post today, requires PHP today, its something we will look to add in GB Pro in the future.

    For the Name and Description add this PHP Snippet:

    
    add_filter( 'render_block', function( $block_content, $block ) {
    
        if ( 
            ! empty( $block['attrs']['className'] ) && 
            strpos($block['attrs']['className'], 'organisation-field') !== false
        ) {
    
            // get the organisation terms
            $terms = get_the_terms( get_the_ID(), 'organisation' );
            // there are terms get the fields
            if ( $terms && ! is_wp_error( $terms ) ) {
                $term = $terms[0];
                $name = get_field( 'name', $term );
                $description = term_description( $term );
            }
            if (
                strpos($block['attrs']['className'], 'org-name') !== false
            ) {
                $block_content = str_replace( 'org-name', $name , $block_content );
            }
            if (
                strpos($block['attrs']['className'], 'org-description') !== false
            ) {
                $block_content = str_replace( 'org-description', $description , $block_content );
            }
        }
        return $block_content;
    }, 10, 2 );
    

    NOTEs:

    1. here is where we you set the custom field name for the orgnanisation name:

    $name = get_field( ‘name’, $term );

    2. the select block to show the name and give it the Advanced > CSS classes of:

    organisation-field org-name

    3. the select block to show the description and give it CSS classes of:

    organisation-field org-description

    For the Button URL add this PHP Snippet

    
    add_filter('generateblocks_dynamic_url_output', function($url, $attributes) {
        if (
            !empty($attributes['className']) && 
            strpos($attributes['className'], 'org-home') !== false
        ) {
            $terms = get_the_terms( get_the_ID(), 'organisation' );
            // there are terms get the fields
            if ( $terms && ! is_wp_error( $terms ) ) {
                $term = $terms[0];
                $homepage = get_field( 'homepage', $term );
            }
            if ($homepage) {
                $url = $homepage;
            }
        }
        return $url;
    }, 10, 2);
    

    Notes:

    1. $homepage = get_field( 'homepage', $term ); is where you set the homepage url custom field name.
    2. the button should have CSS Classes of: org-home

  • David, I made a mistake or it doesn’t work.

    This is what I did in ACF (just for your information so that you know all the names etc.). I made:
    – fieldname ‘komplettname’ holds the full name of the organization and is a text field
    – fieldname ‘homepage’ hold the url to the homepage and is a text field.

    In the line $name = get_field( ‘name’, $term ); I exchanged the expression ‘name’ by ‘komplettname’ nothing else.

    In my

      Element

    for the Single Post Content I added three blocks:
    1. one headline H2 with the additional css classes ‘organisation-field org-name’
    2. one headline p with the additional css classes ‘organisation-field org-description’
    3. one button block with the addition css class ‘org-home’

    But: what do I put inside these 3 blocks? I can’t access any dynamic data which look like homepage link, description or full name. If I leave them blank the headline blocks just don’t show up at all and the button block has no link inside.

    Many thanks
    Klaus

  • You don’t add any dynamic data to the blocks.
    Just place some static text in the headline blocks,
    And for the Buttons add a label and in the link add a #

    Oh, and in both those snippets see this:

    $terms = get_the_terms( get_the_ID(), 'organisation' );

    The organisation is the name of the custom taxonomy – make sure that is correct

  • David, I am very sorry but I can’t get this to work….

    I have now set up the whole thing in my development area. You find the blog post here: https://dev.beckmann-web.com/2023/05/03/testbeitrag/

    At the bottom of the post there are the three blocks for the Fullname, the description and the button for the homepage link. As you see, it is only the dummy text displayed which I put into the blocks when I made the post.

    I have installed ACF and created

    1. a new Taxonomy
    -plural label: Organisationen
    -singular label: Organisation
    -taxonomy key: organisation
    -post type: post

    2. in fields I created a new field group with the name Organisationen inside this field group are two fields:
    -field type text
    -field label: Name and Homepage
    -field name komplettname and homepage
    -For both field the display rules are “show field group” if Taxonomie is equal to Organisation

    I have put the last code you sent me into the function.php of my child theme and have tried all kind of combinations for the ‘name’ (organisation, Organisation, Organisationen) and also the field names but I can’t get any of them to display the Name, Description and Homepagelink._

    Any idea? As I mentioned that I have no knowledge of PHP. Nevertheless I stumbeld over the fact, that in the add_filter(‘render block’…) code I couldn’t identify and reference to the additional CSS classes whereas in the add_filter(generateblocks_dynamic_…..) code there is.

    Any idea what could solve the problem?

    Do you need access to my backend?

    Many thanks again

    Best regards
    Klaus

  • Its me being an idiot 🙂

    Lets deal with the name and description – see here:

    https://generate.support/topic/dynamic-data/#post-6004

    I updated the first snippet below: For the Name and Description add this PHP Snippet:

    Can you try that

  • ok… we are getting closer. I know have the full name (field name: komplettname) and the description visible.

    However:
    in the html listing (see dev tools) the Organisation name is listed without any tags (“MISO e.V., Hannover”). In the original post it is set up as H2 block. The opening and closing h2 tag is not present.

    For the description each paragraph has a p tag but any styling (here: background color: orange, text color: white) I do in the post with GB is getting lost.

    See: https://dev.beckmann-web.com/2023/05/03/testbeitrag/

    Homepage link (button) still not working but I think you know that….

  • Ok. thats good – i updated that code again.

    Now in your headline blocks, edit those.
    And the one displaying the Name, in the block type: org-name
    And for the description type: org-description

    The code will replace those strings with the dynamic data.

    And then we can do the button 🙂

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