如何只显示一个级别子类别?

时间:2016-11-10 08:28:37

标签: php wordpress categories

  

我正在建立一个房地产网站,所以我有很多类别3到4   级别子类别,但我的要求是只显示一个级别子级   父类别的类别,例如 sub1,sub2和sub3 只能看到我有这样类别的结构

- cat1
    - sub1
       - secondsub1
- cat2
    - sub2
      - secondsub2
- cat3
   - sub3
      - secondsub3



$categories = get_terms(
                    'category', 
                    array('hide_empty' => 0,'parent' => 0,'number' =>3,'order'=> 'ASC') 
                        );
foreach ($categories as $category) {
            $catName= $category->term_id;

<?php echo get_cat_name( $catName ); // this is the main category ?>

<!-- subcategory code starts here-->

        $args = array(
                    'type' => 'post',
                    'child_of' => $catName,
                    'parent' => get_query_var(''),
                    'orderby' => 'name',
                    'order' => 'ASC',
                    'hide_empty' => 0,
                    'hierarchical' => 1,
                    'exclude' => '',
                    'include' => '',
                    'number' => '5',
                    'taxonomy' => 'category',
                    'pad_counts' => true );
                    $categories = get_categories($args);
                    foreach($categories as $category) {
<a href="<?php echo get_category_link( $category->term_id )?>" title="View posts in <?php echo $category->name?>"><?php $category->name;?></span><!--these are the sub category-->
 }
}
  

结果我得到 cate1 sub1-&gt; secondsub1 因为    secondsub1 也是 cat1 的孩子,但我只想 sub1 我该如何实现?任何建议

2 个答案:

答案 0 :(得分:3)

使用parent代替child_of

  

parent(int | string)用于检索直接子项的父项ID。

来源:#parameters

答案 1 :(得分:2)

你可以使用wp_list_categories((Developer Wordpress Reference)来获取每个类别的子类别。我不确定这种用法的效率,但试一试。

所以在你的代码中:

public class MainActivity extends Activity implements View.OnClickListener {

    EditText phoneTo;
    Button btn,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btn10,btn11,btn12,btn99;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        phoneTo = (EditText) findViewById(R.id.editText2);
            btn = (Button)findViewById(R.id.button);
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);
        btn4 = (Button)findViewById(R.id.btn4);
        btn5 = (Button)findViewById(R.id.btn5);
        btn6 = (Button)findViewById(R.id.btn6);
        btn7 = (Button)findViewById(R.id.btn7);
        btn8 = (Button)findViewById(R.id.btn8);
        btn9 = (Button)findViewById(R.id.btn9);
        btn10 = (Button)findViewById(R.id.btn10);
        btn11 = (Button)findViewById(R.id.btn11);
        btn12 = (Button)findViewById(R.id.btn12);
        btn99 = (Button)findViewById(R.id.btn99);
        phoneTo.setSelection(phoneTo.getText().length());



       btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                makePhoneCall(view);
            }
        });


    }

    public void makePhoneCall(View view) {

        try {
            String number = phoneTo.getText().toString();
            Intent phoneIntent = new Intent(Intent.ACTION_CALL);
            phoneIntent.setData(Uri.parse("tel:" + number));
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                return;
            }
            startActivity(phoneIntent);


        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(MainActivity.this,
                    "Call failed, please try again later!", Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.btn1:
                phoneTo.setText("1");
                break;
            case R.id.btn2:
                phoneTo.setText("2");
                break;
            case R.id.btn3:
                phoneTo.setText("3");
                break;
            case R.id.btn4:
                phoneTo.setText("4");
                break;
            case R.id.btn5:
                phoneTo.setText("5");
                break;
            case R.id.btn6:
                phoneTo.setText("6");
                break;
            case R.id.btn7:
                phoneTo.setText("7");
                break;
            case R.id.btn8:
                phoneTo.setText("8");
                break;
            case R.id.btn9:
                phoneTo.setText("9");
                break;
            case R.id.btn10:
                phoneTo.setText("0");
                break;
            case R.id.btn11:
                phoneTo.setText("*");
                break;
            case R.id.btn12:
                phoneTo.setText("#");
                break;
            case R.id.btn99:
                String str=phoneTo.getText().toString();
                if (str.length() >1 ) {
                    str = str.substring(0, str.length() - 1);
                    phoneTo.setText(str);
                }
                else if (str.length() <=1 ) {
                    phoneTo.setText("");
                }
                break;

        }

    }
}

经过一些阅读后我找到了其他解决方案,所以你会使用:

<!-- subcategory code starts here-->
wp_list_categories( array(
    'child_of'    => $category->term_id,
    'depth'       => 1    
) );

还要注意使用&#39; $ categories&#39;两次,这可能会有点混乱。