循环中使用自定义字段值的操作

时间:2013-10-02 作者:KalymaStudio

我有一个foreach,用于按术语在一个分类过滤器中显示所有帖子
我有一个用于显示数值的自定义字段<我需要的是对该字段的所有值进行两次数学运算,跨越该学期的所有帖子。

我的意思是:

分类法X有4个术语<每个学期有4个职位
每篇文章都有一个自定义\\u字段
我需要将该术语中的所有custom\\u字段值与该自定义字段的平均值相加。

因此,目前我有:

<?php foreach ( $prod_terms as $prod_term ) {
$prod_query = new WP_Query( array(
 \'post_type\' => \'prod_cientifica\',
 \'prod_area\' => $post_slug,
 \'tax_query\' => array(
    array(
    \'taxonomy\' => \'prod_tipo\',
    \'terms\' => array( $prod_term->slug ),
    \'operator\' => \'IN\',
    \'get\' => \'all\', 
    \'field\' => \'slug\'
    )
)
) );

if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); ?>

<div class="areaItem">
 <h4><?php the_title(); ?></h4>
 <p class="autores"><?php the_field(\'prod_autores\'); ?></p>
 <p class="info"><?php the_field(\'prod_info\'); ?></p>
 <p class="info"><?php the_field(\'prod_fi\'); ?></p>
</div>
<?php endwhile; endif; ?>
我需要的是把所有的prod_fi 每个学期的字段。此外,该字段的平均值也是。

有什么想法吗?

谢谢

--使用代码编辑。

到目前为止,我尝试了几种选择。更接近的是:

<?php
if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); 
$total_fi = array();
$meta_key = \'prod_fi\';//set this to your custom field meta key
$total_fi = $wpdb->get_col($wpdb->prepare
    ("SELECT meta_value FROM wp_postmeta as pm
    INNER JOIN wp_term_relationships as tr ON (pm.post_id = tr.object_id) 
    INNER JOIN wp_term_taxonomy as tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
    WHERE 1
    AND tt.taxonomy = \'prod_tipo\'
    AND pm.meta_key = %s", 
    $meta_key));
    echo \'Total FI \'.array_sum( $total_fi );
    ?>
但是,这一个显示了分类法中所有custom\\u字段的总数量。

这一个,并不是每一项的和的停止,它继续。

$factorI = get_field(\'prod_fi\'); 
if($factorI) {$factor_total += $factorI;}
echo $factor_total;
谢谢你。

我找到了一个棘手的解决办法。我知道这不是正确的方法,如果有人知道正确的方法,我会很高兴的!!!!

<?php
//start the foreach for each term in taxonomy
foreach ( $prod_terms as $prod_term ) {
$prod_query = new WP_Query( array(
    \'post_type\' => \'prod_cientifica\',
    \'prod_area\' => $post_slug,
    \'tax_query\' => array(
         array(
            \'taxonomy\' => \'prod_tipo\',
            \'terms\' => array( $prod_term->slug ),
            \'operator\' => \'IN\',
            \'get\' => \'all\', 
            \'field\' => \'slug\'
            )
         )
) );
?>
<div class="area">
    <h2><?php echo $prod_term->name; ?></h2>
    <?php 
             // First while for fetch the value of "prod_fi" and make the math.
             while ( $prod_query->have_posts() ) : $prod_query->the_post(); 
        $factorI = get_field(\'prod_fi\'); 
        if($factorI) {$factor_total += $factorI;}
                    // count post for average math operation
        $count_posts = $prod_query->current_post + 1;
        $factorP = $factor_total / $count_posts;
    endwhile; ?>
    <p class="areasPublicados"><?php _e(\'Publicados: \', \'twentytwelve\'); ?> <?php echo $count_posts; ?></p>
    <p class="areasFI fleft"><?php _e(\'FI medio: \', \'twentytwelve\'); ?> <?php echo $factorP; ?> </p><p class="areasFI fright"><?php _e(\'FI: \', \'twentytwelve\'); ?> <?php echo $factor_total; ?></p>

    <?php
             //original query to fetch the post in the term
    if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); ?>
    <div class="areaItem">
        <h4><?php the_title(); ?></h4>
        <p class="autores"><?php the_field(\'prod_autores\'); ?></p>
        <p class="info"><?php the_field(\'prod_info\'); ?></p>
            </div>
     <?php endwhile; endif; ?>
</div>
<?php
// Reset math operation of factor_total, so it don\'t continue sum
   $factor_total = null;
   $prod_query = null;
   wp_reset_postdata();
 } //endforeach
?>
再次编辑!

<?php
 // Fetch all post in taxonomy divided by terms.
 foreach ( $prod_terms as $prod_term ) {
    $prod_query = new WP_Query( array(
    \'post_type\' => \'prod_cientifica\',
    \'prod_area\' => $post_slug,
    \'tax_query\' => array(
        array(
        \'taxonomy\' => \'prod_tipo\',
        \'terms\' => array( $prod_term->slug ),
        \'operator\' => \'IN\',
        \'get\' => \'all\', 
        \'field\' => \'slug\'
        )
    )
) );
?>
<?php if($prod_query->have_posts()) : ?>
    <div class="area">
    <h2><?php echo $prod_term->name; ?></h2>
    <?php 
    // Declare total and count before loop
    $factorTotal = 0;
    $factorCount = 0;
    // First loop to fetch prod_fi value
    while ( $prod_query->have_posts() ) : $prod_query->the_post(); 
                $factorI = get_field(\'prod_fi\'); 
        if($factorI) { // Only if it exists
                  $factorTotal += $factorI; // Add it
                  $factorCount++; // Count it
        }
    endwhile; ?>
    <p class="areasPublicados">
       <?php _e(\'Publicados: \', \'twentytwelve\'); ?> <?php echo $factorCount; ?>
    </p>
    <p class="areasFI fleft">
       <?php _e(\'FI medio: \', \'twentytwelve\'); ?> <?php echo $factorTotal; ?> 
    </p>
    <p class="areasFI fright">
       <?php _e(\'FI: \', \'twentytwelve\'); ?> <?php echo ($factorTotal / $factorCount); ?>
    </p>

    <?php
    if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); ?>
        <div class="areaItem">
        <h4><?php the_title(); ?></h4>
        <p class="autores"><?php the_field(\'prod_autores\'); ?></p>
        <p class="info"><?php the_field(\'prod_info\'); ?></p>
        </div>
    <?php endwhile; endif; ?>
</div>
<?php endif; ?>
<?php
// Reset things, for good measure
$factor_total = null;
$prod_query = null;
wp_reset_postdata();
} //enforeach
?>

1 个回复
最合适的回答,由SO网友:Stubbies 整理而成

以下是一种简单的方法:

// First declare total and count before the loop
$total = 0;
$count = 0;

foreach($posts as $post)
{
     if(get_field(\'prod_fi\')){ // If we have a value add it to the total and count it
        $total += get_field(\'prod_fi\');
        $count++;
     }
}

echo \'Count: \'. $count;
echo \'Total Sum: \'. $total;
echo \'Average: \'.($total / $count); // To get the average
编辑:

在您的例子中,如果有$prod\\u查询循环,希望这更有意义:)

// Declare total and count before loop
$factorTotal = 0;
$factorCount = 0;

while ( $prod_query->have_posts() ) : $prod_query->the_post(); 
    $factorI = get_field(\'prod_fi\'); 
    if($factorI){ // Only if it exists
        $factorTotal += $factorI; // Add it
        $factorCount++; // Count it
    } // end if
endwhile;

echo \'Count: \'. $factorCount;
echo \'Total Sum: \'. $factorTotal;
echo \'Average: \'.($factorTotal / $factorCount);

结束

相关推荐

Widgets in the loop if active

我正在尝试将一个小部件作为“第一个帖子”,如果它处于活动状态。。然后让帖子继续。。它可以工作,但第一篇帖子消失了,第二篇帖子成为小部件后的第一行。。看不出我做错了什么。。正如你所看到的,我已经有一个小部件显示在帖子之间。<?php if (have_posts()) : ?> <?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; ?> <?php $i=1;?>

循环中使用自定义字段值的操作 - 小码农CODE - 行之有效找到问题解决它

循环中使用自定义字段值的操作

时间:2013-10-02 作者:KalymaStudio

我有一个foreach,用于按术语在一个分类过滤器中显示所有帖子
我有一个用于显示数值的自定义字段<我需要的是对该字段的所有值进行两次数学运算,跨越该学期的所有帖子。

我的意思是:

分类法X有4个术语<每个学期有4个职位
每篇文章都有一个自定义\\u字段
我需要将该术语中的所有custom\\u字段值与该自定义字段的平均值相加。

因此,目前我有:

<?php foreach ( $prod_terms as $prod_term ) {
$prod_query = new WP_Query( array(
 \'post_type\' => \'prod_cientifica\',
 \'prod_area\' => $post_slug,
 \'tax_query\' => array(
    array(
    \'taxonomy\' => \'prod_tipo\',
    \'terms\' => array( $prod_term->slug ),
    \'operator\' => \'IN\',
    \'get\' => \'all\', 
    \'field\' => \'slug\'
    )
)
) );

if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); ?>

<div class="areaItem">
 <h4><?php the_title(); ?></h4>
 <p class="autores"><?php the_field(\'prod_autores\'); ?></p>
 <p class="info"><?php the_field(\'prod_info\'); ?></p>
 <p class="info"><?php the_field(\'prod_fi\'); ?></p>
</div>
<?php endwhile; endif; ?>
我需要的是把所有的prod_fi 每个学期的字段。此外,该字段的平均值也是。

有什么想法吗?

谢谢

--使用代码编辑。

到目前为止,我尝试了几种选择。更接近的是:

<?php
if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); 
$total_fi = array();
$meta_key = \'prod_fi\';//set this to your custom field meta key
$total_fi = $wpdb->get_col($wpdb->prepare
    ("SELECT meta_value FROM wp_postmeta as pm
    INNER JOIN wp_term_relationships as tr ON (pm.post_id = tr.object_id) 
    INNER JOIN wp_term_taxonomy as tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id)
    WHERE 1
    AND tt.taxonomy = \'prod_tipo\'
    AND pm.meta_key = %s", 
    $meta_key));
    echo \'Total FI \'.array_sum( $total_fi );
    ?>
但是,这一个显示了分类法中所有custom\\u字段的总数量。

这一个,并不是每一项的和的停止,它继续。

$factorI = get_field(\'prod_fi\'); 
if($factorI) {$factor_total += $factorI;}
echo $factor_total;
谢谢你。

我找到了一个棘手的解决办法。我知道这不是正确的方法,如果有人知道正确的方法,我会很高兴的!!!!

<?php
//start the foreach for each term in taxonomy
foreach ( $prod_terms as $prod_term ) {
$prod_query = new WP_Query( array(
    \'post_type\' => \'prod_cientifica\',
    \'prod_area\' => $post_slug,
    \'tax_query\' => array(
         array(
            \'taxonomy\' => \'prod_tipo\',
            \'terms\' => array( $prod_term->slug ),
            \'operator\' => \'IN\',
            \'get\' => \'all\', 
            \'field\' => \'slug\'
            )
         )
) );
?>
<div class="area">
    <h2><?php echo $prod_term->name; ?></h2>
    <?php 
             // First while for fetch the value of "prod_fi" and make the math.
             while ( $prod_query->have_posts() ) : $prod_query->the_post(); 
        $factorI = get_field(\'prod_fi\'); 
        if($factorI) {$factor_total += $factorI;}
                    // count post for average math operation
        $count_posts = $prod_query->current_post + 1;
        $factorP = $factor_total / $count_posts;
    endwhile; ?>
    <p class="areasPublicados"><?php _e(\'Publicados: \', \'twentytwelve\'); ?> <?php echo $count_posts; ?></p>
    <p class="areasFI fleft"><?php _e(\'FI medio: \', \'twentytwelve\'); ?> <?php echo $factorP; ?> </p><p class="areasFI fright"><?php _e(\'FI: \', \'twentytwelve\'); ?> <?php echo $factor_total; ?></p>

    <?php
             //original query to fetch the post in the term
    if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); ?>
    <div class="areaItem">
        <h4><?php the_title(); ?></h4>
        <p class="autores"><?php the_field(\'prod_autores\'); ?></p>
        <p class="info"><?php the_field(\'prod_info\'); ?></p>
            </div>
     <?php endwhile; endif; ?>
</div>
<?php
// Reset math operation of factor_total, so it don\'t continue sum
   $factor_total = null;
   $prod_query = null;
   wp_reset_postdata();
 } //endforeach
?>
再次编辑!

<?php
 // Fetch all post in taxonomy divided by terms.
 foreach ( $prod_terms as $prod_term ) {
    $prod_query = new WP_Query( array(
    \'post_type\' => \'prod_cientifica\',
    \'prod_area\' => $post_slug,
    \'tax_query\' => array(
        array(
        \'taxonomy\' => \'prod_tipo\',
        \'terms\' => array( $prod_term->slug ),
        \'operator\' => \'IN\',
        \'get\' => \'all\', 
        \'field\' => \'slug\'
        )
    )
) );
?>
<?php if($prod_query->have_posts()) : ?>
    <div class="area">
    <h2><?php echo $prod_term->name; ?></h2>
    <?php 
    // Declare total and count before loop
    $factorTotal = 0;
    $factorCount = 0;
    // First loop to fetch prod_fi value
    while ( $prod_query->have_posts() ) : $prod_query->the_post(); 
                $factorI = get_field(\'prod_fi\'); 
        if($factorI) { // Only if it exists
                  $factorTotal += $factorI; // Add it
                  $factorCount++; // Count it
        }
    endwhile; ?>
    <p class="areasPublicados">
       <?php _e(\'Publicados: \', \'twentytwelve\'); ?> <?php echo $factorCount; ?>
    </p>
    <p class="areasFI fleft">
       <?php _e(\'FI medio: \', \'twentytwelve\'); ?> <?php echo $factorTotal; ?> 
    </p>
    <p class="areasFI fright">
       <?php _e(\'FI: \', \'twentytwelve\'); ?> <?php echo ($factorTotal / $factorCount); ?>
    </p>

    <?php
    if ( $prod_query->have_posts() ) : while ( $prod_query->have_posts() ) : $prod_query->the_post(); ?>
        <div class="areaItem">
        <h4><?php the_title(); ?></h4>
        <p class="autores"><?php the_field(\'prod_autores\'); ?></p>
        <p class="info"><?php the_field(\'prod_info\'); ?></p>
        </div>
    <?php endwhile; endif; ?>
</div>
<?php endif; ?>
<?php
// Reset things, for good measure
$factor_total = null;
$prod_query = null;
wp_reset_postdata();
} //enforeach
?>

1 个回复
最合适的回答,由SO网友:Stubbies 整理而成

以下是一种简单的方法:

// First declare total and count before the loop
$total = 0;
$count = 0;

foreach($posts as $post)
{
     if(get_field(\'prod_fi\')){ // If we have a value add it to the total and count it
        $total += get_field(\'prod_fi\');
        $count++;
     }
}

echo \'Count: \'. $count;
echo \'Total Sum: \'. $total;
echo \'Average: \'.($total / $count); // To get the average
编辑:

在您的例子中,如果有$prod\\u查询循环,希望这更有意义:)

// Declare total and count before loop
$factorTotal = 0;
$factorCount = 0;

while ( $prod_query->have_posts() ) : $prod_query->the_post(); 
    $factorI = get_field(\'prod_fi\'); 
    if($factorI){ // Only if it exists
        $factorTotal += $factorI; // Add it
        $factorCount++; // Count it
    } // end if
endwhile;

echo \'Count: \'. $factorCount;
echo \'Total Sum: \'. $factorTotal;
echo \'Average: \'.($factorTotal / $factorCount);

相关推荐

echo a tax term in loop

对于列表中的每个项目,我需要在之后在此循环中回显CPT的一个术语。感谢您的指导或帮助。原始代码来自Stackoverflow。com,因为它起作用了。 <?php $da_place = get_field(\'smart_place\'); //acf field $args = array( \'post_type\' => \'to_do_items\', \'tax_query\' => array