2013-10-08 26 views
0

我想我的主頁顯示我的最新帖子,這是我的投資組合項目。在這些項目縮略圖下方,我已經獲得了我使用高級自定義字段中的Repeater插件來抓取的靜態內容。我無法全部在同一頁面上工作......要麼讓博客工作,要麼讓ACF工作。從來都不是因爲需要成爲指定的靜態頁面而需要成爲帖子頁面。我不明白如何將它們結合在一起,並使其顯示爲一頁。帶有最新帖子和高級自定義字段的WordPress主頁 - 如何?

我已經嘗試過在閱讀面板中設置.. 我使用front-page.php 我已經在WP層次等 我使用模板試着讀了嘗試...... 我嘗試wp_reset_postdata();,我在堆棧溢出的其他地方閱讀。

我必須在閱讀面板中使用什麼設置。我需要使用模板文件嗎?

下面是我正在使用的代碼,我已經在模板和不同文件之間拆分了代碼,但只是爲了方便在這裏閱讀它的所有內容(也許這是正確的方法,無論如何, t知道..)

<!-- The posts/portfolio items --> 

<?php get_header(); ?> 
<div> 
<?php if(have_posts()) : ?> 
    <ul> 
    <?php while (have_posts()) : the_post(); ?> 
     <li> 
      <!-- Permalink,title and post thumbnail here (omitted) --> 
     </li> 
    <?php endwhile; ?> 
    </ul> 
    <?php else: ?> 
<h2>No Posts found</h2> 
<?php endif; ?> 
</div> 


<!-- Now for the ACF Stuff --> 

<?php if(get_field('care_list')): ?> 
    <?php while(has_sub_field('care_list')): ?> 

     <div class="grid_2 what-i-care-about gap"> 
      <div class="important-img-container"> 
      <?php the_sub_field('care_list_image'); ?> 
     </div> 
      <h3><?php the_sub_field('care_list_title'); ?></h3> 
     </div> 
    <?php endwhile; ?> 
<?php endif; ?> 

<?php get_footer(); ?> 

請幫助一個沮喪的學習者!提前致謝。

+0

好的我已經修復了它,但我認爲它可以改進?我將我所有的項目添加到名爲Portfolio的類別,並在if(have_posts)之前加入了這個:'<?php query_posts(array('category_name'=>'Portfolio','posts_per_page'=> -1)); ?>'。然後在循環之後我使用了'wp_reset_query();'。顯然,這種方式並不是由WordPress推薦的。 –

回答

0

它看起來像你將需要添加您的「主頁」(一個與它的ACF中繼器)的帖子ID的get_field()功能,像這樣:

<?php $post_id = **post_id_of_your_homepage_here**; ?> 
<?php if(get_field('care_list', $post_id)): ?> 
<?php while(has_sub_field('care_list')): ?> 

    <div class="grid_2 what-i-care-about gap"> 
     <div class="important-img-container"> 
     <?php the_sub_field('care_list_image'); ?> 
    </div> 
     <h3><?php the_sub_field('care_list_title'); ?></h3> 
    </div> 
<?php endwhile; ?> 

這是因爲$post_id參數默認爲由wordpress提出的當前帖子,這意味着ACF正在查找您顯示的最後一個Portfolio項目/帖子上的轉發器。如果您將$post_id參數設置爲您主頁的ID,則ACF將改爲在該頁面上查找中繼器。

來源:http://www.advancedcustomfields.com/resources/functions/get_field/#parameters

+0

@ Allan-Crabtree,這可能有助於...在wordpress安裝的首頁使用此代碼,您可以按照以下步驟操作:1)創建新頁面。 2)進入設置 - >閱讀並選擇新頁面作爲首頁。 3)創建一個[頁面模板](http://codex.wordpress.org/Pages),並添加你的代碼。 4)轉到您設置爲首頁的頁面的頁面編輯器,然後在頁面屬性下選擇您的新模板。 –

0

如果我理解正確的話,你有一大堆的職位,你想顯示他們與自己的主頁標題和縮略圖後的列表,然後顯示自定義字段你」已分配到帖子列表下的主頁?

第1步:通過複製page.php文件,更改名稱homepage.php並加入這個要頂一個新的頁面模板:

<?php 
/* 
Template Name: Homepage 
*/ ?> 

第2步:克里特島被稱爲「首頁」 WordPress的頁面並在頁面創建工具右側邊欄的屬性模塊中選擇「主頁」作爲您的頁面模板。

第3步:在您的閱讀設置中,將首頁從帖子頁更改爲「主頁」。現在您的主頁是您的「主頁」頁面。

第4步:在你的新頁面模板homepage.php上設置這樣的完整代碼。它會輸出你的帖子列表,然後是你的頁面自定義字段:

<?php get_header(); ?> 

<?php $the_query = new WP_Query($args); 

<?php if ($the_query->have_posts()) : ?> 
    <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> 
     <h2><?php the_title(); ?></h2> 
     <?php the_post_thumbnail(); ?> 
    <?php endwhile; ?> 
    <?php wp_reset_postdata(); ?> 
<?php endif; ?> 

<?php if(get_field('repeater_field_name')): ?> 
    <?php while(has_sub_field('repeater_field_name')): ?> 
     <?php the_sub_field('sub_field_1'); ?> 
    <?php endwhile; ?> 
<?php endif; ?> 

<?php get_footer(); ?> 
相關問題