0

通常のwp_queryを使用して投稿のタイトルを一覧表示しようとしていますが、アイテムのhrefにパーマリンクを追加するだけです。これは私が使用しているコードです。

    <?php $the_query = new WP_Query( 'post_type=artworks_post' );
        // The Loop
        while ( $the_query->have_posts() ) : $the_query->the_post();
            echo '<a rel="' .the_permalink(). '" href="' .the_permalink. ' ">';
            the_title();
            echo '</a>';
        endwhile;

        // Reset Post Data
        wp_reset_postdata();
    ?> 

問題は、コードが機能していないことです。コードは「パーマリンク」という単語を含むhrefを返すだけで、リンク自体は返しません。

私がここで間違っていることは何ですか?

4

3 に答える 3

8

the_permalinkの代わりにget_permalinkを使用してみてください。関数the_permalinkはパーマリンク自体を出力していますが(http://codex.wordpress.org/Function_Reference/the_permalink)、関数get_permalinkはパーマリンク文字列を返します(http://codex.wordpress.org/Function_Reference/get_permalink)。

とにかく単なる提案であり、echoの代わりにprintfを使用してください。そのような、

<?php $the_query = new WP_Query( 'post_type=artworks_post' );
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
        echo '<a rel="' .the_permalink(). '" href="' .the_permalink. ' ">';
        the_title();
        echo '</a>';
    endwhile;

    // Reset Post Data
    wp_reset_postdata();
?> 

要求に応じて、printfの代わりにechoを使用して例を追加します

<?php
    $the_query = new WP_Query( 'post_type=artworks_post' );
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
        echo '<a rel="' .get_permalink(). '" href="' .get_permalink(). ' ">';
        the_title();
        echo '</a>';
    endwhile;

    // Reset Post Data
    wp_reset_postdata();
?> 
于 2012-10-21T12:28:11.057 に答える
1

あなたは括弧を逃しました。

         echo '<a rel="' .the_permalink(). '" href="' .the_permalink(). ' ">';
        the_title();
        echo '</a>';
于 2012-10-21T12:25:21.793 に答える
1

the_permalinkの代わりに使用しました。the_permalink()href

ただし、使用する必要があるのは、です。これは、すぐに使用するのではなく、get_permalink()で使用する値を返します。echoecho

于 2012-10-21T12:25:37.150 に答える