我已启用帖子视图,我正在尝试an options panel option to allow users to choose if they would like to display post views in each post. 如果他们选择“是”,则会显示帖子视图。如果他们选择“否”,则不会显示。
我希望有人能帮助我创建正确的get\\u选项,根据选项面板中选择的内容显示帖子视图。
我试图获取我的帖子视图,但没有成功:
<?php
if (get_option(\'to_post_views\') != \'No\' and function_exists(\'post_views_count\'))
post_views_count(); ?>
如果未启用选项,我可以通过以下方式获取帖子视图:
<!-- Remember Post Views -->
<?php setPostViews(get_the_ID()); ?>
<!-- Display Post Views-->
<?php echo getPostViews(get_the_ID()); ?>
我的后期视图功能:
// Post views
function getPostViews($postID){
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
return "0 View";
}
return $count.\' Views\';
}
function setPostViews($postID) {
$count_key = \'post_views_count\';
$count = get_post_meta($postID, $count_key, true);
if($count==\'\'){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, \'0\');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
remove_action( \'wp_head\', \'adjacent_posts_rel_link_wp_head\', 10, 0);
选项面板的我的阵列:
array( "name" => "Display post views?",
"desc" => "Choose whether or not to display post views.",
"id" => $shortname."_post_views",
"type" => "select",
"options" => array("Yes", "No"),
"std" => "Yes"),
最合适的回答,由SO网友:Pontus Abrahamsson 整理而成
这就是你的主题函数。php文件。这会在您的editscreen右侧的帖子中添加一个名为“在此帖子上启用帖子视图”的元框:
// Hook into WordPress
add_action( \'admin_init\', \'add_custom_metabox\' );
add_action( \'save_post\', \'wpse_16722_save_post_views_value\' );
/**
* Add meta box
*/
function add_custom_metabox() {
add_meta_box( \'enable_post_views\', __( \'Enable Post Views on this Post\' ), \'wpse_16722_enable_post_views\', \'post\', \'side\', \'low\' );
}
/**
* Display the metabox
*/
function wpse_16722_enable_post_views() {
global $post;
$enable_views = get_post_meta( $post->ID, \'enable_views\', true );
?>
<p>
<input type="checkbox" id="enable-views" name="enable_views" value="1" <?php checked( $enable_views, 1 ); ?> style="margin-right: 10px;">
<label for="enable-views"><?php _e(\'Enable Post Views on this Post\', \'domain\'); ?></label>
</p>
<?php
}
/**
* Process the custom metabox fields
*/
function wpse_16722_save_post_views_value( $post_id ) {
global $post;
if( isset( $_POST[\'enable_views\'] ) ? $_POST[\'enable_views\'] : \'\' ) {
update_post_meta( $post->ID, \'enable_views\', $_POST[\'enable_views\'] );
} else {
delete_post_meta( $post->ID, \'enable_views\' );
}
}
/**
* Get and return the values for the URL and description
*/
function wpse_16722_get_post_views_box() {
global $post;
$enable_views = get_post_meta( $post->ID, \'enable_views\', true );
return $enable_views;
}
它将值保存为您帖子上的元数据。这个
meta_key
是“enable\\u views”,您可以在数据库中的
yourprefix_postmeta
.
当您将值保存在帖子上为true时。您可以简单地在您想要的柜台上查看以下内容:
// Inside the loop add this
// Get the values from "enable_views"
// If true add the views counter
$is_enabled = get_post_meta( $post->ID, \'enable_views\', true );
if( $is_enabled ) {
echo getPostViews( post->ID );
}
Update New 19.11
仅当选项为“是”时才显示视图将其放入单曲中。php
// Get the options
$options = get_option(\'to_post_views\');
// Echo if options is Yes
if( $options == \'Yes\' ) {
echo getPostViews( get_the_ID() );
}