簡體   English   中英

使用Posts and Pages插件中的Allow PHP在頁面上進行Wordpress查詢

[英]Wordpress Query on a Page using Allow PHP in Posts and Pages Plugin

它完成了我想要的操作,在頁面的一列中顯示了特定類別的帖子。 現在,它只是標題,但我想鏈接該標題,並且永久鏈接部分不起作用,或者href無效。

[php]
// The Query
$the_query = new WP_Query( 'cat=3' );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul style="list-style:none;">';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . '<a href="the_permalink();">' . get_the_title() . '[/a]'. '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
[/php]

它鏈接到subdomain.domain.com/site/the_permalink(); 而是拉出該帖子的永久鏈接並鏈接到該帖子。

the_permalink(); 返回以回顯您的鏈接。 您只需要返回字符串。 為此,您可以使用get_the_permalink($post->ID); 因為您的永久鏈接函數在echo函數內部。

輸入a-tag時,以HTML開頭,如果將其關閉,則更改為BBCode。

法提赫是對的,您需要使用get_permalink函數。 由於您在循環中,因此無需指定參數( $post->ID )。

另外,您需要像使用get_the_title函數一樣,切換回PHP(這是您的主要問題)。 您的代碼有多個語法問題(括號!)。

該行應如下所示:

echo '<li><a href="'.get_permalink().'">' . get_the_title() . '</a></li>';

了解echoreturn (不僅是)PHP函數之間的區別!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM