【问题标题】:Shortcode in Wordpress causing php below to not functionWordpress 中的简码导致下面的 php 无法运行
【发布时间】:2017-03-14 02:18:55
【问题描述】:

我编写了一个简码,用于在分配给特定团队成员的同一类别的帖子中显示自定义帖子类型(包含团队成员个人资料)的帖子。短代码完美运行,但我在内容下方的页面模板中的 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; ?>

任何帮助将不胜感激

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

    您可以在帖子上使用 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(); ?>
    

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

    【讨论】:

    • 非常感谢!我知道这将是这样简单的事情。我试过 wp_reset_query();但我把它放在了 return $output;线。在结束后立即放置;正如你所展示的那样完美。再次感谢,我已经为此奋斗了几天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    相关资源
    最近更新 更多