Support » Plugin: Latest Post Shortcode » After update custom tiles no longer functioning

  • Resolved opicron

    (@opicron)


    I have postponed updating because it triggered an fatal exception on one of my sites.

    When I update the custom templates are no longer functioning. Functions like get_the_id() etc trigger an error.

    Probably I am doing something wrong in the custom templates. How should I access the current post/page/product element in an template?

    Thank you!

Viewing 15 replies - 1 through 15 (of 17 total)
  • Plugin Author Iulia Cazan

    (@iulia-cazan)

    Hi!

    Can you check in your templates if you write get_the_id() or get_the_ID(). The function is case sensitive as far as I recall. What errors do you actually get in your template?

    Regards,
    Iulia

    I am following this tutorial to the letter:

    https://iuliacazan.ro/wp-content/uploads/2017/06/sample-one-custom-template.txt

    But it is no longer functioning. Something changed in the last few updates– as it was working before, and is working when I rollback to old version.

    Can you confirm that the above code is working for you? My specifics: I choose parent ID page to show child posts. That specifically is not working.

    I disabled all templates except the one described above and then there are no errors, but it loops the parent page instead of the children.

    • This reply was modified 5 months, 2 weeks ago by opicron.
    • This reply was modified 5 months, 2 weeks ago by opicron.
    Plugin Author Iulia Cazan

    (@iulia-cazan)

    Hi,

    Can you give me your exact shortcode, to test with the same configuration?

    Regards,
    Iulia

    This is the used shortcode:

    [latest-selected-content limit="7" image="thumbnail" elements="31" css="four-columns align-left as-overlay tall dark hover-zoom" type="page" status="publish" parent="14815" orderby="dateD"]

    As soon as I remove the elements=”31″ the right posts are shown.

    • This reply was modified 5 months, 2 weeks ago by opicron.
    Plugin Author Iulia Cazan

    (@iulia-cazan)

    From my tests, the_thumbnail native function should be the_post_thumbnail, and can be replaced with some other code to make it work in the latest WP versions
    Try this function instead

    
    if ( ! function_exists( 'my_theme_lps_filter_use_custom_tile_markup' ) ) {
    	/**
    	 * Create the custom output of tiles into the 3rd-party plugin (the markup and logic of the tiles).
    	 *
    	 * @param  string $tile_pattern The tile pattern slug.
    	 * @param  object $post         The WP Post object to be rendered.
    	 * @return string
    	 */
    	function my_theme_lps_filter_use_custom_tile_markup( $tile_pattern, $post ) {
    		switch ( $tile_pattern ) {
    			case '[_custom_my_tile_1]':
    			default:
    				setup_postdata( $post->ID );
    				ob_start();
    				$thumbnail = get_the_post_thumbnail( $post->ID, 'thumbnail' );
    				?>
    				<a href="<?php the_permalink(); ?>" class="my-custom-class">
    					<div class="photo-wrapper">
    						<?php if ( ! empty( $thumbnail ) ) : ?>
    							<?php echo wp_kses_post( $thumbnail ); ?>
    						<?php endif; ?>
    					</div>
    					<div class="text">
    						<h2><?php the_title(); ?></h2>
    						<h5><?php the_author(); ?> - <?php the_date(); ?></h5>
    					</div>
    				</a>
    				<?php
    				$tile_pattern = ob_get_clean();
    				break;
    		}
    
    		return $tile_pattern;
    	}
    }
    add_filter( 'lps_filter_use_custom_tile_markup', 'my_theme_lps_filter_use_custom_tile_markup', 1, 2 );
    

    Let me know it this works.

    • This reply was modified 5 months, 2 weeks ago by Iulia Cazan.
    • This reply was modified 5 months, 2 weeks ago by Iulia Cazan.

    Sorry Lulia, the $post always seem to contain the wrong post. It is always the parent post (page in my case). The number of elements corresponds with the actual number of childs of the page. However each one has the same postdata.

    As soon as I remove the element function the correct pages/posts are shown.

    Plugin Author Iulia Cazan

    (@iulia-cazan)

    I am not sure you are using the exact code from the example. With the code I gave, there is this setup_postdata( $post->ID ); which is doing the $post setup correctly on my end. I cannot replicate the issue. What WP core version do you run?

    I tried both $post and $post->ID. My WP core is 5.5 but the issue also happens on the previous version.

    I am trying to understand why the element argument has an impact. Before setup_postdata() the $post already contains the wrong post.

    The only difference to make it work is rollback to previous LPS version. I tried to see in your code what the element argument makes the difference. But no luck so far.

    If you wish I can send you my staging and live site links to show the difference. Where can I email them?

    • This reply was modified 5 months, 2 weeks ago by opicron.
    Plugin Author Iulia Cazan

    (@iulia-cazan)

    I will test some more and get back to you when I discover/replicate/fix the issue.

    Plugin Author Iulia Cazan

    (@iulia-cazan)

    After more tests I could replicate something like what you described. Seems that setup_postdata is not working as previously. I could not trace back the changes of this native function to see what actually changed about it. However, the dirty solution is to include the global object in the function then reset it (this is important).

    Try this function and let me know if this works for you like this.

    if ( ! function_exists( 'my_theme_lps_filter_use_custom_tile_markup' ) ) {
    	/**
    	 * Create the custom output of tiles into the 3rd-party plugin (the markup and logic of the tiles).
    	 *
    	 * @param  string $tile_pattern The tile pattern slug.
    	 * @param  object $object       The WP Post object to be rendered.
    	 * @param  array  $args         The shortcode usable arguments.
    	 * @return string
    	 */
    	function my_theme_lps_filter_use_custom_tile_markup( $tile_pattern, $object, $args ) {
    		global $post;
    		$content = '';
    		$post    = $object;
    		setup_postdata( $post );
    		switch ( $tile_pattern ) {
    			case '[_custom_my_tile_1]':
    			default:
    				ob_start();
    				?>
    				<a href="<?php the_permalink(); ?>" class="my-custom-class">
    					<div class="photo-wrapper">
    						<?php if ( has_post_thumbnail() ) : ?>
    							<?php the_post_thumbnail(); ?>
    						<?php endif; ?>
    					</div>
    					<div class="text">
    						<h2><?php the_title(); ?></h2>
    						<p><?php the_excerpt(); ?></p>
    						<h5><?php the_author(); ?> - <?php the_date(); ?></h5>
    					</div>
    				</a>
    				<?php
    				$content = ob_get_clean();
    				break;
    		}
    
    		wp_reset_postdata(); // Super important to do this here.
    		return $content;
    	}
    }
    add_filter( 'lps_filter_use_custom_tile_markup', 'my_theme_lps_filter_use_custom_tile_markup', 1, 3 );
    

    Regards,
    Iulia

    That indeed did the trick! Thank you for your efforts. Do you think this can be fixed in future versions?

    Just to add:

    Seems that setup_postdata is not working as previously. I could not trace back the changes of this native function to see what actually changed about it.

    The function did not change, if I rollback to your previous release version of LPS the code works. It seems something changed in LPS to me.

    Plugin Author Iulia Cazan

    (@iulia-cazan)

    I understand, I will check again.
    Which LPS version are you reverting to?

    The version which is working for us is version: 9.6

    • This reply was modified 5 months, 1 week ago by opicron.
    Plugin Author Iulia Cazan

    (@iulia-cazan)

    Hi,

    Please try with 9.6.5 and let me know if this is working as previously.

    Regards,
    Iulia

Viewing 15 replies - 1 through 15 (of 17 total)
  • You must be logged in to reply to this topic.