Php 从get_post_字段修剪内容
我有一个代码,显示主页上的子页内容:Php 从get_post_字段修剪内容,php,wordpress,Php,Wordpress,我有一个代码,显示主页上的子页内容: <?php /* list sub-pages content */ $portfolioID = $post->ID; $portfolio_sections = array( 'post_type' => 'page', 'child_of' => $portfolioID, 'sort_column' => 'menu_order', 'sort_order' => 'ASC', ); $sec
<?php /* list sub-pages content */
$portfolioID = $post->ID;
$portfolio_sections = array(
'post_type' => 'page',
'child_of' => $portfolioID,
'sort_column' => 'menu_order',
'sort_order' => 'ASC',
);
$sections = get_pages($portfolio_sections);
$hierachical = array();
if ( ! empty($sections) ) {
foreach ( $sections as $section ) {
if ( $section->post_parent == $portfolioID ) {
if ( ! isset( $hierachical[$section->ID]) ) $hierachical[$section->ID] = array();
$hierachical[$section->ID]['child'] = $section;
$hierachical[$section->ID]['grandchildes'] = array();
} else {
if ( ! isset( $hierachical[$section->post_parent]) ) $hierachical[$section->post_parent] = array();
$hierachical[$section->post_parent]['grandchildes'][] = $section;
}
}
foreach ( $hierachical as $id => $hierachical_data ) {
if ( ! isset($hierachical_data['child']) || ! is_object($hierachical_data['child']) ) continue;
echo '<div class="subpagegrid">';
echo '<a href="'. get_permalink($hierachical_data['child']->ID) .'"><h2 class="childpage">' . get_the_title($hierachical_data['child']->ID) . '</h2></a>';
echo apply_filters('the_content', get_post_field('post_content', $hierachical_data['child']->ID));
if ( isset($hierachical_data['grandchildes']) && ! empty($hierachical_data['grandchildes']) ) {
foreach ( $hierachical_data['grandchildes'] as $grandchild ) {
echo '<div class="grandpages">';
echo '<a href="'. get_permalink($grandchild) .'"><h3 class="grandchildpage">' . get_the_title($grandchild) . '</h3></a>';
echo apply_filters('the_content', get_post_field('post_content', $grandchild ));
echo '</div>';
}
}
echo '</div>';
}
}
?>
我想把内容的长度限制在50个字符以内(这样看起来像是摘录),但我不知道如何实现它。我相信这部分内容是从db获取的:apply_filters('the_content',get_post_field('post_content',…但是我如何影响长度呢?你可以尝试使用
MooCutstr($string,$length,…)
你可以尝试php的substr函数:echo apply_filters('the_content',substr get_post_field('post_content',$hierachical_data['child']->ID)),0,50);
此答案可能会为您指出正确的方向…不幸的是,它给出了以下错误:警告:substr()的参数计数错误在…我相信这是PHP版本不兼容…感谢有用的post rnevius,将过滤后的数据转换为变量,然后用wp_trim_单词进行剪切。什么?该函数从何而来?