ACF和帖子对象ID无法显示正确的内容

时间:2015-07-20 18:49:45

标签: php wordpress object post advanced-custom-fields

我使用高级自定义字段(ACF)允许用户从页面列表中进行选择,然后显示相应页面的标题,摘录和链接。

出于某种原因,这是拉动当前帖子的摘录而不是相关的帖子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();

1 个答案:

答案 0 :(得分: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>