neosan
neosan

Reputation: 366

Display my block in a conditional-logic way

I use this plugin to filter my searches.

in this section they explain how to create my own custom block.

function prefix_register_block( $blocks ) {
    
    // 'my_block' corresponds to the block slug.
    $blocks['my_block'] = [
        'name' => __( 'My Block', 'text-domain' ),
        'render_callback' => 'prefix_my_block_render',
    ];

    return $blocks;
    
}

add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );

// The render callback function allows to output content in cards.
function prefix_my_block_render() {

    // Get current post, term, or user object.
    $post = wpgb_get_post();

    // Output the post title.
    echo '<h3>' . esc_html( $post->post_title ) . '</h3>';

}

the code looks simple, so it's a matter of common sense and a php expert can figure it out quickly.

I would like to add a line to say if slug or term "office" exist in the property-type taxnonomy

then Output the post title

(which I will of course edit to display custom fields)

but how to make conditional logic possible?

thank you for your patience, I do my best to understand the correct technique

Upvotes: 0

Views: 230

Answers (1)

Kinglish
Kinglish

Reputation: 23654

I'm not a WP guru, but I think it should be this simple. Set up an array of slugs you want to affect in this way and then iterate through them in a foreach loop

function prefix_register_block( $blocks ) {
    $slugs = ['office','somethingelse'];
    foreach($slugs as $slug) {
      $blocks[$slug] = [
        'name' => __( ucwords($slug), 'text-domain' ),
        'render_callback' => 'prefix_my_block_render',
      ];
    }
    return $blocks;
}

add_filter( 'wp_grid_builder/blocks', 'prefix_register_block', 10, 1 );

// The render callback function allows to output content in cards.
function prefix_my_block_render() {
    // Get current post, term, or user object.
    $post = wpgb_get_post();
    // Output the post title.
    echo '<h3>' . esc_html( $post->post_title ) . '</h3>';
}

Upvotes: 1

Related Questions