我刚刚在中读到“如何为wordpress主题创建选项页”wp.tutsplus.com - 我在获取类别方面有问题。
我的问题有两部分
- Part A - Get category slug and display it
本教程中有一个选择选项,如下所示:
$options[] = array(
"section" => "select_section",
"id" => WPTUTS_SHORTNAME . "_select_input",
"title" => __( \'Select (type one)\', \'wptuts_textdomain\' ),
"desc" => __( \'A regular select form field\', \'wptuts_textdomain\' ),
"type" => "select",
"std" => "3",
"choices" => array( "1", "2", "3")
现在,我不想使用1-2-3选项,而是想返回类别slug以向用户显示它-是否有人可以修改此代码,以便它返回在站点上创建的类别的slug?
- Part B - Display category slug in query_posts format
上述代码的典型返回值如下:
<?php echo $wptuts_option[\'wptuts_select_input\']; ?>
我将此代码用于我的查询帖子:
<?php query_posts(\'showposts=1&category_name=news\'); ?>
如果“news”是类别“slug”-现在不是类别的名称,是否有人可以修改代码,以便根据主题选项页中选择的选项获取类别slug?
我一直在解决这个问题,我到处找,但找不到解决方案谢谢
SO网友:Milo
我们从示例中看到,该选项接受一个简单的值数组:
array( "1", "2", "3")
get_categories
返回一个对象数组,因此我们需要重新格式化以使其正常工作:
$categories = get_categories();
$slugs = array();
foreach( $categories as $category )
$slugs[] = $category->slug;
现在我们有一个简单的数组
$slugs
要通过:
$options[] = array(
"section" => "select_section",
"id" => WPTUTS_SHORTNAME . "_select_input",
"title" => __( \'Select (type one)\', \'wptuts_textdomain\' ),
"desc" => __( \'A regular select form field\', \'wptuts_textdomain\' ),
"type" => "select",
"choices" => $slugs
);
SO网友:AddWeb Solution Pvt Ltd
将此代码放入函数中。php
$terms = get_the_terms( $post->ID , \'category\');
if($terms) {
foreach( $terms as $term ) {
$cat_obj = get_term($term->term_id, \'category\');
$cat_slug = $cat_obj->slug;
}
}
然后将var$cat\\u slug设置为“选项”,
然后将var$cat\\u slug设置为“选项”,
$options[] = array(
"section" => "select_section",
"id" => WPTUTS_SHORTNAME . "_select_input",
"title" => _( \'Select (type one)\', \'wptuts_textdomain\' ),
"desc" => _( \'A regular select form field\', \'wptuts_textdomain\' ),
"type" => "select",
"choices" => $cat_slug
);