Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
Get attachment caption outside of Wordpress loop

On my wordpress page, I have the following loop, which successfully outputs an attachment's caption.

<?php 
$the_query = new WP_Query(array( 'post_type' => 'attachment')); 
while ( $the_query->have_posts() ) :  $the_query->the_post();

$attachment_data = wp_prepare_attachment_for_js();
echo '<h2>'.$attachment_data['caption'].'</h2>'
;?>

Now, I also need to call the attachment caption outside of the loop. This is what I'm trying in functions.php:

function custom_info()
     {  
         global $wp_query;
         $query_id = $wp_query->post->ID;
         $attachment_data_query = wp_prepare_attachment_for_js( $query_id );

         wp_register_script( 'custom_data_info');
         $info = array(
             'data_query'   => $attachment_data_query['caption']
         );
         wp_enqueue_script( 'custom_data_info' );
         wp_localize_script('custom_data_info', 'info', $info);
    }
     if ( !is_admin() ) add_action( "wp_enqueue_scripts", "custom_info", 10 );

And then it would be called in the external javascript file like so:

return info.data_query

I know the custom_info function and javascript file are talking with each other correctly. The problem is with how I am defining $query_id

EDIT: Almost fixed. The problem was that I didn't include a foreach statement, and so the first item in the loop was being repeated over and over again.

I've adjusted below, but for some reason, now the array is empty upon output of info.data_query...

function custom_info(){  
     global $wp_query;
     wp_register_script( 'custom_data_info');

     $info = array('data_query' => array());

     foreach ( $wp_query->posts as $query_id) {
        $attachment_data_query = wp_prepare_attachment_for_js( $query_id );
        $info['data_query'][$query_id] = $attachment_data_query['caption'];

      }
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Are you sure $wp_query will return the correct post ID? In my experience, it's better to use global $post instead. Pass that into wp_prepare_attachment_js() instead:

global $post;
$post_id = $post->ID;
$attachment_data_query = wp_prepare_attachment_for_js( $post_id );
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda