Support

Account

Home Forums General Issues Fields from Custom Taxonomy on Single.php

Solving

Fields from Custom Taxonomy on Single.php

  • Dear folks,

    first of all, big thanks for this great plugin and all the effort you put into this!

    I use “Advanced Custom Fields” on a custom taxonomy for custom fields, let’s call them “Movies”.

    So I assign “Movies” to some blog posts, which relate to them.

    I would like to output custom fields from exactly one movie (at best randomly chosen), which of course has been assigned to the post, on a single post (single.php).

    My very basic draft is like this:

    <?php if (has_term('', 'movies')) { ?>
    
    <?php $term_id = $post_id->term_id; ?>
    
    <?php echo get_field('director', 'movies_'.$term_id); ?>
    <?php echo get_field('release', 'movies_'.$term_id); ?>
    
    <?php } ?>

    $term_id should actually be a randomly selected “movie”, which has been assigned to the post.

    My intention is to output all the “metadata” on a single post page from one (randomly chosen) assigned “movie”.

    It’d be great if you could help me with this.

    Thanks!

  • So I found at least a solution to get the fields from the “first” taxonomy.

    It’d be great if someone could tell me how to randomize this!

    <?php
    
    global $post;
    $terms = wp_get_post_terms($post->ID, 'movies', array('fields' => 'ids'));
    
    //print_r($terms);
    
    echo $terms[0];
    
    ?>
    
    <?php echo get_field('director', 'movies_'.$terms[0]); ?>
  • There is an example here using get_terms(), but it could be applied to the results of wp_get_post_terms(). https://wordpress.stackexchange.com/questions/270121/get-random-terms

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.