Displaying the custom post type value

Asked

Viewed 35 times

0

I created a "custom post type", it’s saving right in the post only I’m not able to pull the value to the post when I do the loop repeat.

Below the code:

  /*********************************************************************/
 /************************* CAMPO NOVO -LINK DA DIRETRIZ- (taxonomy) **/
/*********************************************************************/

//adicionando a meta box
add_action( 'add_meta_boxes', 'diretriz_meta_box_add' ); 

// Criando a função para o "diretriz_meta_box_add" que foi adicionado na linha acima
function diretriz_meta_box_add() {
  //Meta box que vai aparecer no POST
  add_meta_box( 'link-da-diretriz', 'Link da Diretriz', 'diretriz_meta_box', 'post', 'normal', 'high' ); 
}
//Adicionando os campos dentro do meta-box
function diretriz_meta_box(){
      global $post;
      $link_diret = get_post_meta($post->ID, 'link_diretrizes', true); 
?>  
  <label for="ValorDir">Link</label>
  <input type="text" name="link_diretrizes" id="ValorDir" value="<?php echo $link_diret; ?>" style="display:block; width: 100% !important; position: relative;" />
<?php
    }
//Salvando o dado que irá ser inserido dentro do campo
    add_action('save_post', 'dir_save');

    function dir_save(){
        global $post;        
          update_post_meta($post->ID, 'link_diretrizes', $_POST['link_diretrizes']);
    }



  /*********************************************************************/
 /************************* CAMPO NOVO -Árvore Decisória- (taxonomy) **/
/*********************************************************************/

//adicionando a meta box
add_action( 'add_meta_boxes', 'Tree_desc_box_add' ); 

// Criando a função para o "diretriz_meta_box_add" que foi adicionado na linha acima
function Tree_desc_box_add() {
  //Meta box que vai aparecer no POST
  add_meta_box( 'link-da-tree', 'Árvore Decisória', 'tree_meta_box', 'post', 'side', 'high' ); 
}
//Adicionando os campos dentro do meta-box
function tree_meta_box(){
      global $post;

    $img_tree = get_post_meta($post->ID, 'upload_image_id', true); 
?>  


  <label for="arvore_decisoria" style="display:block">Árvore Decisória</label>
  <img id="arvore_decisoria" src="<?php echo $image_src ?>" style="max-width:100%;"  />
    <input type="text" name="upload_image_id" id="upload_image_id" value="<?php echo $img_tree; ?>" style="display:block; width: 100% !important; position: relative;"><?php echo $image_src; ?></input>
    <p>
      <a title="<?php esc_attr_e( 'Definir a imagem(Árvore Decisória)' ) ?>" href="#" id="definir-arvore" class="button"><?php _e( 'Definir a imagem(Árvore Decisória)' ) ?></a>
      <a title="<?php esc_attr_e( 'Remover a imagem' ) ?>" href="#" id="remover-arvore" class="button" style="<?php echo ( ! $image_id ? 'display:none;' : '' ); ?>"><?php _e( 'Remover a imagem' ) ?></a>
    </p>

    <script type="text/javascript">
    jQuery(document).ready(function($) {

      // save the send_to_editor handler function
      window.send_to_editor_default = window.send_to_editor;

      $('#definir-arvore').click(function(){

        // replace the default send_to_editor handler function with our own
        window.send_to_editor = window.attach_image;
        tb_show('', 'media-upload.php?post_id=<?php echo $post->ID ?>&amp;type=image&amp;TB_iframe=true');

        return false;
      });

      $('#remover-arvore').click(function() {

        $('#upload_image_id').val('');
        $('img').attr('src', '');
        $(this).hide();

        return false;
      });

      // handler function which is invoked after the user selects an image from the gallery popup.
      // this function displays the image and sets the id so it can be persisted to the post meta
      window.attach_image = function(html) {

        // turn the returned image html into a hidden image element so we can easily pull the relevant attributes we need
        $('body').append('<div id="temp_image">' + html + '</div>');

        var img = $('#temp_image').find('img');

        imgurl   = img.attr('src');
        imgclass = img.attr('class');
        imgid    = parseInt(imgclass.replace(/\D/g, ''), 10);

        $('#upload_image_id').val(imgurl);
        $('#remover-arvore').show();

        $('img#arvore_decisoria').attr('src', imgurl);
        try{tb_remove();}catch(e){};
        $('#temp_image').remove();

        // restore the send_to_editor handler function
        window.send_to_editor = window.send_to_editor_default;

      }

    });
    </script>
</p>
<?php
}

//Salvando o dado que irá ser inserido dentro do campo
    add_action('save_post', 'tree_save');

    function tree_save(){
        global $post;        
          update_post_meta($post->ID, 'upload_image_id', $_POST['upload_image_id']);
    }

The code above made appear the boxes as I wanted in the post edition, so far beauty! Now comes the problem, I can’t redeem the saved values. I am using the following code in my loop:

<?php
    /* LOOP DE TODOS */
    if ( have_posts() ) : if ( is_home() && ! is_front_page() ) : 
      endif;
    /* Start the Loop */
      while ( have_posts() ) : the_post();

       echo get_post_meta($post->ID, 'diretriz_meta_box', true);
       echo get_post_meta($post->ID, 'tree_meta_box', true);

    endwhile;
    endif; ?>
    <?php wp_reset_query(); //Resetando a busca ?>
  • Where are you doing this loop? In a template created specifically for your post type?

  • I don’t think you noticed, but you posted two identical questions. But as I quoted there it seems you are calling the values using the callback name and not the metaboxes identifier. Try so: get_post_meta($post->ID, 'link_diretrizes', true); get_post_meta($post->ID, 'upload_image_id', true);

  • @Caiofelipepera man I’m making the loop in a custom index of my theme.

  • @Noctuam Cara is just that! I was calling wrong... and on the duplicity I found that my post n had gone rsrs was malz... Man may reply that I score your answer!

  • And how do I delete the duplicate question?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.