如何在每次加载页面时加载php代码?

如何在每次加载页面时加载php代码?,php,wordpress,Php,Wordpress,我在WordPress网站上工作。我使用mycred插件,因此任何用户都有积分余额(积分可用于购买特权。下面的php代码可用于扣分 $user\u id=get\u current\u user\u id(); mycred_减法(‘惩罚’,$user_id,-10,‘点数惩罚’); 我如何在用户每次访问特定页面时执行它?因此,对于页面上的每次访问,用户都会失去分数(即使他刷新了,他也会失去分数)任何想法?提前感谢。将您的代码放入函数中,如果您创建了页面模板,则在特定页面上调用该代码 e、 g

我在WordPress网站上工作。我使用mycred插件,因此任何用户都有积分余额(积分可用于购买特权。下面的php代码可用于扣分

$user\u id=get\u current\u user\u id();
mycred_减法(‘惩罚’,$user_id,-10,‘点数惩罚’);

我如何在用户每次访问特定页面时执行它?因此,对于页面上的每次访问,用户都会失去分数(即使他刷新了,他也会失去分数)任何想法?提前感谢。

将您的代码放入函数中,如果您创建了页面模板,则在特定页面上调用该代码

e、 g


在所需页面上调用此函数。

将代码放入函数中,如果已创建页面模板,则在特定页面上调用该代码

e、 g


在您想要的页面上调用此函数。

您可以挂接到“pre\u get\u posts”操作。每当用户请求或查询某个内容时,此操作都会运行。这在$query变量中提供给您

function deduct_points($query){
    //this is standard practice
    if(!is_main_query()){
        return $query;
    }

    $user_id = get_current_user_id();
    //EXAMPLES
    //custom post type
    if($query->post_type === 'my custom post type') {
        //run your function
        mycred_subtract( 'penalty', $user_id, -10, 'Points penalty' );
    }

     //any page or post
     if($query->post_type === 'page' || $query->post_type === 'post'){
         //run your function
         mycred_subtract( 'penalty', $user_id, -10, 'Points penalty' );
     }

    //specific page
    if($query->pagename === 'use the page slug here' ){
          //run your function
    }
    //always return the $query
    return $query;
}
add_action('pre_get_posts', 'sjdh_deduct_points', 1);

您可以连接到“pre_get_posts”操作。每当用户请求或查询某个内容时,此操作都会运行。此操作在$query变量中提供给您

function deduct_points($query){
    //this is standard practice
    if(!is_main_query()){
        return $query;
    }

    $user_id = get_current_user_id();
    //EXAMPLES
    //custom post type
    if($query->post_type === 'my custom post type') {
        //run your function
        mycred_subtract( 'penalty', $user_id, -10, 'Points penalty' );
    }

     //any page or post
     if($query->post_type === 'page' || $query->post_type === 'post'){
         //run your function
         mycred_subtract( 'penalty', $user_id, -10, 'Points penalty' );
     }

    //specific page
    if($query->pagename === 'use the page slug here' ){
          //run your function
    }
    //always return the $query
    return $query;
}
add_action('pre_get_posts', 'sjdh_deduct_points', 1);

你的函数和他已经写的完全一样,他的请求有什么变化?你的函数和他已经写的完全一样,他的请求有什么变化?但是我想问一下如何获得页面slug?你可以去“编辑”在Gutenberg编辑器中,如果您在标题上单击/悬停,您将看到永久链接出现。slug是URL上的最后一个字,但我想问一下如何获取页面slug?您可以转到“编辑”在Gutenberg编辑器中,如果你点击/悬停在标题上,你会看到永久链接出现。slug是url上的最后一个字