【问题标题】:WordPress Loop Takes Custom Meta from other PostsWordPress Loop 从其他帖子中获取自定义元数据
【发布时间】:2016-01-04 11:43:09
【问题描述】:

我使用 Devin Says 的团队帖子类型作为具有自定义元的自定义帖子类型的基础:https://github.com/devinsays/team-post-type。除了语言更新和添加的字段外,我的插件几乎没有变化。

当我输出我的帖子时,循环中后续帖子为空的自定义元值会采用之前帖子的值。所以 - 如果帖子一的“电话”的自定义元设置为“555-555-5555”,而帖子二的“电话”的自定义元未设置,在我的循环中,它会显示“555-555-555” '。

所以 - HTML 输出(例如)看起来像:

<section>
    <h2>Person 1</h2>
    <p>555.555.5555</p>
</section>
<section>
    <h2>Person 2</h2>
    <p>555.555.5555</p>
</section>

...即使数据库中没有第 2 个人的电话号码。

我发现了这个具有相同症状的类似问题,但没有看到对我有帮助的“解决方案” - 所陈述的建议是我已经在做的事情,我看不出方式上有什么重大区别我正在向页面调用数据:Values from custom meta boxes being repeated in posts

有人对这个问题有什么建议吗?

我正在尝试使用以下内容输出我的帖子:

add_shortcode('staff-list', 'jpro_staff_list'); 
function jpro_staff_list($atts) {

extract(shortcode_atts(array(
), $atts)); 

$args = array ( 
    'post_type' => array( 'staff' ), 
    'posts_per_page' => '-1', 
    'orderby' => 'menu_order', 
    'order'   => 'ASC' 
);
$query = new WP_Query( $args );

$staff_list = '<div class="jpro-staff staff-list container">';  

if ( $query->have_posts() ) {                                   
    while ( $query->have_posts() ) {
        $query->the_post(); 

        $post   = get_post($id);
        $postID = $post->ID;

        $image  = get_the_post_thumbnail( $postID, 'medium', array( 'class' => 'aligncenter' ));
        $name   = get_the_title($postID);   
        $bio    = apply_filters('the_content', $post->post_content); 
        $title  = get_post_meta($postID, 'staff_title', true);
        $video  = get_post_meta($postID, 'staff_video', true);
        $email  = get_post_meta($postID, 'staff_email', true);
        $phone  = get_post_meta($postID, 'staff_phone', true);
        $fax    = get_post_meta($postID, 'staff_fax', true);        

        if(!empty($image)){ 
        $output_image = '<div class="staff-list image">'.$image.'</div>';
        }

        if(!empty($name)){
        $output_name ='<h3 class="staff-name staff-list">'.$name.'</h3>';
        }

        if(!empty($bio)){
        $output_bio ='<div class="staff-bio staff-list">'.$bio.'</div>';
        }

        if(!empty($video)){
        $output_video ='
            <div class="staff-video-container">
                <iframe class="staff-video staff-list" src="//www.'.$video.'" width="560" height="315" frameborder="0" allowfullscreen="allowfullscreen">
                </iframe>
            </div>';
            }

        if(!empty($title)){
        $output_title ='<li class="staff-title staff-list">'.$title.'</li>';
        }

        if(!empty($email)){
        $output_email ='
            <a href="mailto:'.$email.'">
                <li class="staff-email staff-list"><i class="fa fa-envelope"></i> '.$email.'</li>
            </a>';
            }

        if(!empty($phone)){
        $output_phone ='
            <a href="tel:'.$phone.'">
                <li class="staff-phone staff-list"><i class="fa fa-phone-square"></i> '.$phone.'</li>
            </a>';
            }

        if(!empty($fax)){
        $output_fax ='<li class="staff-fax staff-list"><i class="fa fa-fax"></i> '.$fax.'</li>';
        }               

            $staff_list .='<section class="jpro-staff staff-list">';
            $staff_list .= $output_name;
            $staff_list .= $output_video;
            $staff_list .= '<ul class="staff-meta staff-list">';
            $staff_list .= $output_image;
            $staff_list .= $output_name;
            $staff_list .= $output_title;
            $staff_list .= $output_email;
            $staff_list .= $output_phone;       
            $staff_list .= $output_fax;
            $staff_list .= '</ul>';
            $staff_list .= $output_bio;             
            $staff_list .= '</section>';
    }
} else {
    $staff_list .='<p>Ooops! No Staff Found.</p>';
}

$staff_list .='</div>';     
wp_reset_postdata();
return $staff_list; 

}

【问题讨论】:

  • 你的简码写得不好,有很多错误。
  • 另外,$id 是未定义的,又一个错误添加到其他许多错误中 ;-)

标签: php wordpress custom-post-type


【解决方案1】:

从技术上讲,您的脚本可以正常工作。因为您在任何时候都不会清除 $output_xyz 变量,您的脚本会采用上一个循环中的值。

要么将您的 if ( !empty (...) ) ... 更改为具有这样的 else 块:

if ( !empty ( $phone ) ) {
    $output_phone = '<a href="tel:'.$phone.'">...</a>';
} else {
    $output_phone = '';
}

或者在每次循环开始时清除变量:

if ( $query->have_posts() ) {                                   
    while ( $query->have_posts() ) {
        $query->the_post();

        $output_image = '';
        $output_phone = '';
        ...
    }
}

【讨论】:

  • 从技术上讲,您的脚本工作正常,不,它没有。它确实有问题,仍然在此之上使用extract()
  • @PieterGoosen 可能是。但这不是他的问题,我没有注意。关于他的问题,剧本完全按照他的吩咐去做。或者换句话说:他应该告诉脚本它不应该做什么,就像在那种特定情况下使用前一个循环中的旧值一样。 :P
  • 完美,马里奥。这确实解决了我的问题。 @PieterGoosen,我看到 WP Codex 的文档不包含 extract() - 当/如果我为此短代码使用属性时,我将使用新技术。关于越野车,您还有其他想法可以让我受益吗? codex.wordpress.org/Shortcode_API#Handling_Attributes
  • 很高兴我能帮上忙。如果您接受答案作为解决方案,那就太好了。谢谢。 :)
  • @JamesRyvenValeii 除了这个答案和$id 中的要点,你应该很高兴。始终打开调试,如果打开调试,页面上应该会出现许多调试错误。 extract() 是一个非常糟糕的功能,已从所有 WorPress 功能中完全删除。你应该有一个最邪恶的列表,上面有query_posts()extract()eval()。永远不要使用任何这些
猜你喜欢
  • 2022-01-19
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多