标签: custom-fields

检查 Wordpress 中是否存在自定义字段

这是我关于 Stack Overflow 的第一个问题,所以请耐心等待。

我有一个WordPress安装了高级自定义字段插件的站点。该站点具有自定义帖子类型,其中附加了各种 ACF 自定义字段。

并非所有自定义字段都是必需的,一些自定义字段也被分组到它们自己的表格结构中。

我需要检查 2 个自定义字段中的任何一个是否有内容,以及它们是否确实显示了表格。

PHP的相当有限,但我做了一些研究,它应该可以工作,但事实并非如此。

代码如下:

<?php if (get_field( 'meeting_documents_agendas' && 'meeting_documents_minutes')) : ?>

    <?php if ( get_field( 'meeting_documents_agendas' ) ) : ?>
        <a href="<?php echo get_field('meeting_documents_agendas');?>">Download file</a>
    <? endif ?>

    <?php if ( get_field( 'meeting_documents_minutes' ) ) : ?>
        <a href="<?php echo get_field('meeting_documents_minutes');?>">Download file</a>
    <? endif ?>

<? endif ?>
Run Code Online (Sandbox Code Playgroud)

基本上没有任何显示,即使我在这些自定义字段中有内容。

代码对吗?这可能是一个WordPress错误吗?

php wordpress custom-fields

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

WordPress 自定义字段在 wp_update_post 上丢失,有一点不同

我使用 wp_update_post 以编程方式从前端向帖子添加标题和标签。在此过程中,我遇到了自定义字段的令人头疼的问题:最初创建帖子时创建和填充的两个自定义字段之一的值已被删除,而另一个则完全正常。

这是我首先用来创建帖子的代码部分:

// Set the post ID so that we know the post was created successfully
$post_id = wp_insert_post(
    array(
        'comment_status'=>  'closed',
        'ping_status'   =>  'closed',
        'post_author'   =>  $author_id,
        'post_name'     =>  $slug,
        'post_status'   =>  'publish',
        'post_type'     =>  'custom'
    )
);

// If the post was created properly
if($post_id) {

    // Add meta/custom field data to post
    add_post_meta($post_id, 'custom_random_id', $randomId);
    add_post_meta($post_id, 'viewcount', '1');
Run Code Online (Sandbox Code Playgroud)

然后,这是我用来更新标题和标签的代码:

// Continue if untampered
if($new_hashed_value == $_POST['hash']) {

    $updatePost = array();
    $updatePost['ID'] = $post_id;
    $updatePost['post_title'] = …
Run Code Online (Sandbox Code Playgroud)

wordpress custom-fields

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

wordpress 高级自定义字段背景图片

我正在尝试设置通过自定义字段插件上传的图像,并将其显示为 div(在滑块中使用)的背景。

但是图像没有显示......我在自定义字段中有文本并且显示正常,所以我认为它与我用来拉入图像的代码行有关。

我正在尝试使用图像设置 .slide1 的背景。

自定义字段名称是 slide1_background。

HTML:

<div class="slide1" style="background-image:url('<?php the_field('slide_bg1'); ?>');">
<div class="slide1-cont"><p class="slide-text">
<h1><?php the_field('slide_title1'); ?></h1>
<img src="<?php bloginfo('template_directory')?>/images/line.png" /></p>
<p><?php the_field('slide_content1'); ?></p></div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

.slide1{
background-repeat:no-repeat;
background-size:cover;
background-position:center;
height: 800px;
}
Run Code Online (Sandbox Code Playgroud)

css php wordpress background-image custom-fields

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

必需的自定义 WooCommerce 结帐字段不验证输入的值

我正在店面子主题functions.php文件中添加 WooCommerce 自定义结帐字段。
它们具有“必需”属性。
目的是让这些字段出现在页面顶部,在计费字段之前。

单击提交按钮继续付款时,我收到所需的自定义字段验证错误(“请填写您的姓名”)并且无法继续付款,即使使用有效数据填充该字段也是如此。

任何线索如何解决这个问题或从哪里开始调试?

这是代码functions.php

add_action( 'woocommerce_before_checkout_form', 'my_custom_checkout_fields' );

function my_custom_checkout_fields( $checkout ) {

    echo '<div id="my_custom_checkout_field" class="col4-set"><h2>' . __('name') . '</h2>';

    woocommerce_form_field( 'developer_name', array(
        'type'          => 'text',
        'class'         => array('developer_name-class form-row form-row-first'),
        'label'         => __('name'),
        'placeholder'   => __('fill in your name'), 
        'required'      => true,
        ), $checkout->get_value( 'developer_name' ));                               

    echo '</div>';

}

/**
 * Process the checkout
 */
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');

function my_custom_checkout_field_process() {
    // Check if set, if its not set add an …
Run Code Online (Sandbox Code Playgroud)

php wordpress checkout custom-fields woocommerce

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

通过 Woocommerce Rest Api 创建具有自定义字段的产品

我正在通过 Woocommerce Rest Api v2 添加产品。但我无法通过 Api 添加自定义字段。

请建议我最好的方法来做到这一点。

json响应:

$data = array(
   // 'product' => array(
        'id' => 8555,
        'title' => 'Notebook',
        'type' => 'simple',
        'product_id' => 8555,
        'regular_price' => '21.99',
        'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
        'short_description' => 'Pellentesque habitant morbi …
Run Code Online (Sandbox Code Playgroud)

php wordpress custom-fields woocommerce woocommerce-rest-api

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

列出ACF中组字段类型的所有标签和值

我真的需要一些帮助,我的问题是我无法使用ACF显示来自组字段的标签

下面的脚本显示名称和值,我需要显示"标签"及其"值",我找不到任何东西.

if( have_rows('product_specifications') ): 
    while( have_rows('product_specifications') ): the_row(); 
    $subfields = get_field('product_specifications'); 
    if( $subfields ) { ?>
    <ul> 
        <?php
        foreach ($subfields as $spec => $value) {
        if ( !empty($value) ) { ?>
        <li><?php echo $spec; ?> : <?php echo $value; ?></li>
        <?php }
        } ?>
    </ul>
    <?php }
    endwhile;
endif;
Run Code Online (Sandbox Code Playgroud)

这是我目前的输出:

lamp_type : E27
wattage : 1x 60W Max
globe_included : 1
colour_cord : Clear
Run Code Online (Sandbox Code Playgroud)

它应该是:

Lamp Type : E27
Wattage : 1x 60W Max
Globe : 1
Colour Cord …
Run Code Online (Sandbox Code Playgroud)

php wordpress custom-fields advanced-custom-fields acfpro

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

如何保存元框值?

我创建了一个元框。代码是:

// Create your custom meta box
add_action( 'add_meta_boxes', 'hotel_amenities' );
   // Add a custom meta box to a post
    function hotel_amenities( $post ) { 
        add_meta_box(
                'Meta Box Amenities', // ID, should be a string
                'Amenities', // Meta Box Title
                'amenities_content', // Your call back function, this is where your form field will go
                'post', // The post type you want this to show up on, can be post, page, or custom post type
                'normal', // The placement of your …
Run Code Online (Sandbox Code Playgroud)

wordpress custom-fields meta-boxes

0
推荐指数
1
解决办法
5503
查看次数

删除自定义元框不起作用

我在这里尝试做的是删除我在选择模板时创建的一些自定义字段,也就是当我选择某个模板时我要隐藏或显示特定的元文件框.

我所拥有的代码如下,但它根本不起作用(也就是说它不会删除任何元文件),我想帮助看看它有什么问题,或者我正在尝试做什么只是不可能.

    add_action('admin_init','my_meta_init');
    function my_meta_init(){
    $template_file = get_post_meta(get_the_ID(), '_wp_page_template', TRUE);

if (($template_file == 'thanks-template.php') || ($template_file == 'view-template.php')) 
{
    remove_meta_box('my_meta_box_id','page','normal'); 
    remove_meta_box('my_meta_box_id_2','page','side'); 
    remove_meta_box('my_meta_box_id_3','page','side');
    remove_meta_box('dynamic_sectionid','page','normal');     
} else
{
    remove_meta_box('my_meta_box_id_4','page','normal'); 
}
    }
Run Code Online (Sandbox Code Playgroud)

wordpress custom-fields meta-boxes

0
推荐指数
1
解决办法
1496
查看次数

在WooCommerce结帐中添加自定义复选框,其值以管理编辑顺序显示

我尝试添加一个<input type="checkbox">值也显示在woocommerce后端,所以我可以在最后看到客户是否勾选了方框.

该复选框应位于付款方式下方.

是否可以在WooCommerce结帐中添加一个自定义复选框,该值在管理编辑顺序中显示?

php wordpress checkout custom-fields woocommerce

0
推荐指数
1
解决办法
4841
查看次数

在 woocommerce 存档页面和加售/相关产品中显示自定义产品价格

有没有办法在 WordPress 仪表板中启用后端字段,以在存档页面和加售/相关产品中显示每个产品的自定义价格,但将价格保留在产品摘要中?

示例:产品 A 的价格为 10 欧元,但我想改为显示“6 欧元/公斤起”。

最简单的方法是使用一个自定义字段,该字段覆盖了 ,woocommerce_template_loop_price但此代码不起作用,但我不明白为什么。

add_action('woocommerce_template_loop_price', 'shopprice_change', 10, 2);
function shopprice_change ($price, $product) {
    global $post, $blog_id;
    $post_id = $post->ID;
    $price = get_post_meta($post_id, 'shoppricechange', true);
    return $price;
    wp_reset_query();
}
Run Code Online (Sandbox Code Playgroud)

更新

我找到了一种解决方案,可以在不更改单个产品页面的情况下更改存档页面中的价格:

function cw_change_product_html( $price_html, $product ) {
    $unit_price = get_post_meta( $product->id, 'shoppricechange', true );
    if (is_product()) return $price_html;
    if ( ! empty( $unit_price ) ) {
        $price_html = '<span class="amount">' . wc_price( $unit_price ) . '</span>';  
    }
    return $price_html;
}
add_filter( 'woocommerce_get_price_html', …
Run Code Online (Sandbox Code Playgroud)

php wordpress product custom-fields woocommerce

0
推荐指数
1
解决办法
2866
查看次数

在 WooCommerce 购物车和结帐表中显示产品自定义字段值

在 WooCommerce 中,我为每个产品添加了一个自定义字段“描述”。我找到了一种同时显示标签名称和值的方法:

add_filter( 'woocommerce_add_cart_item_data', 'save_days_field', 10, 2 );
function save_days_field( $cart_item_data, $product_id ) {
    $special_item = get_post_meta( $product_id , 'description',true );

    if(!empty($special_item)) {
        $cart_item_data[ 'description' ] = $special_item;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'description', $special_item );
    }
    return $cart_item_data;
}

// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data','rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_on_cart_and_checkout( $cart_item_data, $cart_item ) {
    if( isset( $cart_item['description'] ) ) { …
Run Code Online (Sandbox Code Playgroud)

php wordpress cart custom-fields woocommerce

0
推荐指数
1
解决办法
3108
查看次数