0

I have been working on a shortcode with help from this article: http://code.tutsplus.com/tutorials/create-a-shortcode-to-list-posts-with-multiple-parameters--wp-32199. I have tried to do what the article says, however I have tried to include a while loop. The problem is that when I paste it into the functions.php in the theme editor, the page crashes and returns internal server error 500.

My code looks like this:

add_shortcode('lande_galleri', 'lande_galleri_parameter');

function lande_galleri_parameter($atts) {
ob_start();

//Definer attributter
extract( shortcode_atts( array (
    'post_type' => 'embm_beer',
    'order' =>'date',
    'orderby' => 'title',
    'posts' => -1,
    'land' => '',
), $atts));

//Definer parametre
$options = array(
    'post_type' => $type,
    'order' => $order,
    'orderby' => $orderby,
    'posts_per_page' => $posts,
    'land' => $land,
);
$query = new WP_Query($options);

if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();{ ?>
    <div class="findoel"> <?php
        echo '<a href="' . get_permalink($post->ID) . '" title="' . esc_attr($post->post_title) . '">';
        echo get_the_post_thumbnail($post->ID, 'medium');
        echo '</a>';?>
    </div>
<?php endwhile;
wp_reset_query();
wp_reset_postdata();

$myvariable = ob_get_clean();
return $myvariable;
}
}

Can anyone see the problem?

The point of the shortcode: I have a custom post type with a taxanomy. I would like to have the shortcode attribute "land", and then if the input is for example "germany", I would like the file to get all of the posts within the category, and they display the post thumbnails.

  • Look at the end of the if ($query->have_posts()) line. What is that { doing there? – s_ha_dum Oct 30 '15 at 19:18
  • 1
    If you had debugging enabled you would have spotted that. – s_ha_dum Oct 30 '15 at 19:19
  • I tried removing the {, but it still makes the server error – ChristianUlle Oct 30 '15 at 19:23
  • 1
    did you also remove the extra closing } at the bottom? have you enabled debugging yet? ;) – Milo Oct 30 '15 at 19:25
  • You are also missing an endif. If you would indent carefully and avoid those nightmarish "template tag" style conditionals you'd spot this this stuff much more easily. – s_ha_dum Oct 30 '15 at 19:27
0

Like @s_ha_dum said, those nightmarish "template tag" style conditionals made it hard to find those errors. Please use define( 'WP_DEBUG', true ); in your wp-config file to print those PHP errors while you are developing.

function lande_galleri_parameter($atts) {
    extract( shortcode_atts( array (
        'post_type' => 'embm_beer',
        'order' =>'date',
        'orderby' => 'title',
        'posts' => -1,
        'land' => '',
    ), $atts));

    $options = array(
        'post_type' => $post_type,
        'order' => $order,
        'orderby' => $orderby,
        'posts_per_page' => $posts,
        'land' => $land,
    );

    $out = '';

    $query = new WP_Query($options);

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();
            $out .= '<div class="findoel"><a href="' . get_permalink( get_the_ID() ) . '" title="' . esc_attr( get_the_title() ) . '">';
            $out .= get_the_post_thumbnail( get_the_ID(), 'medium');
            $out .= '</a></div>';
        }
    }
    wp_reset_postdata();
    return $$out;
}
add_shortcode('lande_galleri', 'lande_galleri_parameter');
  • Thank you! That solved the problem. And from now on, I will use the DEBUG – ChristianUlle Oct 30 '15 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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