【问题标题】:How to customize the_content() on Wordpress for the homepage?如何在 Wordpress 上为主页自定义 the_content()?
【发布时间】:2012-11-28 00:08:40
【问题描述】:

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

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

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

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

非常感谢!

【问题讨论】:

  • 你试过什么?您是否搜索过成千上万的 wordpress 插件,这些插件可以进行各种形式的显示修改?

标签: php wordpress function customization


【解决方案1】:

这个plugin 完全符合您的要求。请参阅常见问题部分中的item number 4,以从帖子内容中提取图像。

【讨论】:

  • 就是这样!正是我正在寻找的!
【解决方案2】:

如果您不想使用插件解决方案, 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; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 2012-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多