试试这个。。。它至少应该为你指明正确的方向。请注意,在不知道您的功能/选项是什么的情况下very hard 给出一个好的答案。
<?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 ›</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 ›</a></span></div>\' : \'\') . \'\\\', thumb : \\\'\' . $imgUrl . \'\\\', url : \\\'\\\'},\' . "\\n";
}
}
}
}
}
} catch ( Exception $e ) {
echo "<p>A fatal error has occurred...</p><pre>" . print_r($e, true) . "</pre>";
}
是的,这是黑客的。。。但是通过做一系列的回音陈述,你应该确切地看到你所期待的事情没有发生。
正如Howdy所指出的,确保打开调试标志,并以这种方式检查问题。
如果您至少没有运行PHP 5,那么请删除周围的try/catch块。
就个人而言,如果您的代码有问题,我建议您:
确保将各种WP\\U调试标志设置为true一定要检查事情是否如你所期望的那样。不要只做事情,确保变量符合预期。。。或向服务器抛出异常或至少记录通知例如,在代码中,您可以执行以下操作:
if ( $imgUrl != \'\' ) {
# do stuff
}
你真的应该考虑
if ( !empty( $imgUrl ) ) {
# do stuff
} else {
# unexpected! Maybe throw and handle an exception, add to the server error log, etc.
}
从长远来看,这会让你省去很多头痛的事。