• I’d like to show in each post, after the description, in a single line, the following elements: year: taxonomy_selection (taxonomy_selection will be an year, such as 1997, the year I choose in the taxonomy selector before editing the post). All this by code. Of course I have already created a custom taxonomy

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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter sacconi

    (@sacconi)

    I tryed but I was not successfull. Since I have a similar function in single-post :

    <?php echo "codice: ",get_the_ID(), colormag_entry_meta(); ?>

    that is working, I tryed with the following:

    <?php
    			the_content(),echo "Anno: ",get_the_numero_anno() ;

    or with

    <?php
    			the_content(),echo "Anno: ",get_the_tax_input[numero_anno]() ;

    but it didnt work. I put here my taxonomy and its meta box:

    // CREAZIONE TASSONOMIA ANNO
    
    add_action( 'init', function() {
        $labels = array(
            'name'              => _x( 'Anno', 'taxonomy general name' ),
            'singular_name'     => _x( 'Anno', 'taxonomy singular name' ),
        );
        register_taxonomy( 'numero_anno', array( 'post' ), array(
            'hierarchical'      => false,
            'labels'            => $labels,
    'meta_box_cb'       => "post_categories_meta_box",
    'show_admin_column'  => true,
     'public'            => true,
            
        ) );
    } );
    
    add_action( 'admin_head', function() {
        ?>
        <style type="text/css">
            #newtaxonomy_name_parent {
                display: none;
            }
        </style>
        <?php
    });
    add_action( 'admin_init', function() {
        if( isset( $_POST['tax_input'] ) && is_array( $_POST['tax_input'] ) ) {
            $new_tax_input = array();
            foreach( $_POST['tax_input'] as $tax => $terms) {
                if( is_array( $terms ) ) {
                  $taxonomy = get_taxonomy( $tax );
                  if( !$taxonomy->hierarchical ) {
                      $terms = array_map( 'intval', array_filter( $terms ) );
                  }
                }
                $new_tax_input[$tax] = $terms;
            }
            $_POST['tax_input'] = $new_tax_input;
        }
    });
    
    /**
     * Register tax_input[numero_ospiti] field if you want it
     * to be available via WordPress REST API.
     */
    function register_tax_input_anno() {
        \register_meta(
            'post',
            'tax_input[numero_anno]',
            array(
                'object_subtype'    => 'post',
                'description'       => 'Year of the artwork',
                'single'            => true,
                'type'              => 'integer',
                'show_in_rest'      => array(
                    'schema' => array(
                        'type'  => 'integer'
                    )
                ),
                'default'           => 0,
                'sanitize_callback' => function( $value ) { return $value; },
                'auth_callback'     => function() { return '__return_true'; }
            )
        );
    }
    
    
    //creating a META BOX ANNO
    function custom_meta_box_markup( $post ) {
        $post_id = $post->ID;
    
        // tax_input[numero_anno] is an array,
        // but managed as a single value.
        $is_single_value = true;
    
        // Retrieve the term name that was saved to the database.
        $selected_term_name = get_post_meta( $post_id, 'tax_input[numero_anno]', $is_single_value );
        $terms = get_terms( array(
            'taxonomy' => 'numero_anno',
            'fields'   => 'id=>name',
            'hide_empty' => false,
            'number'   => 0
        ) );
    
        $flipped_terms = array_flip( $terms );
        if ( isset( $flipped_terms[ $selected_term_name ] ) ) {
            $selected_term_id = $flipped_terms[ $selected_term_name ];
        } else {
            $selected_term_id = 0;
        }
    
        $set_referer_field = true;
        $echo_html = false;
        $wp_nonce_html = wp_nonce_field(
            basename( __FILE__ ),
            'meta-box-nonce',
            $set_referer_field,
            $echo_html
        );
    
        $args = array(
            'show_option_none'  => ' ',
            'taxonomy'   => 'numero_anno',
            'selected'   => $selected_term_id,
            'name'       => 'tax_input[numero_anno]',
            'hide_empty' => false,
            'echo'       => false
        );
        $dropdown_cat_html = wp_dropdown_categories( $args );
    
        $html = <<<EOHTML
        <div>
            $wp_nonce_html
            <label for="meta-box-ospiti">Anno</label>
            $dropdown_cat_html
        </div>
        EOHTML;
    
        echo $html;
    }
    
    function add_custom_meta_box() {
        add_meta_box(
            'demo-meta-box',          // Unique ID.
            'Anno',                 // Meta box title.
            'custom_meta_box_markup', // Content callback, must be of type callable.
            'post',                   // Screen type.
            'side'                    // Context: where meta box should display in the screen.
        );
    }
    add_action( 'add_meta_boxes', 'add_custom_meta_box' );
    
    /**
     * Update post meta data.
     *
     * @param int $post_id Post ID of post being saved.
     */
    function save_custom_meta_box( $post_id ) {
        if (
            isset( $_POST ) &&
            isset( $_POST['tax_input'] ) &&
            isset( $_POST['tax_input']['numero_anno'] )
        ) {
            $term_id = $_POST['tax_input']['numero_anno'];
            $terms = get_terms( array(
                'taxonomy'   => 'numero_anno',
                'fields'     => 'id=>name',
                'hide_empty' => false,
                'number'     => 0
               ) );
            $selected_term_name = $terms[ $term_id  ];
    
            update_post_meta(
                $post_id,
                'tax_input[numero_anno]',
                 $selected_term_name
            );
        }
    }
    add_action( 'save_post', 'save_custom_meta_box' );
    
    
    

    thnak you in advance!

    Moderator bcworkz

    (@bcworkz)

    get_the_numero_anno() and get_the_tax_input[numero_anno]() are not core functions, we cannot evaluate them for effectiveness unless you provide their source code.

    You can get the assigned taxonomy terms for a post with wp_get_post_terms(). An array of term objects will be returned. You can foreach through each term and output its name in turn. Or use the “fields” arg to get only the term names in an array. Then you could implode() the array and output the resulting string.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to display text+ custom taxonomy in each post after the description?’ is closed to new replies.