One block of PHP codes dont function after another block of PHP codes - Wordpress

admin2025-05-31  5

I have two separated PHP files in which some similar functions and loops of Wordpress are used. They are tested working very well.

However, one block of PHP codes in a file is used on Sidebar of the Single page, and another one is used at the bottom of the Single page of my WP website.

The first block of PHP codes does work showing custom posts for the Sidebar, but the second block at the bottom of the Single page does not show anything posts.

What are the common issues which cause one block of PHP codes not working.

Here is the first block:

    {var $terms = wp_get_post_terms( get_the_id(), 'my_customPostType', array( 'parent' => $category_id ) )} {* Get array of parent categories IDs based on the current post ID *}

    {foreach $terms as $term}
        {if ($term->term_id == 2250) or ($term->term_id == 508) or ($term->term_id == 2502) or ($term->term_id == 501)}  
            {var $parent_cat_id = $term->term_id}
            <?php break; // Exit foreach Loop if its Property or Cars to make sure if a current post has multiple categories one of which is Property or Cars, custom posts under either will be displayed ?>   
        {/if}
        {if ($term->term_id == 3272) or ($term->term_id == 3230) or ($term->term_id == 2754) or ($term->term_id == 2890) or 
            ($term->term_id == 2904) or ($term->term_id == 2914) or ($term->term_id == 2928) or ($term->term_id == 2938)   
        }
            {var $parent_cat_ids = array(2250, 508, 3272, 3230, 2754, 2890, 2904, 2914, 4150)}

            {var $parent_cat_id = $parent_cat_ids[array_rand($parent_cat_ids, 1)]}     {* Get a random parent category ID *}
        {/if}      
    {/foreach}

    {if ($parent_cat_id == 2250) or ($parent_cat_id == 2502)} {* Property: display posts from Property *}
        {includePart customposts/boxed-items-from-all-child-properties-for-rightsidebar-singlepost-4items}
    {/if}

    etc.

Here is the second block:

    <div class="elm-wrapper elm-items-wrapper">
        <div class="elm-mainheader align-center">
            <?php
                if (get_locale() == 'en_US') { 
                    $random_titles = array('Featured Properties', 'Modern Properties', 'Recently Posted Properties', 'Beautiful Properties', 'Nice Properties', 'Fantastic Properties');    // Diffierent titles
                    $random_title = $random_titles[array_rand($random_titles, 1)]; ?>
                     <h2 class="elm-maintitle"><?php echo $random_title; ?></h2> <?php 
                 }
                else { 
                    $random_titles = array('អចលនទ្រព្យស្អាតៗ', 'អចលនទ្រព្យល្អៗ', 'អចលនទ្រព្យទើបដាក់ផុស', 'អចលនទ្រព្យប្រណិតៗ', 'អចលនទ្រព្យទំនើបៗ');    // Diffierent titles
                    $random_title = $random_titles[array_rand($random_titles, 1)]; ?>
                     <h2 class="elm-maintitle"><?php echo $random_title; ?></h2> <?php 
                } ?>
      </div>
    <div class="elm-item-organizer organizer-alt layout-box elm-items">
    <div class="elm-item-organizer-container column-4 layout-box carousel-disabled"> 
      
    <?php
    //$category_id = get_queried_object();  // this wont work; without being declared, it is a default created by the theme
    $terms = get_terms( array( 'parent' => $category_id ) );    // Set this is to display all posts in any top categories with a category ID updated below. For example: Cars, Property, Services, etc.
    //$terms = get_terms( array( 'child_of' => $category_id ) );    // Set this to display all posts in any first subcategories with a subcategory ID updated below. Example: Cars for rent, Cars for sale, Property for rent, Property for sale, etc.
    //$category_id == 2252; // Set this and below to display posts from first child categories in Property for rent with ID: 2252 for EN page; no KM ID not needed
    //$terms = get_terms( array( 'child_of' => $category_id ) );  // Example: first child categories of Property for rent (2252) include Condos (2254), Building (5641), etc.
    ?>

    <?php
    define('POSTSperPAGE', 20);
    $featured_orderByDESC = false;
    $orderByRandom = true;
    $featured_orderByDefault = false;
    $featured_orderByASC = false;

    foreach ($terms as $term) {
        if ($term->term_id == 2250 || $term->term_id == 2502) {   // Parent category (Property) ID for EN page (KM page is automatically switched)

            if( $featured_orderByDefault == true ) {
                $arrayOfposts = query_posts( array(
                        'post_type'      =>  'ait-item',
                        'post_status'    =>  'publish',
                        'posts_per_page' => POSTSperPAGE,           
                        'tax_query'      => array ( array (
                            'taxonomy' => 'my_customPostType',
                            'terms'    => $term->term_id,
                                )
                        ),
                        'meta_query' => array(
                            'relation' => 'OR',
                            array(
                                'key'   => '_ait-item_item-featured',
                                'value' => 1,
                                'compare' => 'LIKE'
                            ),
                        ),
                    )
                );
            } else if( $featured_orderByASC == true ) {
                $arrayOfposts = query_posts( array(
                        'post_type'      =>  'ait-item',
                        'post_status'    =>  'publish',
                        'posts_per_page' => POSTSperPAGE,            
                        'tax_query'      => array ( array (
                            'taxonomy' => 'my_customPostType',
                            'terms'    => $term->term_id,
                                )
                        ),
                        'meta_query' => array(
                            'relation' => 'OR',
                            array(
                                'key'   => '_ait-item_item-featured',
                                'value' => 1,
                                'compare' => 'LIKE'
                            ),
                        ),
                        'order' => 'ASC',
                    )
                );
            } else if ( $orderByRandom == true) {
                $arrayOfposts = query_posts( array(
                        'post_type'      =>  'ait-item',
                        'post_status'    =>  'publish',
                        'posts_per_page' => POSTSperPAGE,            
                        'tax_query'      => array ( array (
                            'taxonomy' => 'my_customPostType',
                            'terms'    => $term->term_id,
                                )
                        ),
                        'meta_query' => array(
                            'relation' => 'OR',
                            array(
                                'key'   => '_my_customPostType_item-featured',
                                'value' => 1,
                                'compare' => 'LIKE'
                            )
                        ),
                        'orderby' => 'rand',
                    )
                );
                
            }
        }
    }
    ?>


        
    <?php
    foreach($arrayOfposts as $cpost) {        
            $p_id   = $cpost->ID;           //get_the_ID();
            $p_meta = get_post_meta($p_id);
            $a = $p_meta['_my_customPostType_item-data'][0];
            $featured = $p_meta['_my_customPostType_item-featured'][0];
            $us = unserialize($a);
            $prc = $us['subtitle'];
            $prc_type = $us['price_type'];   
     ?>  

        <div data-id="<?php static $i = 1; echo $i++; ?>" class="item item-first image-present align-center <?php echo $featured == '1' ? 'item-featured': ''; ?>">
             
           <div class="item-thumbnail">
                <a href="<?php echo get_permalink($p_id); ?>">
                    <?php 
                    if (get_the_post_thumbnail_url($p_id,'post-thumbnail')) { 
                        $imgUrl = get_the_post_thumbnail_url($p_id,'post-thumbnail');
                    } else { 
                        $imgUrl = site_url()."/wp-content/themes/businessfinder2-child/design/img/default_featured_img.jpg";
                    } ?>
                    <div class="item-thumbnail-wrap" style="background-image:url('<?php echo $imgUrl; ?>');"></div>
                    <div class="item-text-wrap">
                        <div class="item-text">
                          <div class="item-excerpt txtrows-3">
                            <?php 
                            //$content_post = get_post($p_id);
                            echo $content = $cpost->post_content;
                            /* $expt = get_the_excerpt($p_id);
                            if($expt != ''){
                                echo $expt; 
                            } else {
                                $content_post = get_post($p_id);
                                $content = $content_post->post_content;
                                $content = apply_filters('the_content', $content);
                                $content = str_replace(']]>', ']]&gt;', $content);
                                echo $content;
                             } */ ?>
                          </div>
                       </div>
                    </div>
                </a>
            </div>

            <div class="item-box-content-wrap">

                
                <div class="item-categories">

                </div>

                <div class="item-title">
                    <a href="<?php echo get_permalink($p_id); ?>">
                             <h3><?php echo $cpost->post_title; ?></h3>
                             <span class="subtitle">

                             </span>
                          </a>
                </div>

                
                <?php 
                   $serializeArr = unserialize($p_meta['_my_customPostType_item-data'][0]);
                   if($serializeArr) { 
                        if($serializeArr['map']['address']) { ?>
                            <div class="item-location">
                                <p class="txtrows-2"><?php echo $serializeArr['map']['address']; ?></p>
                            </div>
                        <?php 
                        } 
                        else { ?>
                            <div class="item-location">
                                <p class="txtrows-2">Cambodia</p>
                            </div>
                        <?php 
                        }
                    } ?>

                
            </div>
        </div>




     <?php }     ?>


        </div>
         </div>
        </div>

Many thanks in advance.

I have two separated PHP files in which some similar functions and loops of Wordpress are used. They are tested working very well.

However, one block of PHP codes in a file is used on Sidebar of the Single page, and another one is used at the bottom of the Single page of my WP website.

The first block of PHP codes does work showing custom posts for the Sidebar, but the second block at the bottom of the Single page does not show anything posts.

What are the common issues which cause one block of PHP codes not working.

Here is the first block:

    {var $terms = wp_get_post_terms( get_the_id(), 'my_customPostType', array( 'parent' => $category_id ) )} {* Get array of parent categories IDs based on the current post ID *}

    {foreach $terms as $term}
        {if ($term->term_id == 2250) or ($term->term_id == 508) or ($term->term_id == 2502) or ($term->term_id == 501)}  
            {var $parent_cat_id = $term->term_id}
            <?php break; // Exit foreach Loop if its Property or Cars to make sure if a current post has multiple categories one of which is Property or Cars, custom posts under either will be displayed ?>   
        {/if}
        {if ($term->term_id == 3272) or ($term->term_id == 3230) or ($term->term_id == 2754) or ($term->term_id == 2890) or 
            ($term->term_id == 2904) or ($term->term_id == 2914) or ($term->term_id == 2928) or ($term->term_id == 2938)   
        }
            {var $parent_cat_ids = array(2250, 508, 3272, 3230, 2754, 2890, 2904, 2914, 4150)}

            {var $parent_cat_id = $parent_cat_ids[array_rand($parent_cat_ids, 1)]}     {* Get a random parent category ID *}
        {/if}      
    {/foreach}

    {if ($parent_cat_id == 2250) or ($parent_cat_id == 2502)} {* Property: display posts from Property *}
        {includePart customposts/boxed-items-from-all-child-properties-for-rightsidebar-singlepost-4items}
    {/if}

    etc.

Here is the second block:

    <div class="elm-wrapper elm-items-wrapper">
        <div class="elm-mainheader align-center">
            <?php
                if (get_locale() == 'en_US') { 
                    $random_titles = array('Featured Properties', 'Modern Properties', 'Recently Posted Properties', 'Beautiful Properties', 'Nice Properties', 'Fantastic Properties');    // Diffierent titles
                    $random_title = $random_titles[array_rand($random_titles, 1)]; ?>
                     <h2 class="elm-maintitle"><?php echo $random_title; ?></h2> <?php 
                 }
                else { 
                    $random_titles = array('អចលនទ្រព្យស្អាតៗ', 'អចលនទ្រព្យល្អៗ', 'អចលនទ្រព្យទើបដាក់ផុស', 'អចលនទ្រព្យប្រណិតៗ', 'អចលនទ្រព្យទំនើបៗ');    // Diffierent titles
                    $random_title = $random_titles[array_rand($random_titles, 1)]; ?>
                     <h2 class="elm-maintitle"><?php echo $random_title; ?></h2> <?php 
                } ?>
      </div>
    <div class="elm-item-organizer organizer-alt layout-box elm-items">
    <div class="elm-item-organizer-container column-4 layout-box carousel-disabled"> 
      
    <?php
    //$category_id = get_queried_object();  // this wont work; without being declared, it is a default created by the theme
    $terms = get_terms( array( 'parent' => $category_id ) );    // Set this is to display all posts in any top categories with a category ID updated below. For example: Cars, Property, Services, etc.
    //$terms = get_terms( array( 'child_of' => $category_id ) );    // Set this to display all posts in any first subcategories with a subcategory ID updated below. Example: Cars for rent, Cars for sale, Property for rent, Property for sale, etc.
    //$category_id == 2252; // Set this and below to display posts from first child categories in Property for rent with ID: 2252 for EN page; no KM ID not needed
    //$terms = get_terms( array( 'child_of' => $category_id ) );  // Example: first child categories of Property for rent (2252) include Condos (2254), Building (5641), etc.
    ?>

    <?php
    define('POSTSperPAGE', 20);
    $featured_orderByDESC = false;
    $orderByRandom = true;
    $featured_orderByDefault = false;
    $featured_orderByASC = false;

    foreach ($terms as $term) {
        if ($term->term_id == 2250 || $term->term_id == 2502) {   // Parent category (Property) ID for EN page (KM page is automatically switched)

            if( $featured_orderByDefault == true ) {
                $arrayOfposts = query_posts( array(
                        'post_type'      =>  'ait-item',
                        'post_status'    =>  'publish',
                        'posts_per_page' => POSTSperPAGE,           
                        'tax_query'      => array ( array (
                            'taxonomy' => 'my_customPostType',
                            'terms'    => $term->term_id,
                                )
                        ),
                        'meta_query' => array(
                            'relation' => 'OR',
                            array(
                                'key'   => '_ait-item_item-featured',
                                'value' => 1,
                                'compare' => 'LIKE'
                            ),
                        ),
                    )
                );
            } else if( $featured_orderByASC == true ) {
                $arrayOfposts = query_posts( array(
                        'post_type'      =>  'ait-item',
                        'post_status'    =>  'publish',
                        'posts_per_page' => POSTSperPAGE,            
                        'tax_query'      => array ( array (
                            'taxonomy' => 'my_customPostType',
                            'terms'    => $term->term_id,
                                )
                        ),
                        'meta_query' => array(
                            'relation' => 'OR',
                            array(
                                'key'   => '_ait-item_item-featured',
                                'value' => 1,
                                'compare' => 'LIKE'
                            ),
                        ),
                        'order' => 'ASC',
                    )
                );
            } else if ( $orderByRandom == true) {
                $arrayOfposts = query_posts( array(
                        'post_type'      =>  'ait-item',
                        'post_status'    =>  'publish',
                        'posts_per_page' => POSTSperPAGE,            
                        'tax_query'      => array ( array (
                            'taxonomy' => 'my_customPostType',
                            'terms'    => $term->term_id,
                                )
                        ),
                        'meta_query' => array(
                            'relation' => 'OR',
                            array(
                                'key'   => '_my_customPostType_item-featured',
                                'value' => 1,
                                'compare' => 'LIKE'
                            )
                        ),
                        'orderby' => 'rand',
                    )
                );
                
            }
        }
    }
    ?>


        
    <?php
    foreach($arrayOfposts as $cpost) {        
            $p_id   = $cpost->ID;           //get_the_ID();
            $p_meta = get_post_meta($p_id);
            $a = $p_meta['_my_customPostType_item-data'][0];
            $featured = $p_meta['_my_customPostType_item-featured'][0];
            $us = unserialize($a);
            $prc = $us['subtitle'];
            $prc_type = $us['price_type'];   
     ?>  

        <div data-id="<?php static $i = 1; echo $i++; ?>" class="item item-first image-present align-center <?php echo $featured == '1' ? 'item-featured': ''; ?>">
             
           <div class="item-thumbnail">
                <a href="<?php echo get_permalink($p_id); ?>">
                    <?php 
                    if (get_the_post_thumbnail_url($p_id,'post-thumbnail')) { 
                        $imgUrl = get_the_post_thumbnail_url($p_id,'post-thumbnail');
                    } else { 
                        $imgUrl = site_url()."/wp-content/themes/businessfinder2-child/design/img/default_featured_img.jpg";
                    } ?>
                    <div class="item-thumbnail-wrap" style="background-image:url('<?php echo $imgUrl; ?>');"></div>
                    <div class="item-text-wrap">
                        <div class="item-text">
                          <div class="item-excerpt txtrows-3">
                            <?php 
                            //$content_post = get_post($p_id);
                            echo $content = $cpost->post_content;
                            /* $expt = get_the_excerpt($p_id);
                            if($expt != ''){
                                echo $expt; 
                            } else {
                                $content_post = get_post($p_id);
                                $content = $content_post->post_content;
                                $content = apply_filters('the_content', $content);
                                $content = str_replace(']]>', ']]&gt;', $content);
                                echo $content;
                             } */ ?>
                          </div>
                       </div>
                    </div>
                </a>
            </div>

            <div class="item-box-content-wrap">

                
                <div class="item-categories">

                </div>

                <div class="item-title">
                    <a href="<?php echo get_permalink($p_id); ?>">
                             <h3><?php echo $cpost->post_title; ?></h3>
                             <span class="subtitle">

                             </span>
                          </a>
                </div>

                
                <?php 
                   $serializeArr = unserialize($p_meta['_my_customPostType_item-data'][0]);
                   if($serializeArr) { 
                        if($serializeArr['map']['address']) { ?>
                            <div class="item-location">
                                <p class="txtrows-2"><?php echo $serializeArr['map']['address']; ?></p>
                            </div>
                        <?php 
                        } 
                        else { ?>
                            <div class="item-location">
                                <p class="txtrows-2">Cambodia</p>
                            </div>
                        <?php 
                        }
                    } ?>

                
            </div>
        </div>




     <?php }     ?>


        </div>
         </div>
        </div>

Many thanks in advance.

Share Improve this question asked Apr 12 at 7:42 Meng888Meng888 536 bronze badges 1
  • I found out that {if is_singular('mycustompost_type')} no longer works for the custom codes at top of footer.php to display custom post type posts at the bottom of the Single post page. – Meng888 Commented Apr 14 at 22:10
Add a comment  | 

1 Answer 1

Reset to default 1

The most common issue that would cause one snippet not to work is if you're doing anything with queries. WP has functions such as wp_reset_query() to run after a custom query to ensure that after you're done using the results of a custom query, you go back to the original one, which the rest of the page expects.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748641133a313717.html

最新回复(0)