X recent posts shortcode causing site error

Hi X Theme, after updating software one page is showing a fatal error on my site. If I remove the shortcode [x_recent_posts count=“1” show_excerpt=“true” no_image=“true” orientation=“vertical” excerpt=“true” excerpt_words=“20” meta=“false”] then the error is resolved.

If I change to a standard wordpress theme the shortcode then works.

Can you please advise what is wrong?

Here is the error showing

This is what the page looked like before we updated:

I have now removed the shortcode from that home page and put it on this page for testing:
https://modelcommerciallease.co.uk/test-2/

Is there an alternative shortcode I can use or do you know what the error might be?

Thank you.

Hi @pyntolimited

I have used your shortcode in my local environment and it works perfectly with the latest version of the theme. I would suggest you check the following common issue to recognize the problem.

There might be some different reasons behind your reason, I would like to suggest troubleshoot the following common issue to help us to recognize the reason.

1.Plugin Conflict
2.Theme Update related issue
3.Child Theme Related issue ** if you have activated it.

If you discover that an issue is coming from a custom code or 3rd party plugin, kindly consult with a developer or contact the plugin author. Please note that we do not provide support for custom codes and 3rd party plugins.
If the above points do not resolve your issue, please provide login credentials for your site in a secure note to examine it further, including:

– WordPress Site URL & Login URL
– WordPress Admin username/password

To create a secure note, click the key icon underneath any of your posts.

Alternatively, you can use the Classic Recent Post element instead of the shortcode.

Thanks

I also just want to confirm that we switched to wp 2021 theme and the error was resolved. We also tried turning all non-essential plugins off and none of them seemed to affect the issue.

Hi @pyntolimited,

It might be due to the improper update of the X theme and Cornerstone plugin, I would suggest you follow the below steps to troubleshoot.

  1. Delete the Cornerstone
  2. After deletion please use the link in the dashboard to automatically install the Cornerstone.

If that does not resolve the issue, please provide login credentials for your site in a secure note to examine it further, including:

– WordPress Site URL & Login URL
– WordPress Admin username/password

To create a secure note, click the key icon underneath any of your posts.

Thanks

It does seemed to be a child theme issue. Here is the code from the functions file, do you think something here is wrong?

<?php // ============================================================================= // FUNCTIONS.PHP // ----------------------------------------------------------------------------- // Overwrite or add your own custom functions to X in this file. // ============================================================================= // ============================================================================= // TABLE OF CONTENTS // ----------------------------------------------------------------------------- // 01. Enqueue Parent Stylesheet // 02. Additional Functions // ============================================================================= // Enqueue Parent Stylesheet // ============================================================================= add_filter( 'x_enqueue_parent_stylesheet', '__return_true' ); // Additional Functions // ============================================================================= add_shortcode( 'current_year', 'get_current_year' ); function get_current_year(){ return date('Y'); } // Add Excerpts to the recent post element // ============================================================================= function x_shortcode_recent_posts_v2code( $atts ) { extract( shortcode_atts( array( 'id' => '', 'class' => '', 'style' => '', 'type' => 'post', 'count' => '', 'category' => '', 'offset' => '', 'orientation' => '', 'show_excerpt' => 'false', 'no_sticky' => '', 'no_image' => '', 'fade' => '' ), $atts, 'x_recent_posts' ) ); $allowed_post_types = apply_filters( 'cs_recent_posts_post_types', array( 'post' => 'post' ) ); $type = ( isset( $allowed_post_types[$type] ) ) ? $allowed_post_types[$type] : 'post'; $id = ( $id != '' ) ? 'id="' . esc_attr( $id ) . '"' : ''; $class = ( $class != '' ) ? 'x-recent-posts cf ' . esc_attr( $class ) : 'x-recent-posts cf'; $style = ( $style != '' ) ? 'style="' . $style . '"' : ''; $count = ( $count != '' ) ? $count : 3; $category = ( $category != '' ) ? $category : ''; $category_type = ( $type == 'post' ) ? 'category_name' : 'portfolio-category'; $offset = ( $offset != '' ) ? $offset : 0; $orientation = ( $orientation != '' ) ? ' ' . $orientation : ' horizontal'; $show_excerpt = ( $show_excerpt == 'true' ); $no_sticky = ( $no_sticky == 'true' ); $no_image = ( $no_image == 'true' ) ? $no_image : ''; $fade = ( $fade == 'true' ) ? $fade : 'false'; $js_params = array( 'fade' => ( $fade == 'true' ) ); $data = cs_generate_data_attributes( 'recent_posts', $js_params ); $output = "
"; $q = new WP_Query( array( 'orderby' => 'date', 'post_type' => "{$type}", 'posts_per_page' => "{$count}", 'offset' => "{$offset}", "{$category_type}" => "{$category}", 'ignore_sticky_posts' => $no_sticky ) ); if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post(); if ( $no_image == 'true' ) { $image_output = ''; $image_output_class = 'no-image'; } else { $image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'entry-cropped' ); $bg_image = ( $image[0] != '' ) ? ' style="background-image: url(' . $image[0] . ');"' : ''; $image_output = '
'; $image_output_class = 'with-image'; } $excerpt = ( $show_excerpt ) ? '

' . preg_replace('//', '', cs_get_raw_excerpt() ) . ' read more

' : ''; $output .= '' . '' . '
' . $image_output . '
' . '

' . get_the_title() . '

' . '' . get_the_date() . '' . $excerpt . '
' . '
' . '' . '
'; endwhile; endif; wp_reset_postdata(); $output .= '
'; return $output; } // ============================================================================= add_action('wp_head', 'change_recent_posts_to_v2'); function change_recent_posts_to_v2() { remove_shortcode( 'x_recent_posts' ); add_shortcode( 'x_recent_posts', 'x_shortcode_recent_posts_v2code' ); } // =============================================================================

Hello @pyntolimited,

It is very difficult to check the code since the code formatting is missing. I would suggest you contact a developer who can assist you with your conecern or you can avail services called One where customization questions are answered. Please note that we don’t provide customized child theme support. It is out of the support scope.

Thanks for understanding

Ok I understand sorry about the formatting I will send it formatted. This code was supplied by X theme I believe to get the recent post short code to work.

If you are able to tell me how to get the one most recent post to show as title, date, first 20 words and read more link that would be great, please?

// Add Excerpts to the recent post element
// =============================================================================
function x_shortcode_recent_posts_v2code( $atts ) {
extract( shortcode_atts( array(
‘id’ => ‘’,
‘class’ => ‘’,
‘style’ => ‘’,
‘type’ => ‘post’,
‘count’ => ‘’,
‘category’ => ‘’,
‘offset’ => ‘’,
‘orientation’ => ‘’,
‘show_excerpt’ => ‘false’,
‘no_sticky’ => ‘’,
‘no_image’ => ‘’,
‘fade’ => ‘’
), $atts, ‘x_recent_posts’ ) );

$allowed_post_types = apply_filters( ‘cs_recent_posts_post_types’, array( ‘post’ => ‘post’ ) );
$type = ( isset( $allowed_post_types[$type] ) ) ? $allowed_post_types[$type] : ‘post’;

$id = ( $id != ‘’ ) ? ‘id="’ . esc_attr( $id ) . ‘"’ : ‘’;
$class = ( $class != ‘’ ) ? ‘x-recent-posts cf ’ . esc_attr( $class ) : ‘x-recent-posts cf’;
$style = ( $style != ‘’ ) ? ‘style="’ . $style . ‘"’ : ‘’;
$count = ( $count != ‘’ ) ? $count : 3;
$category = ( $category != ‘’ ) ? $category : ‘’;
$category_type = ( $type == ‘post’ ) ? ‘category_name’ : ‘portfolio-category’;
$offset = ( $offset != ‘’ ) ? $offset : 0;
$orientation = ( $orientation != ‘’ ) ? ’ ’ . $orientation : ’ horizontal’;
$show_excerpt = ( $show_excerpt == ‘true’ );
$no_sticky = ( $no_sticky == ‘true’ );
$no_image = ( $no_image == ‘true’ ) ? $no_image : ‘’;
$fade = ( $fade == ‘true’ ) ? $fade : ‘false’;

$js_params = array(
‘fade’ => ( $fade == ‘true’ )
);

$data = cs_generate_data_attributes( ‘recent_posts’, $js_params );

$output = “<div {$id} class=”{$class}{$orientation}" {$style} {$data} data-fade="{$fade}" >";

$q = new WP_Query( array(
  'orderby'             => 'date',
  'post_type'           => "{$type}",
  'posts_per_page'      => "{$count}",
  'offset'              => "{$offset}",
  "{$category_type}"    => "{$category}",
  'ignore_sticky_posts' => $no_sticky
) );

if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();

  if ( $no_image == 'true' ) {
    $image_output       = '';
    $image_output_class = 'no-image';
  } else {
    $image              = wp_get_attachment_image_src( get_post_thumbnail_id(), 'entry-cropped' );
    $bg_image           = ( $image[0] != '' ) ? ' style="background-image: url(' . $image[0] . ');"' : '';
    $image_output       = '<div class="x-recent-posts-img"' . $bg_image . '></div>';
    $image_output_class = 'with-image';
  }

   $excerpt = ( $show_excerpt ) ? '<div class="x-recent-posts-excerpt"><p style="color:initial;">' . preg_replace('/<a.*?more-link.*?<\/a>/', '', cs_get_raw_excerpt() ) . ' read more</p></div>' : '';

  $output .= '<a class="x-recent-post' . $count . ' ' . $image_output_class . '" href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to: "%s"', 'cornerstone' ), the_title_attribute( 'echo=0' ) ) ) . '">'
             . '<article id="post-' . get_the_ID() . '" class="' . implode( ' ', get_post_class() ) . '">'
               . '<div class="entry-wrap">'
                 . $image_output
                 . '<div class="x-recent-posts-content">'
                   . '<h3 class="h-recent-posts">' . get_the_title() . '</h3>'
                   . '<span class="x-recent-posts-date">' . get_the_date() . '</span>'
                       . $excerpt
                 . '</div>'
               . '</div>'
             . '</article>'
           . '</a>';

endwhile; endif; wp_reset_postdata();

$output .= ‘’;

return $output;
}

// =============================================================================

add_action(‘wp_head’, ‘change_recent_posts_to_v2’);
function change_recent_posts_to_v2() {
remove_shortcode( ‘x_recent_posts’ );
add_shortcode( ‘x_recent_posts’, ‘x_shortcode_recent_posts_v2code’ );
}
// =============================================================================

Hi @pyntolimited,

Still, the code is not formatted well to check. I would suggest you go through the suggestion given by my colleague in his previous post.

Thanks

If I take that code out the error stops - so it is obviously no longer compatible with the latest version of x theme.

Could you please adivse how to add the most recent post with shortcode. I would like to show tite, date, first 20 words and then a ‘read more’ link.

Thank you.

I have decided to abandon the recent posts shortcode and will use a plugin ‘display posts’ instead.

Thank you for trying to help.

Hi @pyntolimited,

Glad to know that.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.