合并PHP代码

时间:2016-02-06 15:45:38

标签: php wordpress

我想将我的PHP代码的一部分合并到另一部分,我似乎无法弄清楚如何做到这一点。

这是代码:

php composer.phar require mailgun/mailgun-php:~x.x

我想在最后添加第一部分php。 我想说一点:

        <?php
        if (is_single()) {
            while (have_posts()) : the_post();
                // get thumbnail
                wikiwp_get_thumbnail($post);
        ?>
<div>This should stay here in a post</div>
<div>And this should also be visible in this case</div>


       <?php
            endwhile;
        } else {

        ?>

<div>This should stay here in a page</div>

        <?php
        } 
        ?>

进入这一部分:(不破坏代码)

                <?php
                if (is_page()) {
                    while (have_posts()) : the_post();
                        // get thumbnail
                        wikiwp_get_thumbnail($post);
                ?>

结果是: 如果它是一个帖子,它将显示:

           <?php
                endwhile;
            } else {

            ?>

如果它是一个页面,它将显示:

[thumbnail] 
This should stay here 
And this should also be visible in this case

2 个答案:

答案 0 :(得分:1)

这个怎么样?

<?php

if (is_single() || is_page()) {
    while (have_posts()):
        the_post();

        # page + post
        wikiwp_get_thumbnail($post);

        # post only
        if (is_single()) {
?>

<div>This should stay here in a post</div>
<div>And this should also be visible in this case</div>

<?php
        } else if (is_page()) {
?>

<div>This should stay here in a page</div>

<?php
        }
    endwhile;
} else {
?>

<div>This is neither a page or post!</div>
<?php
}
?>

答案 1 :(得分:0)

您提供的代码合并在一起,如下所示:

<?php
    if (is_single()) {
        while (have_posts()) : the_post();
            // get thumbnail
            wikiwp_get_thumbnail($post);
?>
            <div>This should stay here in a post</div>
            <div>And this should also be visible in this case</div>
<?php
        endwhile;
    } else {
        if (is_page()) {
            while (have_posts()) : the_post();
            // get thumbnail
            wikiwp_get_thumbnail($post);
?>
            <div>This should stay here in a page</div>
<?php
        }
    } 
?>

请注意,如果不是单个帖子或页面,这将输出任何内容。