• Resolved mr-graphiste

    (@mr-graphiste)


    Hello, i created a custom post type (“presse” with post_tag taxonomy.
    I added a custom field (“url_img”) to the taxonomy to add a featured image url.

    I’m looking for a way to display this value on a Block Loop Request.
    Thanks in advance for your reply.

    • This topic was modified 1 year, 1 month ago by mr-graphiste.
Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Phi Phan

    (@mr2p)

    Hello @mr-graphiste, Could you please explain a bit more about your request? Do you want to display the value of the url_img field inside a Query Loop of the presse post type? And is the custom field from the post_tag taxonomy? Am I correct? I need to understand it to help you properly.

    Phi.

    Thread Starter mr-graphiste

    (@mr-graphiste)

    Hi mister,

    Thanks for your quick reply.

    Yes this is exactly what i’m trying to achieve.

    Plugin Author Phi Phan

    (@mr2p)

    @mr-graphiste I have some more questions, What happen if there are more than one post_tag on a presse item? You will display a list of images?

    Thread Starter mr-graphiste

    (@mr-graphiste)

    hello

    there will be only one post_tag

    Plugin Author Phi Phan

    (@mr2p)

    @mr-graphiste Here is your snippet:

    // Create a function to build the value for the field.
    function yourprefix_get_featured_image_from_post_tag( $term_id ) {
      $field_value = get_term_meta( $term_id, 'url_img', true );
    
      // If the meta field is an ACF Field. The code will be:
      // $field_value = get_field( 'url_img', 'term_' . $term_id );
    
      if ( ! $field_value ) {
        return '';
      }
    
      $output = sprintf( '<img src="%1$s" alt="Featured image"/>', esc_url( $field_value ) );
    
      return $output;
    }
    
    // Render the block on the front end.
    add_filter( 'meta_field_block_get_block_content', function ( $block_content, $attributes, $block, $post_id ) {
      $field_name = $attributes['fieldName'] ?? '';
    
      // Replace url_img with your unique name.
      if ( 'url_img' === $field_name && is_singular( 'presse' ) ) {
        $terms = get_the_terms( $post_id, 'post_tag' );
        if ( $terms && count( $terms ) > 0 ) {
          $block_content = yourprefix_get_featured_image_from_post_tag( $terms[0] );
        }
      }
    
      return $block_content;
    }, 10, 4);
    Thread Starter mr-graphiste

    (@mr-graphiste)

    Hi,

    Thanks for your help, i don’t know why it doesn’t works…

    I’m using your plugin for custom metabox there is no problem.

    Do you know what could be the problem ?

    // Bouton Source URL
    add_filter( ‘meta_field_block_get_block_content’, function ( $block_content, $attributes, $block, $post_id ) {
    $field_name = $attributes[‘fieldName’] ?? ”;

    // Replace your_field_name with your unique name.
    if ( ‘_url’ === $field_name ) {
    $block_content = ‘Lire l\’article complet sur le site‘;
    }

    return $block_content;
    }, 10, 4);

    Plugin Author Phi Phan

    (@mr2p)

    Hi @mr-graphiste, I could not see the code, can you input the code in a code block?

    Thread Starter mr-graphiste

    (@mr-graphiste)

    Hi,

    It doesn’t matter, it’s an example of another meta_block_field used in the same block loop.
    I will try creating a custom taxonomy categorie instead of post_tag.
    Could you please help me for updating the snippet.
    Many thanks for your help and patience.

    Plugin Author Phi Phan

    (@mr2p)

    @mr-graphiste You can use my above snippet, you only have to replace post_tag with your new custom taxonomy name.

    Thread Starter mr-graphiste

    (@mr-graphiste)

    This is very strange, i very don’t know what’s wrong with your code…

    Do you have any idea ?

    Plugin Author Phi Phan

    (@mr2p)

    @mr-graphiste, I need to see your site and your code to know what the issue is. I may have misunderstood your request.

    Thread Starter mr-graphiste

    (@mr-graphiste)

    Hi, no no this is exactly what i need to achieve…
    Can i transmit my website details in a private message ?

    Plugin Author Phi Phan

    (@mr2p)

    @mr-graphiste Sure you can send me directly via hello @boldblocks.net. Please remove the space before the @ character.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Get Term Meta for Custom Taxonomy’ is closed to new replies.