2014-01-30 32 views
0
<?php 
$the_query = new WP_Query(array( 
    'post_type' => 'needs', 
    'orderby' => 'date', 
    'category_name' => 'volunteers', //name of category by slug 
    'order' => 'DESC', 
    'posts_per_page' => '5')); // how many posts to show 

    // Put into the loop 
    while ($the_query->have_posts()) : 
    $the_query->the_post(); 
    echo '<tr><td>' . get_the_date() . '</td>'; 
    echo '<td>' . get_the_title() . '</a></td></tr>'; 

    endwhile; 



    // Restore original Post Data if needed 
    wp_reset_postdata(); ?> 

我使用此代碼正確顯示志願者類別下的需求標題和日期。 我需要標題鏈接到帖子本身,但我無法獲得固定鏈接來處理此代碼。 有什麼建議嗎?使標題可點擊進入帖子本身。 (WordPress的)

回答

1

如果get_the_title()是爲你工作,實在沒有理由爲什麼這是行不通的:

<?php 
$the_query = new WP_Query(array( 
    'post_type' => 'needs', 
    'orderby' => 'date', 
    'category_name' => 'volunteers', //name of category by slug 
    'order' => 'DESC', 
    'posts_per_page' => '5')); // how many posts to show 

    // Put into the loop 
    while ($the_query->have_posts()) : 
    $the_query->the_post(); 
    echo '<tr><td>' . get_the_date() . '</td>'; 
    echo '<td><a href="' . get_permalink($post->ID) . '">' . get_the_title() . '</a></td></tr>'; 

    endwhile; 



    // Restore original Post Data if needed 
    wp_reset_postdata(); ?> 

嘗試一下!

+0

這是正確的。另一種同樣適用的方法是: echo'​​' . get_the_title() . ''; 你和這個之間有什麼區別?他們都工作。 – 4ndy

+0

很高興爲你效勞! 沒有區別,因爲get_the_ID()只是一個從$ post變量中獲取ID的函數。 $ post-> ID是獲得相同ID的更正確和直接的方式,您可能會更頻繁地看到它。 – jrobson153

1

變化

echo '<td>' . get_the_title() . '</a></td></tr>'; 

echo '<td><a href="'. get_permalink($post->ID) . '">' . get_the_title() . '</a></td></tr>'; 
+0

這種方法實際上鍊接到我當前所在的頁面,而不是郵件本身。 – 4ndy

+0

你剛剛實現..更新 – juz