Aihsan Majeed
Aihsan Majeed

Reputation: 135

Custom post type page shortcode not working

Here is the code from my template archive-gallery.php

<?php
/*
Template Name: Gallery
*/
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'bm_custom_loop' );
function bm_custom_loop() {
     $gallery = new WP_Query( array( 'post_type' => 'gallery','posts_per_page' => 7) );
     if ( $gallery -> have_posts() ){
?>
<h1> Behind the scene photo gallery </h1>
<br>
<?php
         while ( $gallery -> have_posts() ){
             $gallery -> the_post();

?>
    <?php $images = get_field('image');
        if( $images ): ?>
            <?php foreach( $images as $image ):?>
                <div id="box"> <img class="galimg" src="<?php echo $image['url']; ?>">
                    <a href="<?php echo $image['url']; ?>"><div id="over">
                        <span id="plus"><i class="fa  fa-2x fa-camera"></i></span>
                    </div></a>
                </div>
        <?php endforeach; ?>
    <?php endif; ?><?php

        } // End IF
     } // End While
} //End Loop
add_shortcode( 'behind', 'bm_custom_loop' ); 
genesis();

and I use this shortcode [behind] to call the shortcode

The page template works fine when selected using page attributes, but when using the shortcode, the outcome is the shortcode [behind]

Upvotes: 0

Views: 1146

Answers (1)

Ajith
Ajith

Reputation: 639

write the following code in your theme's function.php file.

function bm_custom_loop() {
 $gallery = new WP_Query( array( 'post_type' => 'gallery','posts_per_page' => 7) );
 if ( $gallery -> have_posts() ){
?>
<h1> Behind the scene photo gallery </h1>
<br>
<?php
     while ( $gallery -> have_posts() ){
         $gallery -> the_post();
?>
<?php $images = get_field('image');
    if( $images ): ?>
        <?php foreach( $images as $image ):?>
            <div id="box"> <img class="galimg" src="<?php echo $image['url']; ?>">
                <a href="<?php echo $image['url']; ?>"><div id="over">
                    <span id="plus"><i class="fa  fa-2x fa-camera"></i></span>
                </div></a>
            </div>
    <?php endforeach; ?>
<?php endif; ?>
<?php

    } // End IF
 } // End While
} //End Loop
add_shortcode( 'behind', 'bm_custom_loop' );

and call the shortcode in your archive-gallery.php like bellow.

echo do_shortcode( '[behind]' );

Upvotes: 1

Related Questions