【问题标题】:Create Output using PHP for Specific Category of a Post使用 PHP 为帖子的特定类别创建输出
【发布时间】:2015-03-06 01:14:54
【问题描述】:

我是 PHP 编码的新手,我正在尝试将此部分从特定类别输出帖子,例如“事件”。我尝试查看不同的示例,但找不到任何似乎适合此 PHP 模板中的编码的内容。我认为这是我缺少的一个简单的补充。

这是我目前的代码:

<?php $args=array( 'posts_per_page'=> 3, 'post_type' => 'post'); $myposts = get_posts( $args ); foreach ($myposts as $post) { ?>

                  <?php $content=$post->post_content; ?>
                  <?php $contnt=substr($content, 0, 150);?>
                  <?php if(has_post_thumbnail()){ ?>
                  <p>
                  <div style="float: left; margin-right: 30px;margin-bottom:10px; width:113px">
                    <?php the_post_thumbnail(); ?>
                  </div>
                  </p>
                  <?php } else { ?>
                  <img src="<?php echo site_url();?>/wp-content/uploads/2014/10/noimageavailable2.jpg" style="float: left; margin-right: 30px;margin-bottom:10px; width:113px">
                  <?php } ?>
                  <h4> <a href="<?php the_permalink(); ?>">
                    <?php the_title(); ?>
                    </a></h4>
                  <p>
                    <?php the_excerpt();?>
                    .....</p>
                  <hr class="line-dots clearbreak">
                  <?php } ?>

任何见解或方向将不胜感激。

谢谢!

【问题讨论】:

    标签: php wordpress post categories posts


    【解决方案1】:

    首先,我建议您从模板中分配逻辑。这将帮助您更好地理解您的代码。

    现在,php 有了模板语法。我可以这样翻译你的代码:

    <?php
    $args=array( 'posts_per_page'=> 3, 'post_type' => 'post');
    $myposts = get_posts( $args );
    foreach ($myposts as $post): ?>
        <?php if(has_post_thumbnail()): ?>
        <p>
            <div style="float: left; margin-right: 30px;margin-bottom:10px; width:113px">
            <?php the_post_thumbnail(); ?>
            </div>
        </p>
        <?php else: ?>
        <img src="<?php echo site_url();?>/wp-content/uploads/2014/10/noimageavailable2.jpg" style="float: left; margin-right: 30px;margin-bottom:10px; width:113px">
        <?php endif; ?>
        <h4><a href="<?php the_permalink(); ?>">
            <?php the_title(); ?>
        </a></h4>
        <p><?php the_excerpt();?></p>
        <hr class="line-dots clearbreak">
        <?php endif; ?>
    <?php endforeach; ?>
    

    现在看起来更容易理解了,不是吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-27
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      相关资源
      最近更新 更多