我正在尝试将某种小部件或菜单添加到我所有 Wordpress 站点页面的顶部

I am trying to add some sort of widget or menu to the top of all of my Wordpress sites pages

我目前正在 Wordpress 网站上工作,其中一项任务是创建我能够做到的自定义 post 位置 post 类型。然后我使用我在 functions.php 中编写的过滤器来包含我编写的单独的 locations.php 文件,以显示在我的位置 post 类型页面上。请参阅下面的过滤器:

add_filter('the_content', 'prepend_location_data' );
function prepend_location_data( $content ){
    if( is_singular('location')){

        $html=include("locations.php");
    return $content . $html;
    }
    return $content;
}

然后我能够使用 ACF 插件将自定义字段添加到我的位置 post 并在我的 locations.php 页面上使用 ACF 功能显示它们。我的下一个任务是在所有页面的顶部添加某种小部件或菜单,以根据用户 IP 显示特定的 phone 号码。我能够在我制作的 locations.php 文件中编写一个 php 函数来获取用户的 IP,使用 IP 获取 IP 的城市,然后如果该城市等于包含在我创建的城市自定义字段之一将 return 该城市的 phone 号码。请参阅以下函数:

<?php
function get_the_user_ip() {
    if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
    //Checks if IP is from shared internet
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
    //Checks if IP is passed from proxy
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } 
    else {
    //Most trustworthy source of IP address
    $ip = $_SERVER['REMOTE_ADDR'];
    }
    //Uses ipinfo.io to find location information based on IP address
    $details = json_decode(file_get_contents("https://ipinfo.io/{$ip}"));

    //Returns city value from the details array
    $city=$details->city;

    return apply_filters('wpb_get_ip', $city );
    }


function display_phone(){
    $cityField=get_field('city');
    $phoneField=get_field('phone_number');
    //Assigns the return value of get_the_user_ip to a variable
    $userCity=get_the_user_ip();
    if($userCity==$cityField){
        return ($phoneField);
    }
}

add_shortcode('show_phone', 'display_phone');
?>

问题是短代码只能在位置页面上工作。我意识到我必须以某种方式将 display_phone 函数的 return 值传递回 functions.php 文件,或者可能只是在 functions.php 中执行所有这些逻辑。有人对要尝试的事情或去哪里看有什么建议吗?我可以在 functions.php 中使用 ACF 函数吗?我应该使用 Javascript 函数来执行此操作吗?任何建议表示赞赏。

我也试过对 display_phone 函数进行以下修改,但无济于事。

function display_phone(){
    global $post;
    $current_id = $post->ID;
    $cityField=get_field('city', $post->ID);
    $phoneField=get_field('phone_number');
    //Assigns the return value of get_the_user_ip to a variable
    $userCity=get_the_user_ip();
    if($userCity==$cityField){
        return ($phoneField);
}
}

对display_phone函数的下一次修改:

function display_phone(){
    global $post;
    $current_id = $post->ID;
    $cityField=get_field('city', $current_id);
    $phoneField=get_field('phone_number');
    //Assigns the return value of get_the_user_ip to a variable
    $userCity=get_the_user_ip();
    if($userCity==$cityField){
        return ($phoneField);
}
}

更新: 好的,所以在考虑更多之后我想我需要编写一个函数来循环遍历所有城市和 phone 位置 post 类型的数字字段以查看它们是否匹配return 由 get_the_user_ip 函数编辑的城市。显然,必须修改显示 phone 函数才能执行此操作,我想我必须将这两个函数从 locations.php 中取出,并可能在 header.php 文件中执行此逻辑。有没有人有做类似事情的经验或关于如何做的任何建议?我不太确定如何遍历我的位置 post 类型的字段数据,除非我实际上是在 post 中执行逻辑。我意识到这可以通过使用硬编码的城市数组和 phone 数字来解决,但我试图避免这种情况并改用 ACF 插件。

更新: 所以我将逻辑从 locations.php 页面中取出,并将其移至 functions.php 文件的末尾。现在它正在加载的每个页面的顶部打印正确的 phone 数字,但只有一秒钟。所以看起来这个过程的下一步是把这个数字分配给一个变量并以某种方式通过一个函数,这样我就可以分配给一个简码并在我的整个网站上使用。

<?php 

function get_the_user_ip() {
    if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
    //Checks if IP is from shared internet
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
    //Checks if IP is passed from proxy
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } 
    else {
    //Most trustworthy source of IP address
    $ip = $_SERVER['REMOTE_ADDR'];
    }
    //Uses ipinfo.io to find location information based on IP address
    $details = json_decode(file_get_contents("https://ipinfo.io/{$ip}"));

    //Returns city value from the details array
    $city=$details->city;

    return apply_filters('wpb_get_ip', $city );
    }


$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'location'
));
$userCity=get_the_user_ip();
if( $posts ): 



     foreach( $posts as $post ): 

        $cityField=get_field('city');
        //echo "$cityField";        
        $phoneField=get_field('phone_number');
        //echo "$phoneField";
         if($userCity==$cityField){
             echo ($phoneField);
         }

     endforeach;


    wp_reset_postdata(); 

 endif; 


?>

我认为您还需要提供页面 ID,否则 WordPress 将如何知道要使用哪些位置 phone 号码?所以

get_field('city');

会变成

get_field('city', $post->ID);

您可以在 display_phone 函数中使用以下代码获取当前页面 ID:

global $post;
$current_id = $post->ID;

更新:

看起来你可以开始了。就像你说的,你需要做的就是创建一个短代码来调用它。

function rusty_showphone() {
  $posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'location'
  ));
  $userCity=get_the_user_ip();
  if( $posts ): 
    foreach( $posts as $post ): 
        $cityField=get_field('city');        
        $phoneField=get_field('phone_number');
         if($userCity==$cityField){
             return $phoneField;
         }
     endforeach;
     wp_reset_postdata(); 
   endif; 
  }
add_shortcode('showphone', 'rusty_showphone');

所以我找到了解决办法。如果有人对我如何做得更好有任何建议,那就太好了。

首先,我在我的 wordpress 子主题中创建了一个名为 phone-display.php 的新文件,如下所示:

<?php 

function get_the_user_ip() {
    if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
    //Checks if IP is from shared internet
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
    //Checks if IP is passed from proxy
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } 
    else {
    //Most trustworthy source of IP address
    $ip = $_SERVER['REMOTE_ADDR'];
    }
    //Uses ipinfo.io to find location information based on IP address
    $details = json_decode(file_get_contents("https://ipinfo.io/{$ip}"));

    //Returns city value from the details array
    $city=$details->city;

    return apply_filters('wpb_get_ip', $city );
    }


$posts = get_posts(array(
    'posts_per_page'    => -1,
    'post_type'         => 'location'
));
$userCity=get_the_user_ip();
if( $posts ): 



     foreach( $posts as $post ): 

        $cityField=get_field('city');
        //echo "$cityField";        
        $phoneField=get_field('phone_number');
        //echo "$phoneField";
         if($userCity==$cityField){
             echo ($phoneField);
         }

     endforeach;


    wp_reset_postdata(); 

 endif; 


?>

然后我在希望显示 phone 数字的地方添加了这个自定义 div 元素。

<div id="phone"></div>

然后我将此 JavaScript 代码添加到我的 footer.php 文件的底部。

<script>document.getElementById("phone").innerHTML = '<?php include("phone-display.php"); ?>';</script>