Wordpress中的短代码导致下面的PHP无法正常工作

时间:2017-03-14 02:18:55

标签: php wordpress

我已经编写了一个短代码,用于在分配给特定团队成员的相同类别的帖子中显示自定义帖子类型(包含团队成员个人资料)的帖子。短代码完美地工作,但我在页面模板下面的内容不再起作用的php函数(特别是第一个if语句)。奇怪的是,如果我删除第一个if语句,内容下面的php正确显示

我的短代码是:

function team_embed() {
ob_start();
global $post;
// get the domain page category
$pagecat = get_the_category();
$CategoryName = $pagecat[0]->cat_name;
$slug = $pagecat[0]->slug;
$slug1 = $pagecat[0]->slug;
$exp = '_experience';
$experience = $slug1 .= $exp;
$query = array( 'post_type' => 'team', 'posts_per_page' => -1, 'category_name' => $slug );
$loop = new WP_Query($query);
while ( $loop->have_posts() ) : $loop->the_post();
    $phone = get_post_meta($post->ID, 'Phone Number', true);
    $linkedin = get_post_meta($post->ID, 'Linkedin', true);
    ?>
    <div class="team-embed-wrapper">
        <div class="team-embed">
            <h3><?php the_title(); ?></h3>
                <div class="team-embed-thumbnail">
                    <?php the_post_thumbnail(); ?>
                </div>
            <div class="team-embed-excerpt">
                <?php
                    if ($phone) { ?>
                        Phone:&nbsp;<?php echo $phone; ?><br /> <?php }
                    else {}     
                    if ($linkedin) {
                        echo $linkedin; ?><br /> <?php }
                    else {} ?>
                <br />
                <a href="<?php the_permalink() ?>" class="rm-button">Read Complete Profile</a>  
            </div>
        </div>
        <div class="team-embed-experience">
            <strong><?php echo $CategoryName; ?> domain related experience:</strong><br />
            <p><?php echo get_field( $experience ); ?></p>
        </div>
    </div>
    <div style="clear:both"></div>  
<?php       
endwhile;
$output = ob_get_clean();       
return $output;
}
add_shortcode( 'Team_Embed', 'team_embed' );

我页面模板中页面内容下面的代码(不使用短代码时有效:

<!-- Add closures and Case Studies if available -->
<?php
    // get the category
    $category = get_the_category();
    $firstCategory = $category[0]->cat_name;
?>
<?php if( get_field('tclosure_add') == 'yes' ): ?>
    <h2><?php echo $firstCategory; ?> closures</h2>
    <div class="closure-cat">
    <?php echo do_shortcode("[pt_view id=1723554lt4 cat='$firstCategory' limit=3]"); ?>
    <a class="rm-button" href="/experience">View All Company Closures</a></div>
<?php endif; ?>
<?php if( get_field('case_studies_add') == 'yes' ): ?>
    <h2><?php echo $firstCategory; ?> Case Studies</h2>
    <div class="case-study-embed-wrapper">
        <?php echo do_shortcode("[pt_view id=2c2b5b7ulr cat='$firstCategory' limit=3]"); ?>
    </div>
    <a class="rm-button" href="/representative-engagements/#case-studies">View All Company Case Studies</a>
    <?php endif; ?>

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我认为你可能会搞乱你的主要查询,这会弄乱页面模板代码。

您可以在帖子上使用foreach

<?php foreach( $loop->posts as $post ): ?>
    <!-- Gather info here, remember that the $post is not global so you will have to use functions that can accept a post ID... -->
<?php endforeach;

或者您可以在循环结束时使用wp_reset_query()

<?php while ( $loop->have_posts() ) : $loop->the_post(); 
    // .....
endwhile; wp_reset_query(); ?>

如果它不起作用,请告诉我。