标签: advanced-custom-fields

ACF关系字段 - 来自其他帖子类型的get_field值

在我的POSTS页面(常规帖子类型)中,我设置了一个ACF关系字段.在这里我可以选择公司名称,这些名称都在directory_listings的post类型下.

现在,我在目录列表页面上有以下代码,因此使用简单的get_field不起作用,因为这些值不在此页面上,而是在POST类型的其他位置.

所以不确定如何获取信息.

DIRECTORY_LISTINGS帖子类型下的其中一个页面上的代码:

$posts = get_field('related_articles');

if( $posts ): ?>
    <ul>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endforeach; ?>
    </ul>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)

示例图,因为我不太通过文本解释. 在此输入图像描述

目前,我已在公司编辑页面(directory_listing)上设置了关系字段.它执行以下操作时:1)此商家列表的相关帖子 - >选择帖子 - >发布 - >现在显示商家列表页面上的列表.示例:http://bit.ly/1vwydDl(页面底部)

2)我想从POST编辑页面选择一个将发布帖子的商家.我可以通过ACF将该字段放在那里没有问题,但让它实际显示我无法弄清楚的结果.

wordpress advanced-custom-fields

7
推荐指数
1
解决办法
2万
查看次数

点击其他时更改Google地图标记图标

我使用高级自定义字段Google地图字段创建了Google地图多个位置页面.

我已设法在单击时更改标记图标,但我希望在单击其他图标时将其更改回来.

这是代码的一个例子:

    for (i = 0; i < locations.length; i++) {  
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    icon: iconBase + 'Stock%20Index%20Up.png'
  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0], locations[i][6]);
      infowindow.open(map, marker);
      marker.setIcon("https://cdn3.iconfinder.com/data/icons/musthave/24/Stock%20Index%20Down.png");
    }
  })(marker, i));
Run Code Online (Sandbox Code Playgroud)

在这里更好地了解工作代码:http: //jsfiddle.net/gargiguy/s8vgxp3g

javascript google-maps-api-3 google-maps-markers advanced-custom-fields

7
推荐指数
1
解决办法
1万
查看次数

高级自定义字段 - 按先前选择的分类法过滤后期对象字段

我有一个名为Event的自定义帖子类型,我想在其中添加从大型位置列表中选择的位置.为了使这个过程更容易,我想通过一个名为locality的类别过滤这些位置.

为此,我创建了一个名为Locations的自定义帖子类型和一个名为Locality的自定义分类.每个事件都添加了一个字段组,其中包含两个下拉列表,一个用于Locality(分类法),另一个用于Locations(Post Object).

我没有达到的目的是按所选地点过滤地点.

wordpress wordpress-theming advanced-custom-fields

7
推荐指数
1
解决办法
663
查看次数

如何计算ACF转发器输出中的总行数

问题:如何简单计算ACF转发器字段输出中的行?

目标:当只有一行而不是一行时,使输出看起来与css类不同.

我的代码:

if( have_rows('testimonials')) {
    $counter = 0;
    $numtestimonials = '';

    //loop thru the rows
    while ( have_rows('testimonials') ){
        the_row();
        $counter++;
            if ($counter < 2) {                         
                $numtestimonials = 'onlyone';               
            }
        echo '<div class="testimonial ' . $numtestimonials . '">';
           // bunch of output here
        echo '</div>';                          
    }
}
Run Code Online (Sandbox Code Playgroud)

显然,我在这里这样做的方式不会起作用,因为第一次通过行时计数<2,所以即使计算了更多的行,它也会返回true.

谢谢!

php mysql wordpress repeater advanced-custom-fields

7
推荐指数
2
解决办法
2万
查看次数

使用meta_query进行复杂的custom_field搜索

背景

我使用Advanced Custom Fields Pro管理我的自定义字段,它们有一个"repeater"字段,其中包含存储的子字段,repeatername_X_fieldname其中X是转发器的行号.

我有一个自定义的帖子类型student,attendance其中包含date和包含的转发器class.

因此,当学生上课时,它将按如下方式存储他们的出勤率

  • meta_key: 'attendance_X_date' meta_value: '20170701'
  • meta_key: 'attendance_X_class' meta_value: '历史101'

为了搜索任何已经去过某个班级或在某个日期范围内就读的学生,我必须挂钩get_meta_sql并将我转换meta_query为使用LIKE而不是=当值包含时%

function key_rewrite($parts){
    foreach($parts as &$part){
        $part = preg_replace("/(meta_key = )(\'[^']*[%][^']*\')/", "meta_key LIKE $2", $part);
    }
    return $parts;
}
add_action( 'get_meta_sql', 'key_rewrite');
Run Code Online (Sandbox Code Playgroud)

这让我可以做类似的事情

$args = array(
    'post_type' => 'student',
    'meta_query' => array(
        array(
            'key'=>'attendance_%_class',
            'compare'=>'=',
            'value'=>'History 101'
        )
    )
);
$my_query = …
Run Code Online (Sandbox Code Playgroud)

wordpress custom-fields advanced-custom-fields

7
推荐指数
1
解决办法
623
查看次数

按状态显示订单数量,并按Woocommerce中的自定义ACF字段显示

我正在开发一种内部"插件"来显示我目前通过状态和客户经理进行拆分的订单数量.所以我想把它作为一个表格来呈现:

Account Manager | No of pending orders | No of canceled orders etc
Run Code Online (Sandbox Code Playgroud)

我有这样一段代码:

function display_woocommerce_order_count( $atts, $content = null ) {
    $args = shortcode_atts( array(
        'status' => 'completed',
    ), $atts );
    $statuses    = array_map( 'trim', explode( ',', $args['status'] ) );
    $order_count = 0;
    foreach ( $statuses as $status ) {
        // if we didn't get a wc- prefix, add one
        if ( 0 !== strpos( $status, 'wc-' ) ) {
            $status = 'wc-' . $status;
        }
        $order_count += …
Run Code Online (Sandbox Code Playgroud)

php wordpress orders woocommerce advanced-custom-fields

7
推荐指数
1
解决办法
348
查看次数

我可以使用 Javascript 更新 Wordpress 编辑器中的默认类吗?

在 WordPress Gutenberg 编辑器中,我试图以编程方式在图像块上设置默认类,该类无需用户通过“附加 CSS”字段手动添加即可应用。

我曾尝试在图像块上应用默认样式,该样式最初有效 -

wp.blocks.registerBlockStyle( 'core/image', {
  name: 'retailResidential',
  label: 'Retail & Residential',
  isDefault: true
});
Run Code Online (Sandbox Code Playgroud)

但是在用户更改自定义下拉列表中的字段后,我需要更新此默认类。更改此下拉列表时,我将取消注册块样式,然后注册新的默认块样式 - 但它对额外创建的图像没有影响(不使用更新的默认样式创建图像,仍使用旧的)。

wp.blocks.unregisterBlockStyle(
  'core/image',
  [ 'retailResidential', 'weddingsEvents', 'advertisingEditorial']
);
Run Code Online (Sandbox Code Playgroud)

更新默认图片块样式后是否需要刷新编辑器?或者有没有其他更好的方法来做到这一点?

更新块样式的参考

javascript wordpress advanced-custom-fields wordpress-gutenberg

7
推荐指数
1
解决办法
406
查看次数

使用自定义分类术语标识自定义字段

我已经为我的布道自定义帖子类型创建了一个自定义的单个sermons.php模板文件,并希望在此帖子中包含布道说话者的讲道图像自定义字段.

自定义分类ID:sermon_speaker自定义字段ID:sermon_speaker_image

我已经能够获得分类学术语ID以在页面上显示为数字,使用:

<?php

$terms = wp_get_post_terms($post->ID, "sermon_speaker");
foreach ($terms as $termid) {  
echo $termid->term_id;
} 

?>
Run Code Online (Sandbox Code Playgroud)

我正在尝试弄清楚如何在我目前拥有的代码中使用此术语ID.$ term_id,以便将术语ID添加到背景图像URL的末尾.

<div class="sermon-speaker-image" style="background-image: url('<?php the_field( 'sermon_speaker_image', 'sermon_speaker_' . $term_id ); ?>');"></div>
Run Code Online (Sandbox Code Playgroud)

更新:以下代码是基于以下答案的工作解决方案:

<?php
global $post;

// load all 'sermon_speaker' terms for the post
$terms = get_the_terms($post->ID, 'sermon_speaker');

// we will use the first term to load ACF data from
if( !empty($terms) )
{
    $term = array_pop($terms);

    $custom_field = get_field('sermon_speaker_image', $term );

}
?>
<div class="sermon-speaker-image" style="background-image: url('<?php echo $custom_field; ?>');"></div>
Run Code Online (Sandbox Code Playgroud)

php wordpress custom-fields custom-taxonomy advanced-custom-fields

6
推荐指数
1
解决办法
1197
查看次数

高级自定义字段 (ACF) Pro acf-json 工作流程

我的团队刚刚改用 acf-json,我们对如何使用 git 的团队处理文件有一些疑问。

\n\n

问题基本上是,当 .json 文件显示为已修改时,我们是否应该始终提交它们?如果在拉取后立即同步,每个团队成员是否应该坚持一个习惯?

\n\n

最近(在我了解同步不是自动之前)我在同步之前更改了管理中的一些内容。当然,这使得我的 \xe2\x80\x98modified\xe2\x80\x99 日期更新,并且不再选择同步。我们通过从生产服务器生成新的 .json 文件并将本地计算机同步到它们来修复它。问题解决了。

\n\n

但是\xe2\x80\xa6 在我同步后,创建了较新的.json,它实际上与我刚刚导入的内容相同,但具有较新的修改日期。\n现在,如果我提交这些文件并且另一个用户同步这些文件,则将不会\xe2\x80\ x99t同样的事情会永远发生吗?

\n\n

现在我只会删除而不提交它们,但我\xe2\x80\x99m希望有人可以解释编辑字段组时更加自动化、不那么实际的工作流程。

\n\n

谢谢。

\n

git wordpress advanced-custom-fields

6
推荐指数
1
解决办法
2882
查看次数

如何为 wordpress WP_QUERY 清理 $_POST?

有谁知道如何清理$_POSTfor wordpress?或者它在我使用时已经消毒了WP_QUERY?谢谢!

我在想我是使用mysql_escape()还是esc_sql()[wordpress 功能]。

function checkIfEmailAndPasswordHaveUser( $email, $password ) {   

$args = array(
    'post_type'  => 'my_custom_post_type',
    'meta_query' => array(
        array(
            'key'     => 'email',
            'value'   => $email
        ),
        array(
            'key'       => 'password',
            'value'     => $password
        ),
    ),
);

$query = new WP_Query( $args );
    if( !$query->have_posts() ) {
        return false;
    } else {
        // return the user's ID
        return $query->posts[0]->ID;
    }
}

$post_user_email        = trim( $_POST['user_email'] );
$post_user_password     = trim( $_POST['user_password'] );

// …
Run Code Online (Sandbox Code Playgroud)

php wordpress advanced-custom-fields

6
推荐指数
1
解决办法
1497
查看次数