将自定义字段添加到固定链接结构

时间:2015-05-31 23:35:46

标签: php wordpress permalinks

我有一个使用自定义帖子类型'external-projects'的wordpress网站。在该自定义帖子类型中,有一个自定义字段“导演”。我要做的是重写固定链接以包含导演名称。我有这个工作在外部项目,但然后它与其他网站网址混淆。以下404s等是我的代码。任何帮助都会很棒。

add_action('init', 'tdd_add_rewrite_rules', 10, 0);

function tdd_add_rewrite_rules(){
  // Register custom rewrite rules
  global $wp_rewrite;
  $wp_rewrite->add_rewrite_tag('%external-projects%', '([^/]+)', 'external-projects=');
  $wp_rewrite->add_rewrite_tag('%director%', '([^/]+)', 'director=');
  $wp_rewrite->add_permastruct('external-projects', '/%director%/%external-projects%', false);

}



add_filter('post_type_link', 'tdd_permalinks', 10, 3);

function tdd_permalinks($permalink, $post, $leavename){
$no_data = 'no-director';
$post_id = $post->ID;

if($post->post_type != 'external-projects' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
  return $permalink;
  //$var1 = get_post_meta($post_id, 'director', true);
  $var1 = 'test_director';// I'll get this later.
  $var1 = sanitize_title($var1);

  if(!$var1) { $var1 = $no_data; }
  $permalink = str_replace('%director%', $var1, $permalink);
  return $permalink;
}

0 个答案:

没有答案