如何在Wordpress上为主页自定义the_content()?

时间:2012-11-28 00:08:40

标签: php wordpress function customization

我正在尝试自定义wordpress主页,确切地说是the_content()函数。 我正在尝试使用的网站是http://sportsponsorizzazioni.com/

如果在关于“循环”的文件中我使用“ the_content()”功能,它会在主页上为我提供完整文章的列表,带有图像我已经包含在帖子正文和我希望格式化的文本中。 以另一种方式使用函数“the_excerpt()”,只显示摘录文本,如5行,不带任何图像。

我只想在我的主页上显示文章的第一部分,后面有“阅读更多”按钮和图像,但是我在the_content('')上做了子串操作,它只给了我前X个字符,但没有图像...即使在我的帖子中标签位于帖子的开头。

我需要一些自动的东西,不能在每一个帖子上添加“缩略图”或“特色图片”,有没有办法修改“the_content('')”让我举例来说只有第一个我的帖子中有30个单词的图片? 或者(更好)有没有办法在“the_excerpt()”中包含我文章中编码的图像?

非常感谢!

2 个答案:

答案 0 :(得分:0)

plugin完全符合您的要求。有关从帖子内容中提取图片的信息,请参阅常见问题解答部分中的item number 4

答案 1 :(得分:0)

如果您不想使用插件解决方案, 1.为帖子添加自定义字段,例如使用后映像名称和图像URL值(尝试对所有帖子使用自定义字段)。 2.在home.php中包含类似下面的代码

<?php if(have_post()):

  while(have_post()):
   the_post(); ?>

   <div id="<?php the_ID();?>" <?php post_class();?>>
      <!--output the excerpt-->
            <?php the_excerpt(); ?>
        <!--access the image source as below-->
         <?php $img_src=get_post_meta($post->ID,'post-image',$single=true); ?>

          <!--check if $img_src is not empty, in case there will be 
          a post without post-image custom field.
          -->
          <?php if($img_src !==''): ?>
          <img src="<?php $img_src;?>" alt="<?php the_title();?>"      class="home_image" />
                        <?php endif;?>
                          <!--whenever there is a post with no image, you may like 
                             to add else logic here and do something-->
     </div>

   <?php endwhile; else: ?>

   <!--some error message-->

   <?php endif; ?>