Wordpress帖子类别名称链接

时间:2017-03-04 00:07:41

标签: php html wordpress loops

经过多次谷歌搜索后,我已经完成了这段代码(在PHP上不太好)

final RelativeLayout rl = (RelativeLayout)itemView.findViewById(R.id.rl1);
final RelativeLayout rl2 = (RelativeLayout)itemView.findViewById(R.id.rl2);
public ViewHolder(final View itemView) {
    super(itemView);

    itemView.setTag("f");


        //text layout
        mSenderUsernameView = (TextView) itemView.findViewById(R.id.tv_username);
        mSenderMessageView = (TextView) itemView.findViewById(R.id.et_message);
        mSenderTimeView = (TextView) itemView.findViewById(R.id.tv_time);

        mSenderUsernameView.setOnClickListener(this);
        mSenderMessageView.setOnClickListener(this);
        mSenderTimeView.setOnClickListener(this);
        rl.setOnClickListener(this);
        rl2.setOnClickListener(this);

        }   

@Override
public void onClick(View v) {
    if (v.getId() == rl.getId() || v.getId() == rl2.getId() || v.getId() == mSenderUsernameView.getId() ||
            v.getId() == mSenderMessageView.getId() || v.getId() == mSenderTimeView.getId()) {


        //check if msg is selected or not
        //not selected
        if(itemView.getTag().toString().equals("f")){                rl.setBackgroundResource(R.drawable.selected_msg_color_roundings);
            rl2.setBackgroundResource(R.drawable.selected_msg_color_roundings);
            itemView.setTag("t");
        }
        //selected
        else if(itemView.getTag().toString().equals("t")){
            //Toast.makeText(context, mMessages.get(getAdapterPosition()).getUsername()
            //+ " at " + mPos + " is " + va, Toast.LENGTH_LONG).show();
            rl.setBackgroundResource(R.drawable.msg_color_roundings);
            rl2.setBackgroundResource(R.drawable.msg_color_roundings);

            itemView.setTag("f");

        }
    }

此代码显示Wordpress帖子类别的循环,即时尝试获取每个类别链接,但我仍未获得正确的链接。

提前任何帮助都会很棒。

感谢罗德里戈

2 个答案:

答案 0 :(得分:1)

你非常接近。

您需要针对$category循环中foreach的ID运行get_category_link()

看起来像这样:

<?php
foreach ( $categories as $category ) {
    echo '<a href="' . get_category_link( $category->term_id ) . '"> <h2>' . $category->name . '</h2></a>';
}
?>

所以,总的来说,你的整个代码应该是:

<div>
    <?php
    $args = array(
        'post_type' => 'post'
    );

    $categories = get_categories( $args );
    foreach ( $categories as $category ) {
        echo '<a href="' . get_category_link( $category->term_id ) . '"> <h2>' . $category->name . '</h2></a>';
   }
   ?>
</div>

答案 1 :(得分:0)

使用get_permalink功能。请参阅Wordpress site

上的参考资料