我正在为我的wordpress博客开发评论插件,我有一些小问题。首先,这是插件代码:
add_action( \'init\', \'create_devicemondo_review\' );
function create_devicemondo_review() {
register_post_type( \'review\',
array(
\'labels\' => array(
\'name\' => \'DeviceMondo Reviews\',
\'singular_name\' => \'DeviceMondo Review\',
\'add_new\' => \'Add New\',
\'add_new_item\' => \'Add New Product Review\',
\'edit\' => \'Edit\',
\'edit_item\' => \'Edit Product Review\',
\'new_item\' => \'New Product Review\',
\'view\' => \'View\',
\'view_item\' => \'View Product Review\',
\'search_items\' => \'Search Product Reviews\',
\'not_found\' => \'No Product Reviews found\',
\'not_found_in_trash\' => \'No Product Reviews found in Trash\',
\'parent\' => \'Parent Product Review\'
),
\'public\' => true,
\'menu_position\' => 15,
\'supports\' => array( \'title\', \'editor\', \'comments\', \'thumbnail\', \'excerpt\', \'custom-fields\' ),
\'taxonomies\' => array( \'\' ),
\'menu_icon\' => plugins_url( \'images/image.png\', __FILE__ ),
\'rewrite\' => array( \'slug\' => \'review\', \'with_front\' => false ),
\'has_archive\' => true
)
);
}
//Creating Meta Box Fields for Custom Post Types
add_action( \'admin_init\', \'my_admin\' );
function my_admin() {
add_meta_box( \'product_review_meta_box\',
\'Product Review Details\',
\'display_product_review_meta_box\',
\'review\', \'normal\', \'high\'
);
}
//Implementation of the display_product_review_meta_box Function
function display_product_review_meta_box( $review ) {
// Retrieve current product type and product rating based on review ID
$product_choice = esc_html( get_post_meta( $review->ID, \'product_choice\', true ) );
$product_type = esc_html( get_post_meta( $review->ID, \'product_type\', true ) );
$product_rating = intval( get_post_meta( $review->ID, \'product_rating\', true ) );
?>
<table>
<tr>
<td style="width: 100%">Editor Choice</td>
<td><input type="text" size="80" name="product_review_choice" value="<?php echo $product_choice; ?>" /></td>
</tr>
<tr>
<td style="width: 100%">Product Type</td>
<td><input type="text" size="80" name="product_review_type" value="<?php echo $product_type; ?>" /></td>
</tr>
<tr>
<td style="width: 150px">Product Rating</td>
<td>
<select style="width: 100px" name="product_review_rating">
<?php
// Generate all items of drop-down list
for ( $rating = 5; $rating >= 1; $rating -- ) {
?>
<option value="<?php echo $rating; ?>" <?php echo selected( $rating, $product_rating ); ?>>
<?php echo $rating; ?> stars <?php } ?>
</select>
</td>
</tr>
</table>
<?php
}
//Registering a Save Post Function
add_action( \'save_post\', \'add_product_review_fields\', 10, 3 );
//Implementation of the add_product_review_fields Function
function add_product_review_fields( $product_review_id, $product_review ) {
// Check post type for product reviews
if ( $product_review->post_type == \'review\' ) {
// Store data in post meta table if present in post data
if ( isset( $_POST[\'product_review_choice\'] ) && $_POST[\'product_review_choice\'] != \'\' ) {
update_post_meta( $product_review_id, \'product_choice\', $_POST[\'product_review_choice\'] );
}
if ( isset( $_POST[\'product_review_type\'] ) && $_POST[\'product_review_type\'] != \'\' ) {
update_post_meta( $product_review_id, \'product_type\', $_POST[\'product_review_type\'] );
}
if ( isset( $_POST[\'product_review_rating\'] ) && $_POST[\'product_review_rating\'] != \'\' ) {
update_post_meta( $product_review_id, \'product_rating\', $_POST[\'product_review_rating\'] );
}
}
}
//Register a Function to Force the Dedicated Template
add_filter( \'template_include\', \'include_template_function\', 1 );
//Implementation of the function
function include_template_function( $template_path ) {
if ( get_post_type() == \'review\' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( \'single-review.php\' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( __FILE__ ) . \'/single-review.php\';
}
}
}
return $template_path;
}
现在这个插件工作得很好,我已经创建了一个带有模板单一评论的页面。我的模板文件夹中的php。此页面显示我使用此插件发布的所有评论的列表。这是该模板的代码:
<div id="primary">
<div id="content" role="main" style="width: 605px!important;">
<?php
$mypost = array( \'post_type\' => \'review\', \'posts_per_page\' => 1,);
$loop = new WP_Query( $mypost );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<article class="brd-bottom" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Display featured image in right-aligned floating div -->
<div class="review-thumb">
<?php the_post_thumbnail( array( 100, 100 ) ); ?>
</div>
<div class="review-short-content">
<ul>
<li id="indv_title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<li>
<div style="float: left">
<?php
$nb_stars = intval( get_post_meta( get_the_ID(), \'product_rating\', true ) );
for ( $star_counter = 1; $star_counter <= 5; $star_counter++ ) {
if ( $star_counter <= $nb_stars ) {
echo \'<img src="\' . plugins_url( \'devicemondo-reviews/images/icon.png\' ) . \'" />\';
} else {
echo \'<img src="\' . plugins_url( \'devicemondo-reviews/images/grey.png\' ). \'" />\';
}
}
?>
</div>
<?php
$key = \'product_choice\';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != \'\') {
echo \'<div class="editor-pick">Editor Choice</div>\';
}
?></li>
<li><?php the_terms( $post->ID, \'device_reviews_device_brand\' , \' \' ); ?> <?php the_excerpt(); ?></li>
</ul>
</div>
<!-- Display product
review contents -->
</article>
<?php endwhile; ?>
</div>
</div>
现在是使用单一评论的页面。php模板还显示了除我添加的链接之外的所有结果
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
不在其页面上显示内容。它再次显示所有评论的列表。我希望有人理解我试图解决的问题,如果你愿意,我可以为你提供链接到我的测试页面。