• Resolved Rhand

    (@rhand)


    Hi there everyone! Have not used Genesis for years, but I am back and I was working with Genesis Pro Custom Blocks and a CPT following this Studio Press’s blog post . Made:

    <?php
    /**
     * Events CPT Block
     *
    **/
    // Create id attribute allowing for custom "anchor" value.
    // $id = 'pm-' . $block['id'];
    // if( !empty($block['anchor']) ) {
    // 	$id = $block['anchor'];
    // }
    
    // Create class attribute allowing for custom "className" and "align" values.
    $className = 'block-events ';
    if( !empty($block['className']) ) {
    	$className .= ' ' . $block['className'];
    }
    if( !empty($block['align']) ) {
    	$className .= ' align' . $block['align'];
    }
    
    // Variables
    $number_of_events = block_value( 'number-of-events' );
    $category = block_value( 'event-category' );
    
    // The Query
    $the_query = new WP_Query( 
        array( 
            'post_type' => 'event',
            'posts_per_page' => $number_of_events,
            'category_name' => $category->name
            ) 
    );
    
    // The Loop
    if ($the_query->have_posts()) {
        echo '<div class="dh-events alignwide">';
        while ($the_query->have_posts()) {
            $the_query->the_post();
            
            // Variables
            $categories = get_the_category();
            $first_cat = $categories[0]->name;
            $first_cat_url = get_category_link( $categories[0]->term_id );
            ?>
    
            <div class="dh-events__tile">
                <div class="dh-game-event__image" style="background-image: url('<?php echo get_the_post_thumbnail_url(); ?>');">
                    <a class="dh-event__cat" href="<?php echo $first_cat_url ?>"><?php echo $first_cat; ?></a>
                </div>
                <div class="dh-event__tile-content">
                    <h4><?php the_title(); ?></h4>
                    <?php the_excerpt(); ?>
                    <div class="dh-event__event-author">
                        <img class="dh-event__avatar" src="<?php echo get_avatar_url( get_the_author_meta( 'ID' )); ?>" alt="">
                        <span><?php the_author(); ?></span>
                    </div>
                    <a class="dh-event__button" href="<?php the_permalink(); ?>">Read Event</a>
                </div>
            </div>
            <?php
        }
        echo '</div>';
    } else {
        // no posts found
    }
    /* Restore original Post Data */
    wp_reset_postdata();
    ?>

    so far but having

    Warning: Undefined property: WP_Error::$name in /Users/jasper/code/site-in-progress/wp-content/themes/theme-in-progress/blocks/dynamic-events/block.php on line 30
    
    Warning: Undefined array key 0 in /Users/jasper/code/site-in-progress/wp-content/themes/theme-in-progress/blocks/dynamic-events/block.php on line 42

    Any ideas why the category name is not being found? I selected a category for the block in the editor.. here the json:

    {
      "genesis-custom-blocks\/dynamic-events": {
        "name": "dynamic-events",
        "title": "Dynamic Events",
        "excluded": [
          "post",
          "cheat-sheets",
          "sponsors",
          "event"
        ],
        "icon": "notifications",
        "category": {
          "icon": null,
          "slug": "theme",
          "title": "Theme"
        },
        "keywords": [],
        "fields": {
          "number-of-events": {
            "location": "editor",
            "width": "100",
            "help": "",
            "min": 0,
            "max": 100,
            "step": 1,
            "default": "",
            "name": "number-of-events",
            "label": "Number of Events",
            "order": 0,
            "control": "range",
            "type": "integer"
          },
          "event-category": {
            "location": "editor",
            "width": "100",
            "help": "",
            "post_type_rest_slug": "event_category",
            "name": "event-category",
            "label": "Event Category",
            "order": 1,
            "control": "taxonomy",
            "type": "object"
          }
        }
      }
    }

    and then used the code as shown in block.php Anyone seeing what I am missing?

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Rhand

    (@rhand)

    Printing the variable print_r($category); shows me

    
    WP_Error Object ( [errors] => Array ( [invalid_term] => Array ( [0] => Empty Term. ) ) [error_data] => Array ( ) [additional_data:protected] => Array ( ) ) 
    

    and when I select the category from the dropdown instead of typing it in the editor block field I see this.

    
    WP_Term Object ( [term_id] => 179 [name] => Virtual [slug] => virtual [term_group] => 0 [term_taxonomy_id] => 179 [taxonomy] => event_category [description] => [parent] => 0 [count] => 1 [filter] => raw [meta] => Array ( ) )
    

    Not sure yet why.But does look like the selection of a category in the generated block does not stick somehow.

    And on clicking field I see this JS error

    TypeError: null is not an object (evaluating 'e.addEventListener')
    Global Code — toggle.js:7
    Selected Element
    <div class="block-editor-block-list__layout is-root-container" data-is-drop-zone="true">…</div>
    • This reply was modified 4 years, 7 months ago by Rhand.
    • This reply was modified 4 years, 7 months ago by Rhand.
    • This reply was modified 4 years, 7 months ago by Rhand.
    • This reply was modified 4 years, 7 months ago by Rhand.
    • This reply was modified 4 years, 7 months ago by Rhand.
    Thread Starter Rhand

    (@rhand)

    When I typed the category and saved instead of using dropdown as I mentioned earlier I do not see anything loading but do get this printing the variable

    WP_Term Object ( [term_id] => 180 [name] => OnSite [slug] => onsite [term_group] => 0 [term_taxonomy_id] => 180 [taxonomy] => event_category [description] => [parent] => 0 [count] => 1 [filter] => raw [meta] => Array ( ) )

    So choosing a term from dropdown I get to load stuff based on Custom Post Type despite empty term and errors. When I type the term it does get grabbed but nothing of the custom post type loads.

    Testing in Chrome I could dropdown select with more ease compared to Safari, but nothing loads still and I had this JS Error

    Uncaught TypeError: Cannot read properties of null (reading 'addEventListener')

    on selecting from dropdown

    • This reply was modified 4 years, 7 months ago by Rhand.
    Thread Starter Rhand

    (@rhand)

    Using “just”

    // Variables
    $number_of_events = block_value( 'number-of-events' );
    $taxonomy = block_value( 'event-category' );
    $terms = get_terms( $taxonomy );
    // print_r($taxonomy);
    // print_r($terms);
    
    // The Loop
    foreach ($terms as $term ) {
        $term_link = get_term_link( $term );
    
        if ( is_wp_error( $term_link ) ) { // just in case we encounter an error
            continue;
        }
    
        echo 'View more: <a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>';
    }

    does load without errors. And shows the term / category selected. Just now need to load parts like other terms like event title, date and such. And… still wondering why before I had all these errors… Well come to think of it.. perhaps cause I am using a Custom Post Type here..?

    • This reply was modified 4 years, 7 months ago by Rhand.
    • This reply was modified 4 years, 7 months ago by Rhand.
    Thread Starter Rhand

    (@rhand)

    Using block_field instead of block_value I do get event category name Virtual

    $event_category = block_field( 'event-category' );
    print_r($event_category);

    However, in the loop I get all posts whatever the event category:

    $args = array(
        'post_type' => 'event',
        'posts_per_page' => $number_of_events,
        'category_name' => $event_category
    );
    $the_query = new WP_Query( $args ); ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
        <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
            <h2><?php the_title(); ?></h2>
        <?php endwhile; ?>
    
        <?php wp_reset_postdata(); ?>
    
    <?php endif; ?>
    Thread Starter Rhand

    (@rhand)

    Using

    // Variables
    $number_of_events = block_value( 'number-of-events' );
    // $event_category = block_field( 'event-category' );
    $event_terms_object = block_value( 'event-category' );
    // print_r($event_terms_object);
    // WP_Term Object ( [term_id] => 179 [name] => Virtual [slug] => virtual [term_group] => 0 [term_taxonomy_id] => 179 [taxonomy] => event_category 
    // [description] => [parent] => 0 [count] => 2 [filter] => raw [meta] => Array ( ) ) Virtual
    
    $event_category_name = $event_terms_object->name;
    // print_r($event_category_name);
    // Virtual
    // $terms = get_terms(array(
    //     'taxonomy' => 'event_category',
    //     'hide_empty' => false,
    // ));
    // $slugs = wp_list_pluck( $terms, 'slug' ); 
    // $names = wp_list_pluck( $terms, 'name' );
    // print_r($names);
    
    $args = array(
        'post_type' => 'event',
        'posts_per_page' => $number_of_events,
        'category_name' => $event_category_name
    );

    I do see Virtual for $event_category_name but it is not used as a filter for the new query args… Nothing loads with the added event category name variable.

    • This reply was modified 4 years, 7 months ago by Rhand.
    • This reply was modified 4 years, 7 months ago by Rhand.
    Thread Starter Rhand

    (@rhand)

    In the end I needed event_category instead of category_name. This should work here with Genesis Custom Block or an ACF Block. In the process of doing some final tweaking so will update here again later on.

    Plugin Contributor Ryan Kienstra

    (@ryankienstra)

    Hi @rhand,
    Sorry for not responding to you. It sounds like you mainly figured it out.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Issue loading CPT Category Name’ is closed to new replies.