0

I've read an article on how to implement a custom Gallery template in wordpress posts or pages. I have a simple question, is it possible to not show the attached images inside the post content but only inside my custom bootstrap carousel that I've coded with the gallery template?

here is the code I'm using (I'm not experienced with wordpress because usually I use laravel or plain php for my projects).

What I want to do is to show the attached media only inside the carousel, with my code for now the images are loaded also inside the post and this is not what I expect. Another question is about the bootstrap navwalker. I've a parent page that has four child pages, but I'm only able to click on the page name inside the menu to show the dropdown with the child pages. How i can fix this? Thanks for your help

<?php
/*
Template Name: Gallery
 */
get_header();
?>

<div class="container">
  <div class="row justify-content-center">
<?php if( have_posts() ): while( have_posts() ): the_post(); ?>

<?php
  $args = array(
      'numberposts' => -1, // Using -1 loads all posts
      'orderby' => 'menu_order', // This ensures images are in the order set in the page media manager
      'order'=> 'ASC',
      'post_mime_type' => 'image', // Make sure it doesn't pull other resources, like videos
      'post_parent' => $post->ID, // Important part - ensures the associated images are loaded
      'post_status' => null,
      'post_type' => 'attachment'
  );
  $images = get_children( $args );
  $count = 0;
?>
    <div class="col-sm-12 col-md-8 col-lg-8 text-justify">
      <h2 class=""><?php the_title(); ?></h2>
<?php the_content(); ?>
    </div>
<?php if($images): ?>
      <div class="col-sm-12 col-md-12 col-lg-12" id="">
        <div class="carousel slide" data-ride="carousel">
          <div class="carousel-inner" id="">
<?php foreach($images as $image): ?>
            <div class="carousel-item <?php echo ($count == 0) ? 'active' : ''; ?>">
              <img class="img-responsive d-block w-100" src="<?php echo $image->guid; ?>" alt="<?php echo $image->post_title; ?>" title="<?php echo $image->post_title; ?>" />
            </div>
<?php $count++; ?>
<?php endforeach; ?>
          </div>
        </div>
      </div>
<?php endif; ?>

<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
  </div>
</div>

<?php get_footer(); ?>

What I want to do is to show the attached media only inside the carousel, with my code for now the images are loaded also inside the post and this is not what I expect. Another question is about the bootstrap navwalker. I've a parent page that has four child pages, but I'm only able to click on the page name inside the menu to show the dropdown with the child pages. How i can fix this? Thanks for your help

1

My first recommendation would be to use Advanced Custom Fields to setup a gallery rather than using this method.

However, I think you may be able to load your images using Add Media -> Create Gallery which will add a shortcode to your post content

[gallery ids="56,57,58"]

You could then use the_content filter and strip_shortcodes to remove this shortcode from the_content(); on your template. Hopefully you can then use get_post_gallery() or get_post_gallery_images() to retrieve the images you need for your carousel.

But try Advanced Custom Fields - it's much easier!

| improve this answer | |
  • thanks, i will take a look to get_post_gallery_images() reference. I need the url. Thanks a lot for the suggestion of ACF, I know that plugin. Good. – ZWPDev Feb 10 '19 at 18:51
  • That function should give you the image URLs. ACF is fantastic and we use it on most sites that we build. If I wanted to do something similar to your question, that's what I'd use. Good luck. – Alexander Holsgrove Feb 10 '19 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.