使用ACF字段值的自定义帖子类型的多层永久链接

发布于 2025-02-04 13:05:44 字数 1641 浏览 3 评论 0原文

我有一个客户站点,上面有几种自定义帖子类型 - 地区,县,城市,社区。我想为它们设置动态生成的永久链接结构,如下所示:

/[region]

/[region]/[county]

/[region]/[county]/[county]

/[action]/[region]/[county]/[县]/[县] /[社区]

我为POST_TYPE_LINK添加了一个过滤器,并传递了一个正在更新永久链接的函数:

add_filter('post_type_link', array(&$this, 'rewriteCPTpermalinks'), 10, 2);

/**
   * Rewrite Custom Post Type Permalinks
   *
   * @return void
   */
  public function rewriteCPTpermalinks( $url, $post ){
    global $wp_rewrite;

    if ($post->post_type === 'counties') {
        $regionObject = get_field('region', $post->ID );
        // $url = get_the_permalink($regionObject->ID).$post->post_name;
        $url = str_replace( '%region%', get_the_permalink($regionObject->ID), $url );
    }
    if ($post->post_type == 'cities') {
      $countyObject = get_field('county', $post->ID);
      $regionObject = get_field('region', $countyObject->ID );
      $url = get_the_permalink($regionObject->ID).$countyObject->post_name.'/'.$post->post_name;
    }
    if ($post->post_type == 'communities') {
      $cityObject = get_field('city_post', $post->ID);
      $countyObject = get_field('county', $cityObject->ID);
      $regionObject = get_field( 'region', $countyObject->ID );
      $url = $regionObject->post_name.$countyObject->post_name.'/'.$cityObject->post_name.'/'.$post->post_name;
    }
    
    return $url;

  }

这​​是在更新永久链接,但我将为所有帖子提供404页。我在设置中保存了永久链接>永久链接和尝试flush_rewrite_rules(),但似乎无法使这些工作。

这样做的关键是要使事情自动起作用,而不必从每个帖子中选择自定义分类法。

我不确定我是否在这里使用正确的方法,但感谢任何帮助。

I have a client site with several custom post types - region, county, city, community. I want to set up dynamically generated permalink structures for them as follows:

/[region]

/[region]/[county]

/[region]/[county]/[city]

/[region]/[county]/[city]/[community]

I have added a filter for post_type_link and am passing in a function that is updating the permalinks:

add_filter('post_type_link', array(&$this, 'rewriteCPTpermalinks'), 10, 2);

/**
   * Rewrite Custom Post Type Permalinks
   *
   * @return void
   */
  public function rewriteCPTpermalinks( $url, $post ){
    global $wp_rewrite;

    if ($post->post_type === 'counties') {
        $regionObject = get_field('region', $post->ID );
        // $url = get_the_permalink($regionObject->ID).$post->post_name;
        $url = str_replace( '%region%', get_the_permalink($regionObject->ID), $url );
    }
    if ($post->post_type == 'cities') {
      $countyObject = get_field('county', $post->ID);
      $regionObject = get_field('region', $countyObject->ID );
      $url = get_the_permalink($regionObject->ID).$countyObject->post_name.'/'.$post->post_name;
    }
    if ($post->post_type == 'communities') {
      $cityObject = get_field('city_post', $post->ID);
      $countyObject = get_field('county', $cityObject->ID);
      $regionObject = get_field( 'region', $countyObject->ID );
      $url = $regionObject->post_name.$countyObject->post_name.'/'.$cityObject->post_name.'/'.$post->post_name;
    }
    
    return $url;

  }

This is updating the permalinks but I am getting a 404 page for all posts. I have saved permalinks in Settings > Permalinks and tried flush_rewrite_rules() but cannot seem to get these working.

The key for this is to have things work automatically without having to choose from a custom taxonomy for every post.

I'm not sure if I am using the right approach here but would appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文