在wordpressurl中添加第三个参数


add third parameter to wordpress url

我有urlhttp://domain.com/real-estate-news/phuket-luxury-property-in-high-demand-ms其中"房地产新闻"是类别,"需求旺盛的普吉豪华房产ms"是帖子名称。当我打印$wp_query->query_vars['name']时;它给出了post-slug,$wpquery->queryvars['category_name]给出了类别slug。

我想生成一个新的urlhttp://domain.com/real-estate-news/phuket-luxury-property-in-high-demand-ms/xyz

并且想要像一样在查询var中获得xyz

    echo $wp_query->query_vars['name']; // print "phuket-luxury-property-in-high-demand-ms"
    echo $wp_query->query_vars['category_name']; // print "real-estate-news"
    echo $wp_query->query_vars['section']; //print "xyz"

如何在wordpress中添加部分查询var请帮助我

这是自定义帖子类型吗?如果是,你有更多的选择。在register_post_type()的数组中,您可以输入"rewrite"的参数来更改段塞名称,并破解该特定CPT的特定重写规则。由于这个项目的复杂性,我把它搁置了。如果你能找到答案,我很乐意听到。这是我对此事的笔记。

register_post_type(array(
//options
'rewrite'    => array( 
'slug'       => 'beach', //a slug used to identify the post type in URLs.
'with_front' => false,
'feed'       => true,  
'pages'      => true 
), 

dreamdare.org

shibashake.com1

shibashake.com2

小心wp.tutsplus——作者自己经常在那里提供错误信息。

wp.tutsplus.com

我设法做到这一点的方法是添加一个重写规则。

这看起来与自定义post-type创建的非常相似,但也接收第三个参数。所以在你的情况下,重写规则看起来像这个

add_rewrite_rule( 
  // post-type      | post-slug          | section           
  '^real-state-news/([^/]+)(?:/([0-9]+))?/([^/]+)/?$',
  //                                                    | Here the section is added
  'index.php?post_type=real-state-news&name=$matches[1]&section=$matches[3]',
  'top'
);
//You then need to add a tag to it
add_rewrite_tag('%section%','([^&]+)');
// At this point, you should be able to grab that param
get_query_var('section')