1

Background:

I am making a photography website and thought that skt full width is a good theme for the landing page. It worked out all right. Then I started to get fancy. I wanted my client to be able to select whether they could have six random pictures selected for the front page slideshow. This is done through an extra option in the media editor, where they could select if they want a specific image to be part of the slideshow.

I edited their theme functions file and in theory the code should work, however now instead the code does not work. I kept all their code and just added on to the file, and when I select either option in the customizer for the random photos or the preselected photos, both of them do not work.

Question:

Is their a fix to the code (shown below), that would make the slideshow work?

Also since I want to use this theme only as my landing/home page, should I copy the code for the home page into my site plugin, and then select a different theme?

Code:

<?php
if (is_front_page() || is_home()) {
    if (get_theme_mod('rand_slide') == 'static') {
        for ($i = 1; $i < 6; $i++) {
            if (of_get_option('slide' . $i, true) != "") {
                $imgUrl = esc_url(of_get_option('slide' . $i, true));
                $imgTitle = esc_html(of_get_option('slidetitle' . $i, true));
                $imgDesc = esc_html(of_get_option('slidedesc' . $i, true));
                $imgHref = esc_html(of_get_option('slideurl' . $i, true));
                if ($imgUrl != '') {
                    echo '{image : \'' . $imgUrl . '\', title : \'<div class="slide-title"><span>' . ( ($imgHref != '' && $imgTitle != '') ? '<a href="' . $imgHref . '">' : '') . $imgTitle . ( ($imgHref != '' && $imgTitle != '') ? '</a>' : '') . '</span></div><div class="slide-description"><span>' . $imgDesc . '</span></div>' . ( ($imgHref != '') ? '<div class="slide-description"><span><a href="' . $imgHref . '">Read More &rsaquo;</a></span></div>' : '') . '\', thumb : \'' . $imgUrl . '\', url : \'\'},' . "\n";
                }
            }
        }
    } elseif (get_theme_mod('rand_slide') == 'random') {
        $args = array(
            'post_type' => 'attachment',
            'meta_key' => 'on_front_page',
            'meta_value' => '1',
            'orderby' => 'rand',
            'posts_per_page' => 6,
            'max_num_pages' => 1,
        );

        $slides = new WP_Query($args);

        if ($slides->have_posts()) {
            while ($slides->have_posts()) {
                $img = $slides->next_post();
                $imgId = $img->ID;
                $imgTitle = $img->post_title;
                $imgHref = get_permalink($img);
                $imgDesc = $img->post_content;
                $imgData = wp_get_attachment_image_src($imgId, 'thumbnail');
                $imgUrl = $imgData[0];
                if ($imgUrl != '') {
                    echo '{image : \'' . $imgUrl . '\', title : \'<div class="slide-title"><span>' . ( ($imgHref != '' && $imgTitle != '') ? '<a href="' . $imgHref . '">' : '') . $imgTitle . ( ($imgHref != '' && $imgTitle != '') ? '</a>' : '') . '</span></div><div class="slide-description"><span>' . $imgDesc . '</span></div>' . ( ($imgHref != '') ? '<div class="slide-description"><span><a href="' . $imgHref . '">Read More &rsaquo;</a></span></div>' : '') . '\', thumb : \'' . $imgUrl . '\', url : \'\'},' . "\n";
                }
            }
        }
    }
}

The original code started at line 170 in their theme function file.

  • 1
    Have you tried turning on Debugging in the wp-config.php file? That's usually one of the most useful ways to figure out any kind of syntax errors if any exist. The debug log should point out lines and specific error messages if enabled. – Howdy_McGee Jan 27 '15 at 22:50
  • Where would those errors be shown? – benj rei Jan 27 '15 at 22:57
  • 1
    If you enabled debugging and WP_DEBUG_LOG, refresh your page where you expect the code to load, if something is wrong you would see a new file called debug.log in your /wp-content/ folder. – Howdy_McGee Jan 27 '15 at 22:58
  • There is nothing there and I refreshed all relevant pages, and I do not see the file – benj rei Jan 27 '15 at 23:06
0

Try this... it should at least point you in the right direction. Note that without knowing what your functions / options are, it is very hard to give a good answer.

<?php
try {
  $do_echo = true;
  if (is_front_page() || is_home()) {
    if ( $do_echo ) { echo "<p>Is front page or blog</p>"; }
    $test = get_theme_mod('rand_slide');
    if ( $do_echo ) { echo "<p>get theme mode: {$test}</p>"; }
    if (get_theme_mod('rand_slide') == 'static') {
      if ( $do_echo ) { echo "<p>Random slide is static</p>"; }
      for ($i = 1; $i < 6; $i++) {
        $test_two = of_get_option('slide' . $i, true);
        if ( $do_echo ) { echo "<p>of_get_option( slide{$i}, true ) is {$test_two}</p>"; }
        if (of_get_option('slide' . $i, true) != "") {
            if ( $do_echo ) { echo "<p>In inner loop for {$i}</p>"; }
            $imgUrl = esc_url(of_get_option('slide' . $i, true));
            if ( $do_echo ) { echo "<p>imgUrl is now {$imgUrl}</p>"; }
            $imgTitle = esc_html(of_get_option('slidetitle' . $i, true));
            $imgDesc = esc_html(of_get_option('slidedesc' . $i, true));
            $imgHref = esc_html(of_get_option('slideurl' . $i, true));
            if ($imgUrl != '') {
                if ( $do_echo ) { echo "<p>Going to echo slide...</p>"; }
                $to_echo = '{image : \'' . $imgUrl . '\', title : \'<div class="slide-title"><span>' . ( ($imgHref != '' && $imgTitle != '') ? '<a href="' . $imgHref . '">' : '') . $imgTitle . ( ($imgHref != '' && $imgTitle != '') ? '</a>' : '') . '</span></div><div class="slide-description"><span>' . $imgDesc . '</span></div>' . ( ($imgHref != '') ? '<div class="slide-description"><span><a href="' . $imgHref . '">Read More &rsaquo;</a></span></div>' : '') . '\', thumb : \'' . $imgUrl . '\', url : \'\'},' . "\n";
               if ( $do_echo ) { echo "<p>Would now echo " . html_attributes($to_echo) . "</p>";
               echo $to_echo;
            } else if ( $do_echo ) {
               echo "<p>imgUrl empty, not displaying slide</p>";
            }
        } else if ( $do_echo ) {
            echo "<p>of_get_option returned empty string. not echoing slide.</p>";
        }
      }
    } elseif (get_theme_mod('rand_slide') == 'random') {
      $args = array(
        'post_type' => 'attachment',
        'meta_key' => 'on_front_page',
        'meta_value' => '1',
        'orderby' => 'rand',
        'posts_per_page' => 6,
        'max_num_pages' => 1,
      );

      $slides = new WP_Query($args);

      if ($slides->have_posts()) {
        while ($slides->have_posts()) {
            $img = $slides->next_post();
            $imgId = $img->ID;
            $imgTitle = $img->post_title;
            $imgHref = get_permalink($img);
            $imgDesc = $img->post_content;
            $imgData = wp_get_attachment_image_src($imgId, 'thumbnail');
            $imgUrl = $imgData[0];
            if ($imgUrl != '') {
                echo '{image : \'' . $imgUrl . '\', title : \'<div class="slide-title"><span>' . ( ($imgHref != '' && $imgTitle != '') ? '<a href="' . $imgHref . '">' : '') . $imgTitle . ( ($imgHref != '' && $imgTitle != '') ? '</a>' : '') . '</span></div><div class="slide-description"><span>' . $imgDesc . '</span></div>' . ( ($imgHref != '') ? '<div class="slide-description"><span><a href="' . $imgHref . '">Read More &rsaquo;</a></span></div>' : '') . '\', thumb : \'' . $imgUrl . '\', url : \'\'},' . "\n";
            }
        }
      }
    }
  }
} catch ( Exception $e ) {
    echo "<p>A fatal error has occurred...</p><pre>" . print_r($e, true) . "</pre>";
}

Yes, this is hackish... but by doing a bunch of echo statements, you should see exactly where what you are expecting is not happening.

And as noted by Howdy, be sure to turn your debug flags on and check for problems that way as well.

If you are not running at least PHP 5, then remove the surrounding try / catch block.

Personally, if you are having troubles with your code, I'd recommend the following:

  • Make sure that the various WP_DEBUG flags are set to true.
  • Make sure to check that things are as you expect them to be. Don't just do things, make sure that the variables are as expected ... or throw an exception or at least log a notification to the server.

For example, in your code, you do:

if ( $imgUrl != '' ) {
   # do stuff
}

You really should consider

if ( !empty( $imgUrl ) ) {
   # do stuff
} else {
   # unexpected! Maybe throw and handle an exception, add to the server error log, etc.
}

That will save you tons of headaches in the long run.

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.