0

Wordpress 用語のカスタム リンクを作成しようとしています。次のことを行いました。

<?php $terms = get_the_terms( $post->ID, 'blog' );                  
if ( $terms && ! is_wp_error( $terms ) ) : 
    $blog_links = array();
    $blog_slugs = array();

    foreach ( $terms as $term ) {
        $blog_links[] = $term->name;
    }

    foreach ( $terms as $termslug ) {
        $blog_slugs[] = $termslug->slug;
    }   

    $blog = join( ", ", $blog_links );
    $blogs = join( ", ", $blog_slugs ); 
?>

<a href="<?php bloginfo('url'); ?>/blog/<?php echo $blogs; ?>"><?php echo $blog; ?></a>
<?php endif; ?>

これにより、次の URL が作成されます。

http://www.domain.com/blog/news,%20guest-blogs

リンクのテキストは次のようになります (つまり、すべて 1 つのリンクになっています - スクリーンショットを参照してください)。

ここに画像の説明を入力

どちらが近いです!実際には、各用語をリンクに分割して (間にカンマを入れて)、URLをhttp://www.domain.com/blog/newsおよびhttp://www.domain.com/blog/guest-にしたいと考えています。ブログ. foreach各リンクを個別に出力するa が欠落していると思います。

誰かが最後のビットを正しくするのを手伝ってくれますか?

4

2 に答える 2

1

単純に使いやすいかもしれません

<?php echo get_the_term_list( $post->ID, 'blog', '', ', ', '' ); ?>


あなたのコードに何か問題があります...おそらくこのようなものです...

<?php $terms = get_the_terms( $post->ID, 'blog' );                  
if ( $terms && ! is_wp_error( $terms ) ) : 
    $blogs = array();

    foreach ( $terms as $term ) {
        $blogs[] = '<a href="' . get_bloginfo('url') . '/blog/'. $term->slug .'">' . $term->name . '</a>';
    }    

    $blog_links = join( ", ", $blogs);

    echo $blog_links;

endif; ?>
于 2013-01-15T09:44:44.963 に答える
0

コードは出力要素を1つだけ提供aします。このスニペットを変更して、の各要素が正しいhrefとアンカーを持つ要素を$terms生成するようにする必要があります。aこれはおそらく1つの解決策です

<?php 
$terms = get_the_terms( $post->ID, 'blog' );                  
if ( $terms && ! is_wp_error( $terms ) ) : 
    $output = array();
    foreach($terms as $term):
        // Use sprintf to quickly modify the template
        $output[] = sprintf("<a href='%s'>%s</a>", get_bloginfo('url') . '/blog/' . $term->slug, $term->name);
    endforeach;
    echo implode(', ' $output); // This is the result
endif; 
?>
于 2013-01-15T09:55:47.187 に答える