检查帖子是否已被查看/访问过(Wordpress)

时间:2017-08-28 18:44:17

标签: php wordpress

我只是想知道是否有办法检查帖子是否被访问过,基本上我有一个自定义帖子类型"视频"并且希望当用户查看帖子/视频时,当返回列表页面时,会有一个勾号表示它已被查看..

这可能吗?也许用饼干?

2 个答案:

答案 0 :(得分:0)

您可以在查看时为帖子添加元数据。 在你的模板文件中添加

add_post_meta(get_the_ID(), 'viewed', '1');

或者,如果您的模板不仅适用于视频帖子类型 添加if语句

if(is_singular('videos')){
add_post_meta(get_the_ID(), 'viewed', '1');
}

并检查是否在任何有帖子ID的地方查看

if(get_post_meta(get_the_ID(), 'viewed', true) == '1'){
   // this is viewed post
}

答案 1 :(得分:0)

为此,您可以编写一个简短代码以添加显示页面或帖子的帖子的第一时间。

[set_seen_post]-在您的页面中添加的简码,不返回任何内容

function pref_set_seen_post_func(){
    $post_id   = get_the_ID();

    // using a boolean with acf - you can do the same with wordpress functions
    if ( get_field('visto', $id_post_oferta) == false ) {
        update_field('visto', true, $post_id);
    }
}

add_shortcode( 'set_seen_post', 'pref_set_seen_post_func' );
相关问题