2016-09-26 38 views
0

中获得'注册自定义文章类型是插件领域',我使用foreach()来显示我的模板部分之一中名为content-latest.php的最新文章,它是工作正常之前,我添加wp_trim_words()显示一些摘录到循环,其获取文本输出说“注册自定义帖子类型是一个插件领域...”在每一篇文章。缩略图,标题是正常的,但没有摘录。 the_excerpt()也是如此。使用wp_trim_words并在我的自定义循环中的所有文章摘录

这是我的代码之前添加的摘录(和工作,但没有摘录):

<?php 
    $recent_posts = wp_get_recent_posts(); 
    foreach($recent_posts as $recent){ 
    if($recent['post_status']=="publish"){ 
    if (has_post_thumbnail($recent["ID"])) { 
     echo '<li><div class="media"> 
      <a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" class="media-left">' 
      . get_the_post_thumbnail($recent["ID"], 'thumbnail').'</a> <div class="media-body"> 
      <a href="' . get_permalink($recent["ID"]). '" 
      class="catg_title" style="color:#000000;">' .esc_attr($recent["post_title"]). '</a></div></div></li> '; 
     }else{ 
     //something here.. 
      } 
     } 
    } 
    ?> 

代码之后:

<?php 
     $recent_posts = wp_get_recent_posts(); 
     $trim = wp_trim_words(get_the_content(), 7, '...'); 
     foreach($recent_posts as $recent){ 
     if($recent['post_status']=="publish"){ 
     if (has_post_thumbnail($recent["ID"])) { 
      echo '<li><div class="media"> 
       <a href="' . get_permalink($recent["ID"]) . '" title="'.esc_attr($recent["post_title"]).'" class="media-left">' 
       . get_the_post_thumbnail($recent["ID"], 'thumbnail').'</a> <div class="media-body"> 
       <a href="' . get_permalink($recent["ID"]). '" 
       class="catg_title" style="color:#000000;">' .esc_attr($recent["post_title"]). '<br>' . esc_attr($trim) . '</a></div></div></li> '; 
      }else{ 
      //something here.. 
       } 
      } 
     } 
     ?> 

注意,我分配wp_trim_words$trim,把它作为摘抄。我正在学习过程中,所以我不知道为什么或如何处理。有人可以向我解释一下吗?

回答

0

get_the_content需要一个职位ID工作。

foreach($recent_posts as $recent){ 
    $trim = wp_trim_words(get_the_content($recent['ID']), 7, '...'); 
} 
+0

谢谢你的解释,但我仍然得到同样的结果..请看看这个PIC [这里](http://tinypic.com/view.php?pic=25um793&s=9#。 V-kIs1V96ig)。 我找了个原因,其中一个来自[这里](http://wordpress.stackexchange.com/questions/219745/how-can-i-fix-those-issues-generated-by-the-themecheck-plugin ),我认为这种类型的循环不应该直接添加到模板部分,是吗? – Jjero

+0

你在哪里注册帖子类型? – WordpressDave

+0

你用你自己的主题吗? – WordpressDave

相关问题