![](/img/trans.png)
[英]WordPress - 'The Loop' to show all posts, instead posts the title of the home page
[英]Loop through all posts, show attachment if there (WordPress)
我創建了兩個簡單的foreach
循環:一個用於所有帖子,一個用於所有附件。 我希望它顯示每個帖子標題,如果有附件,則顯示該附件。
到目前為止,我有:
$get_posts_array = array( 'posts_per_page' => 3, 'post_type' => 'post' );
$get_posts = get_posts( $get_posts_array );
foreach ($get_posts as $post)
{
the_title();
$get_images_array = array( 'posts_per_page' => 1, 'post_type' => 'attachment' );
$get_images = get_posts($get_images_array);
if ($get_images)
{
foreach ( $get_images as $post )
{
?>
<li> <?php the_attachment_link( $post->ID ) ;?> </li>
<?php
}
}
}
?>
但是,它沒有按預期工作。
它檢索每個帖子標題,但對所有帖子使用相同的第一個附件。
任何幫助都會很棒(我對PHP沒有經驗,所以這可能完全是錯誤的)。
從您提供的代碼中,您要說的是循環瀏覽所有帖子,並針對每個帖子循環瀏覽所有附件並返回第一個附件,然后移至下一個帖子。 您需要具有將附件與帖子綁定的標識符。 示例:您有一個ID為1,3,5,7的帖子數組。 當您遍歷帖子數組時,您希望獲取與當前正在遍歷的特定帖子ID關聯的附件。
希望能有所幫助。 我不精通WordPress語法,但我認為這與...類似
$get_images_array = array( 'posts_per_page' => 1, 'post_type' => 'attachment', 'post_id' => $post->ID );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.