当查看包含密钥的帖子时,我试图保存帖子元userid:x
这个信息让我知道当前用户是否读过这篇文章(之后在侧栏中发出通知)。
我通过在single.php
紧接着the_content();
:
<?php if ( in_category( \'oznamy\' )) {
$current_user = wp_get_current_user();
$currentuserid = $current_user->ID;
$towrite = "userid:";
$meta_key = $towrite . $currentuserid;
$meta_value = $post->ID ;
add_post_meta($post->ID , $meta_key, $meta_value, true);
} ?>
它确实保存了当前显示的帖子的meta值,但是它总是为另一篇帖子保存meta值。
将meta值设置为post ID只是为了检查它保存到不同post的内容。它保存不同帖子的ID,但该帖子不会显示在single.php
环
因此,如果我查看前端的post 676,它会保存元键userid:1
和价值676
, 这就是我想要的。然而,它也为key保存了metauserid:1
和价值674
对于post ID674
未显示。
为什么会single.php
甚至触摸不同的ID?
更新:整单。php代码如下:
<?php get_header(); ?>
<?php get_template_part(\'template-part\', \'head\'); ?>
<?php get_template_part(\'template-part\', \'topnav\'); ?>
<!-- start content container -->
<div class="row dmbs-content">
<?php //left sidebar ?>
<?php get_sidebar( \'left\' ); ?>
<div class="col-md-<?php devdmbootstrap3_main_content_width(); ?> dmbs-main">
<?php
//if this was a search we display a page header with the results count. If there were no results we display the search form.
if (is_search()) :
$total_results = $wp_query->found_posts;
echo "<h2 class=\'page-header\'>" . sprintf( __(\'%s Search Results for "%s"\',\'devdmbootstrap3\'), $total_results, get_search_query() ) . "</h2>";
if ($total_results == 0) :
get_search_form(true);
endif;
endif;
?>
<?php
global $current_user;
get_currentuserinfo();
?>
<?php if( is_user_member_of_blog( $current_user->ID ) ): ?>
<?php if( is_user_logged_in() ): ?>
<script>
$("#menu-item-508").addClass("active");
</script>
<?php // theloop
if ( have_posts() ) : while ( have_posts() ) : the_post();
// single post
if ( is_single() ) : ?>
<div <?php post_class(); ?>>
<h2 class="page-header-single"><?php the_title() ;?></h2>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<div class="clear"></div>
<?php endif; ?>
<?php the_content(); ?>
<!-- zapis id usera ktory precital oznam -->
<?php if ( in_category( \'oznamy\' )) {
$current_user = wp_get_current_user();
$currentuserid = $current_user->ID;
$towrite = "userid:";
$meta_key = $towrite . $currentuserid;
$meta_value = $post->ID ;
add_post_meta($meta_value , $meta_key, $meta_value, true);
} ?>
<!-- koniec zapis id usera ktory precital oznam -->
<?php wp_link_pages(); ?>
<?php get_template_part(\'template-part\', \'postmeta\'); ?>
Komentáre:
<?php comments_template(); ?>
</div>
<?php
// list of posts
else : ?>
<div <?php post_class(); ?>>
<h2 class="page-header">
<a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( \'Permalink to %s\', \'devdmbootstrap3\' ), the_title_attribute( \'echo=0\' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
</h2>
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<div class="clear"></div>
<?php endif; ?>
<?php the_content(); ?>
<?php wp_link_pages(); ?>
<?php get_template_part(\'template-part\', \'postmeta\'); ?>
<?php if ( comments_open() ) : ?>
<div class="clear"></div>
<p class="text-right">
<a class="btn btn-success" href="<?php the_permalink(); ?>#comments"><?php comments_number(__(\'Leave a Comment\',\'devdmbootstrap3\'), __(\'One Comment\',\'devdmbootstrap3\'), \'%\' . __(\' Comments\',\'devdmbootstrap3\') );?> <span class="glyphicon glyphicon-comment"></span></a>
</p>
<?php endif; ?>
</div>
<?php endif; ?>
<?php endwhile; ?>
<?php posts_nav_link(); ?>
<?php else: ?>
<?php get_404_template(); ?>
<?php endif; ?>
<?php else:
wp_die(\'Unauthorized.\');
endif; ?>
<?php else:
wp_die(\'Unauthorized.\');
endif; ?>
</div>
<?php //get the right sidebar ?>
<?php get_sidebar( \'right\' ); ?>
</div>
<!-- end content container -->
<?php get_footer(); ?>