images - Get gallery in loop through ajax

admin2025-06-03  2

So, calling this PHP from click through ajax I manage to display Title and content in my modal. But when the code gets to get_post_gallery it always returns null.

$the_query  = new WP_Query(array('p' => $pid));

if ($the_query->have_posts()) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $data = '
        <div class="post-container">
            <div id="project-content">
                <h1 class="entry-title">' . get_the_title() . '</h1>
                <div class="entry-content">' . get_the_content() . '</div>

            </div>
        </div>  
        ';

        if ( get_post_gallery( ) ) :
                $gallery = get_post_gallery(get_the_ID(), false );              
        endif;
    }
}

On a side note, the method works on a single.php page. So I assume it's a problem of context. Thanks for your help.

So, calling this PHP from click through ajax I manage to display Title and content in my modal. But when the code gets to get_post_gallery it always returns null.

$the_query  = new WP_Query(array('p' => $pid));

if ($the_query->have_posts()) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $data = '
        <div class="post-container">
            <div id="project-content">
                <h1 class="entry-title">' . get_the_title() . '</h1>
                <div class="entry-content">' . get_the_content() . '</div>

            </div>
        </div>  
        ';

        if ( get_post_gallery( ) ) :
                $gallery = get_post_gallery(get_the_ID(), false );              
        endif;
    }
}

On a side note, the method works on a single.php page. So I assume it's a problem of context. Thanks for your help.

Share Improve this question edited Jan 30, 2019 at 10:22 Pratik Patel 1,1091 gold badge11 silver badges23 bronze badges asked Jan 30, 2019 at 10:13 AntoineAntoine 12 bronze badges 1
  • You will return an array try to print r the $gallery. – Tejas Gajjar Commented Jan 30, 2019 at 10:50
Add a comment  | 

1 Answer 1

Reset to default 0

Have you tried passing $pid instead of using get_the_ID()?

if (get_post_gallery($pid)) :
   $gallery = get_post_gallery($pid, false);
endif;
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748962631a315194.html

最新回复(0)