Search code examples
phpwordpresswordpress-themingcustom-wordpress-pageswordpress-rest-api

Facing Error in WordPress Custom Post


I'm facing issue in custom post type I write code of wordpress custom post type everything working fine but I want to make global code to call everywhere. But when I'm trying to make 2 different files to make code global then showing error "500 error"

Here is code

    $custom_terms = get_field('portfolio_gallery_category_name');
print_r(get_field('portfolio_gallery_category_name'));

foreach($custom_terms as $custom_term):
    wp_reset_query();
    $args = array('post_type' => 'portfolio',
        'tax_query' => array(
            array(
                'taxonomy' => 'portfolio_type',
                'field' => 'term_id',
                'terms' => $custom_term->term_id,
            ),
        ),
     );
$loop = new WP_Query($args);
include('templates-sections/portfolios.php');

and the other remaining code in other global file name is templates-sections/portfolios.php

<?php

error_reporting(E_ALL);

 if($loop->have_posts()): ?>

    <?php while($loop->have_posts()) : $loop->the_post();
            ?>
            <div class="col-lg-3 col-md-4 col-sm-6 p-0 scale-anm" data-aos="fade-zoom">
            <a href="<?php the_post_thumbnail_url('full'); ?>" data-toggle="lightbox" data-gallery="example-gallery">
            <img src="<?php the_post_thumbnail_url('full'); ?>" class="img-responsive" /></a>
        </div>
      <?php endwhile; ?>
     <?php endif; ?>
<? endforeach; ?>

I've on all errors reporting but same thing showing "500" error can anyone help to resolve this issue.

I know lots of peoples have solutions and someone resolve this minor issue.

Thanks in advance.


Solution

  • You can create a function in your second file and call it in your first file.

    <?php
    
    $custom_terms = get_field('portfolio_gallery_category_name');
    print_r(get_field('portfolio_gallery_category_name'));
    
    include('templates-sections/portfolios.php');
    
    foreach($custom_terms as $custom_term):
        wp_reset_query();
        $args = array('post_type' => 'portfolio',
            'tax_query' => array(
                array(
                    'taxonomy' => 'portfolio_type',
                    'field' => 'term_id',
                    'terms' => $custom_term->term_id,
                ),
            ),
        );
    
    $loop = new WP_Query($args);
    
    myFunction( $loop );
    
    endforeach;
    
    ?>
    

    Your second file

    <?php
    
    error_reporting(E_ALL);
    
    function myFunction( $loop ) {
        if($loop->have_posts()): 
    
            while($loop->have_posts()) : $loop->the_post();
               ?>
               <div class="col-lg-3 col-md-4 col-sm-6 p-0 scale-anm" data-aos="fade-zoom">
                  <a href="<?php the_post_thumbnail_url('full'); ?>" data-toggle="lightbox" data-gallery="example-gallery">
                     <img src="<?php the_post_thumbnail_url('full'); ?>" class="img-responsive" /></a>
                 </div>
             <?php endwhile;
         endif;
    
     }
    
    
     ?>