wordpress循环内的代码安排

时间:2013-11-08 12:21:50

标签: php wordpress code-snippets

这是循环的实际代码:

<div id="content" class="span9" role="main">



        <?php if ( have_posts() ){

        // Do we have any posts in the databse that match our query?

        ?>


    <?php 

    get_template_part( 'inc/post-format/single', get_post_format() ); 

    $enable_rtl = of_get_option('enable_rtl', false);



    if(!of_get_option('disable_pagination')){

        if($enable_rtl){

            $next_post = get_adjacent_post( false, '', true ); 

            $prev_post = get_adjacent_post( false, '', false ); 

            }else{

            $next_post = get_adjacent_post( false, '', false ); 

            $prev_post = get_adjacent_post( false, '', true ); 

            }

            ?>

            <?php

            }



        // show related posts by tag

        if(!of_get_option('disable_related_posts')){ 

        get_template_part( 'inc/related-posts' );

          }

        endwhile; // OK, let's stop the post loop once we've displayed it 



    // If comments are open or we have at least one comment, load up the default comment template provided by Wordpress

        if ( comments_open()  )

        comments_template( '', true );



         }else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>



            <article class="post error">

                <h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>

            </article>



        <?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>



    </div><!-- #content .site-content -->

这是我要添加的代码段:

// Parent CPT
if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
    // Normal loop here
    if ( have_posts() ) : while ( have_posts() ) : the_post();
        // Normal loop markup
    endwhile; endif;
} 
// Child CPT
else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
    // Globalize post object
    global $post;

    // Output Parent CPT title and content
    $parent = get_post( $post->post_parent );
    echo '<h1>' . $parent->post_title . '</h1>';
    echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>';

    // Fetch parent CPT comments
    $parent_cpt_comments = get_comments( array(
        'post_id' => $post->post_parent,
        'status' => 'approve'
    ) );
    // Loop through parent CPT comments
    foreach ( $parent_cpt_comments as $comment ) {
        // Output comment list markup here
    }
}
// Grandchild CPT
else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
    // Comment Stats code goes here
}

这是我在实际代码中添加它的方式:

<div id="content" class="span9" role="main">




    <?php if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
        // Normal loop here
        if ( have_posts() ) : while ( have_posts() ) : the_post();

        get_template_part( 'inc/post-format/single', get_post_format() ); 
        endwhile; endif;
    } 
    // Child CPT
    else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
        // Globalize post object
        global $post;

        // Output Parent CPT title and content
        $parent = get_post( $post->post_parent );
        echo '<h1>' . $parent->post_title . '</h1>';
        echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>';

        // Fetch parent CPT comments
        $parent_cpt_comments = get_comments( array(
            'post_id' => $post->post_parent,
            'status' => 'approve'
        ) );
        // Loop through parent CPT comments
        foreach ( $parent_cpt_comments as $comment ) {
            // Output comment list markup here
        }
    }
    // Grandchild CPT
    else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
        // Comment Stats code goes here
    } 

    ?>

            <?php 


                $enable_rtl         = of_get_option('enable_rtl', false);



                if(!of_get_option('disable_pagination')){

                    if($enable_rtl){

                        $next_post = get_adjacent_post( false, '', true ); 

                        $prev_post = get_adjacent_post( false, '', false ); 

                    }else{

                        $next_post = get_adjacent_post( false, '', false ); 

                        $prev_post = get_adjacent_post( false, '', true ); 

                    }

                    ?>

                    <?php

                }



            // show related posts by tag

            if(!of_get_option('disable_related_posts')){ 

                get_template_part( 'inc/related-posts' );

            }

            endwhile; // OK, let's stop the post loop once we've displayed it 



            // If comments are open or we have at least one comment, load up the default comment template provided by Wordpress

            if ( comments_open()  )

                comments_template( '', true );



         }else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>



        <article class="post error">

            <h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>

        </article>



    <?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>



</div><!-- #content .site-content -->

我收到此错误:P ** ass错误:语法错误,此行中意外的T_ENDWHILE **:endwhile;

我确定我添加代码的方式有些不对,但对我来说这很麻烦,因为实际的代码周围有很多php标签......

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我已更改,请尝试以下代码

<div id="content" class="span9" role="main">




<?php if ( 'cpt-parent' == wpse121567_get_cpt_hierarchy() ) {
    // Normal loop here
    if ( have_posts() ) : while ( have_posts() ) : the_post();

    get_template_part( 'inc/post-format/single', get_post_format() ); 
    endwhile; endif;
} 
// Child CPT
else if ( 'cpt-child' == wpse121567_get_cpt_hierarchy() ) {
    // Globalize post object
    global $post;

    // Output Parent CPT title and content
    $parent = get_post( $post->post_parent );
    echo '<h1>' . $parent->post_title . '</h1>';
    echo '<div>' . apply_filters( 'the_content', $parent->post_content ) . '</div>';

    // Fetch parent CPT comments
    $parent_cpt_comments = get_comments( array(
        'post_id' => $post->post_parent,
        'status' => 'approve'
    ) );
    // Loop through parent CPT comments
    foreach ( $parent_cpt_comments as $comment ) {
        // Output comment list markup here
    }
}
// Grandchild CPT
else if ( 'cpt-grandchild' == wpse121567_get_cpt_hierarchy() ) {
    // Comment Stats code goes here


?>

        <?php 


            $enable_rtl         = of_get_option('enable_rtl', false);



            if(!of_get_option('disable_pagination')){

                if($enable_rtl){

                    $next_post = get_adjacent_post( false, '', true ); 

                    $prev_post = get_adjacent_post( false, '', false ); 

                }else{

                    $next_post = get_adjacent_post( false, '', false ); 

                    $prev_post = get_adjacent_post( false, '', true ); 

                }

                ?>

                <?php

            }



        // show related posts by tag

        if(!of_get_option('disable_related_posts')){ 

            get_template_part( 'inc/related-posts' );

        }





        // If comments are open or we have at least one comment, load up the default comment template provided by Wordpress

        if ( comments_open()  )

            comments_template( '', true );



     }else{ // Well, if there are no posts to display and loop through, let's apologize to the reader (also your 404 error) ?>



    <article class="post error">

        <h1 class="404"><?php _e('Page not found', 'outbox'); ?></h1>

    </article>



<?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>