2012-10-01 122 views
0

我有一個Wordpress插件,它顯示Google的鼠標移動片段。 1.0 - 5.0,我想爲所選的值添加一個圖片,在評分之後,但它沒有。在php中添加圖片

到目前爲止,我已經得到它加載圖像,但它不顯示我想要的地方,那就是我卡住的地方。

example pic

這裏是我想補充:

if ($rating == "5.0"){ print("<IMG SRC ="stars/5-0.png>");} 

這裏是插件代碼:

<?php 
$prefix = 'pk_rs_'; 

DEFINE ('PK_RS_DEFAULT_RATING', '-'); 

DEFINE ('PK_RS_DISPLAY', true); 

$pk_rs_meta_box = array(
    'id' => 'pk_rich_snippet_review', 
    'title' => 'Google Rich Snippets: Reviews', 
    'page' => 'post', 
    'context' => 'normal', 
    'priority' => 'high', 
    'fields' => array(
     array(
      'name' => 'Rating', 
      'desc' => 'Product rating, from 1 to 5.', 
      'id' => 'rating', 
      'type' => 'select', 
      'options' => array('-', '1.0', '1.5', '2.0', '2.5', '3.0', '3.5', '4.0', '4.5', '5.0') 
     ), 
     array(
      'name' => 'Author', 
      'desc' => 'Author display name.', 
      'id' => 'author', 
      'type' => 'text', 
      'std' => '' 
     ) 

    ) 
); 

add_action('admin_menu', 'pk_rich_snippet_add_box'); 

// Add meta box 
function pk_rich_snippet_add_box() { 
    global $pk_rs_meta_box; 

    add_meta_box($pk_rs_meta_box['id'], $pk_rs_meta_box['title'], 'pk_rich_snippet_show_box', $pk_rs_meta_box['page'], $pk_rs_meta_box['context'], $pk_rs_meta_box['priority']); 
} 

// Callback function to show fields in meta box 
function pk_rich_snippet_show_box() { 
    global $pk_rs_meta_box, $post; 

    // Use nonce for verification 
    echo '<input type="hidden" name="pk_rich_snippet_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />'; 

    echo '<table class="form-table">'; 

    foreach ($pk_rs_meta_box['fields'] as $field) { 
     // get current post meta data 
     $meta = get_post_meta($post->ID, $field['id'], true); 

     echo '<tr>', 
       '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>', 
       '<td>'; 
     switch ($field['type']) { 
      case 'text': 
       echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />', 
        '<br />', $field['desc']; 
       break; 
      case 'textarea': 
       echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>', 
        '<br />', $field['desc']; 
       break; 
      case 'select': 
       echo '<select name="', $field['id'], '" id="', $field['id'], '">'; 
       foreach ($field['options'] as $option) { 
        echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>'; 
       } 
       echo '</select>', '<br />', $field['desc']; 
       break; 
      case 'radio': 
       foreach ($field['options'] as $option) { 
        echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name']; 
       } 
       break; 
      case 'checkbox': 
       echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />', 
       '<br />', $field['desc']; 
       break; 
     } 
     echo '<td>', 
      '</tr>'; 
    } 

    echo '</table>'; 
} 

add_action('save_post', 'pk_rs_save_data'); 

// Save data from meta box 
function pk_rs_save_data($post_id) { 
    global $pk_rs_meta_box; 

    // verify nonce 
    if (!wp_verify_nonce($_POST['pk_rich_snippet_nonce'], basename(__FILE__))) { 
     return $post_id; 
    } 

    // check autosave 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
     return $post_id; 
    } 

    // check permissions 
    if ('page' == $_POST['post_type']) { 
     if (!current_user_can('edit_page', $post_id)) { 
      return $post_id; 
     } 
    } elseif (!current_user_can('edit_post', $post_id)) { 
     return $post_id; 
    } 

    foreach ($pk_rs_meta_box['fields'] as $field) { 
     $old = get_post_meta($post_id, $field['id'], true); 
     $new = $_POST[$field['id']]; 

     if ($new && $new != $old) { 
      update_post_meta($post_id, $field['id'], $new); 
     } elseif ('' == $new && $old) { 
      delete_post_meta($post_id, $field['id'], $old); 
     } 
    } 
} 

add_filter('the_content', 'pk_rs_add_rich_snippet_to_content', 20); 
function pk_rs_add_rich_snippet_to_content($content){ 
    if (is_single()&&!is_feed()){ 
     global $post; 
     $rating = get_post_meta($post->ID, 'rating', true); 
     $rating = ('-' == $rating && '-' != PK_RS_DEFAULT_RATING) ? PK_RS_DEFAULT_RATING : $rating; 
     if ('-' != $rating){ 
      $title = $post->post_title ; 
      $dateTime = date_create($post->post_date); 
      $date = $dateTime->format("Y-m-d"); 
      $date_only = $dateTime->format("M j"); 
      $author = get_post_meta($post->ID, 'author', true); 
      $author = ('' == $author) ? ucfirst(get_the_author_meta('display_name', $post->post_author)) : $author; 
      $summary = get_post_meta($post->ID, 'summary', true); 
      $description = get_post_meta($post->ID, 'description', true); 
      if (!PK_RS_DISPLAY) { 
       $output = "<div class=\"hreview\" style=\"display:none\">"; 
       $output .= "<span class=\"item\"><span class=\"fn entry-title\">".$title."</span></span>"; 
       $output .= "Reviewed by <span class=\"reviewer\">".$author."</span> on <span class=\"dtreviewed\"> ".$date_only."<span class=\"value-title\" title=\"".$date."\"></span></span>"; 
       $output .= "Rating: <span class=\"rating\">".$rating."</span>";   
       $output .= "<span class=\"summary\">".$summary."</span>"; 
       $output .= "<span class=\"description\">".$description."</span>"; 
       $output .= "</div>"; 
      } else { 
       $output = "<div class=\"hreview\" style=\"display:block; margin: 0 0 10px 10px; padding: 10px; background: #F6F6F6; border: 1px solid #DDD; -moz-border-radius: 3px; border-radius: 3px; font-size: 0.8em; width: 30%; float: right;\">"; 
       $output .= "Title: <span class=\"item\"><span class=\"fn entry-title\" style=\"font-size: 0.8em;font-weight: normal;\">".$title."</span></span><br />"; 
       $output .= "Reviewed by <span class=\"reviewer\">".$author."</span> on <span class=\"dtreviewed\"> ".$date_only."<span class=\"value-title\" title=\"".$date."\"></span></span><br/>"; 
       $output .= "Rating: <span class=\"rating\">".$rating."</span><br/>"; 
       $output .= (0 < strlen($summary)) ? "Summary: <span class=\"summary\">".$summary."</span><br/>" : ""; 
       $output .= (0 < strlen($description)) ? "<p><span class=\"description\">".$description."</span></p>" : ""; 
       $output .= "</div>"; 
      } 

      $content = $output.$content ; 
     } 
    } 
    return $content; 
} 
?> 
+0

你的HTML是凌亂的,沒有什麼做用PHP,你應該重新打 –

+1

你需要檢查你的HTML輸出。確保您在正確的位置輸出圖像。你也可以首先創建一個靜態HTML模擬器,這樣就可以更容易地將你的插件移植到你的插件中。 – hakre

回答

0

對於初學者...這樣的:

if ($rating == "5.0"){ print("<IMG SRC ="stars/5-0.png>");} 

應該是這樣的:

if ($rating == "5.0"){ print('<IMG SRC ="stars/5-0.png">');} 

if ($rating == "5.0"){ print("<IMG SRC =\"stars/5-0.png\">");} 

你要打印引號內的報價,這可能,本身是造成您的問題......你離開了你的圖像源的收盤報價。

0

您的報價不正確。該

if ($rating == "5.0"){ print("<IMG SRC ="stars/5-0.png>");} 

必須

if ($rating == "5.0"){ print("<IMG SRC =\"stars/5-0.png\">");} 

如果IT'IS只是一個錯字,檢查,如果條件運作良好。 如果是這樣檢查相對路徑stars/5-0.png是否正確。

使用Firebug檢查你的HTML