0

I'm looking to insert a function which is otherwise denoted as a WP shortcode. The page is failing to display entirely when this code is inserted. Here is the full code which comprises the shortcode:

function vntd_blog_carousel($atts, $content = null) {
extract(shortcode_atts(array(
    "cats" => '',
    "posts_nr" => '',
    "thumb_style" => ''
), $atts));

wp_enqueue_script('prettyPhoto', '', '', '', true);
wp_enqueue_style('prettyPhoto');

wp_enqueue_script('owl-carousel', '', '', '', true);
wp_enqueue_style('owl-carousel');

ob_start();

echo '<div class="vntd-portfolio-carousel blog-carousel"><div class="works white">';

$size = 'portfolio-square';

wp_reset_postdata();

$args = array(
    'posts_per_page' => 6,
    'cat'       => 4,
    'orderby'   => 'slug'
);
$the_query = new WP_Query($args); 

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

I would normally use the shortcode, but I am placing this directly in the PHP page file. The output is failing to display on the page however, and the page fails to load with this code. I am assuming this is because there is an error with one of the parameters that I have manually filled in.

1 Answer 1

2

Your code is badly broken.

  1. You start a function definition but never finish it. There is no associated } to end the function.
  2. You start an if conditional and never close it.
  3. You start a while loop and never close it.

Those are why your page won't load with that code. You are getting a fatal PHP error and would be getting multiple fatal errors if the whole script didn't bail at the first one. If you have debugging enabled you would see that.

  1. You are also using output buffering in a way that doesn't do anything at all, and if I am not mistaken in a way that will cause the rest of the page to fail to display even if you didn't have the fatal errors.

Additionally, your Loop wouldn't output anything anyway as you haven't included any code that actually echos post information. The minimal fixes you need are:

And you are passing category information into the shortcode but have a category hard-coded into the query. I've altered your defaults to compensate.

function vntd_blog_carousel($atts, $content = null) {
  extract(shortcode_atts(array(
      "cats" => 6, // default cat, if any
      "posts_nr" => '',
      "thumb_style" => ''
  ), $atts));

  wp_enqueue_script('prettyPhoto', '', '', '', true);
  wp_enqueue_style('prettyPhoto');

  wp_enqueue_script('owl-carousel', '', '', '', true);
  wp_enqueue_style('owl-carousel');

  ob_start();

  echo '<div class="vntd-portfolio-carousel blog-carousel"><div class="works white">';

  $size = 'portfolio-square';

  $args = array(
      'posts_per_page' => 6,
      'cat'       => $cats,
      'orderby'   => 'slug'
  );
  $the_query = new WP_Query($args); 

  if ($the_query->have_posts()) {
    while ($the_query->have_posts()) {
      $the_query->the_post();
      // echo some post data
      the_title();
      the_content();
    } // close the while Loop
  } // close the if conditional

  wp_reset_postdata(); // moved from before the Loop where it really wasn't doing any good

  return ob_get_clean(); // use the output buffer that you've created
} // close the function definition.

See the code comments for exactly what I did. I have also formatted the code for readability and removed that nightmarish alternative control syntax, which is only good for crazy-making.

To turn the whole thing into a shortcode all you need is:

add_shortcode('vntd_blog_carousel','vntd_blog_carousel');

And to manually run it use:

do_shortcode('[vntd_blog_carousel cats="2"]');

I have no idea what the other two shortcode attributes are supposed to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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