2015-07-20 93 views
1

我正在使用高級自定義字段(ACF)來允許用戶從頁面列表中進行選擇,然後將顯示相應頁面的標題,摘錄和鏈接。ACF和帖子對象ID未能顯示正確的內容

由於某些原因,這是拉取當前帖子的摘錄而不是相關的帖子ID。根據需要標題和固定字詞。我想要一些幫助。

感謝, 傑弗裏

<?php 

/* 
// Adding our custom content output 
/* 
* Loop through post objects (assuming this is a multi-select field) (don't setup postdata) 
* Using this method, the $post object is never changed so all functions need a second parameter of the post ID in question. 
*/ 

add_action('genesis_entry_content', 'genesis_project_list', 10, 2); 
add_action('genesis_post_content', 'genesis_project_list', 10, 2); 

// The Custom Content output function 
function genesis_project_list() { 
$post_objects = get_field('acf_selected_projects'); 

if($post_objects): ?> 
<ul style="list-style:none;"> 
<?php foreach($post_objects as $post_object): ?> 
    <li style="list-style:none;"> 
     <h3><a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a></h3> 
     <span><?php echo get_the_excerpt($post_object->ID); ?></span> 
     <a href="<?php echo get_permalink($post_object->ID); ?>">Read more...</a> 

    </li> 
<?php endforeach; ?> 
</ul> 
<?php endif; 

} 
genesis(); 
+0

請張貼'get_the_excerpt()'函數的代碼,因爲它可能是出現問題的地方,上面的代碼似乎看起來不錯。 –

+0

剛發現'get_the_excerpt()'是一個內置的Wordpress函數,我錯誤地認爲它是你網站的一部分)) –

+0

@ mik-t是的,它是核心功能。我仍然不明白爲什麼我的原始代碼不起作用。令人高興的是,賈裏德有另一種解決方案。 – jeffreyd00

回答

1

我不認爲get_the_excerpt()接受一個參數,所以你不能傳遞$post_object->ID獲得該職位的摘錄。您必須編寫自己的自定義函數來創建摘錄。這裏是我以前用過的一些示例代碼(添加到您的functions.php):

function custom_excerpt($str,$length=40,$append='...'){ 
    $pieces=explode(' ',strip_tags($str)); 
    $excerpt=implode(' ',array_slice($pieces,0,$length)); 
    if(count($pieces)>$length) 
     $excerpt.=$append; 
    return $excerpt; 
} 

,然後在模板:

<span><?php echo custom_excerpt($post_object->post_content); ?></span> 
+0

完美運作。謝謝! 只是爲了滿足我的好奇心,有沒有可能有一個更簡單的方法來做到這一點,而無需添加代碼到functions.php?我問這是因爲它早些時候爲我工作,我沒有什麼比獲取摘錄更多。 – jeffreyd00

+0

試試把這個放在你的模板裏:'<?php echo implode('',array_slice(explode('',strip_tags($ post_object-> post_content)),0,40))。'...'; ?>' – Jared

+0

謝謝,我要選擇1號門。 :) – jeffreyd00