小编use*_*511的帖子

如何访问古腾堡块中的高级自定义字段值?

我有一个带有一些高级自定义字段的自定义帖子类型。我正在尝试从古腾堡块内访问这些自定义字段值。

我已将以下内容添加到我的register_post_type函数中

    'show_in_rest' => true,
    'supports' => array( 'title', 'editor', 'custom-fields' ),
Run Code Online (Sandbox Code Playgroud)

我可以使用以下命令成功地从我的古腾堡块中检索自定义帖子:

select('core').getEntityRecords('postType', 'customType')

但我没有看到自定义字段或其值。

这是我的块的 JavaScript:

const { __ } = wp.i18n;
const { registerBlockType } = wp.blocks;
const { withSelect } = wp.data;

registerBlockType('cgb/block-press-block', {
  title: __('Press Block'),
  icon: 'awards',
  category: 'common',
  keywords: [
    __('press-block'),
  ],
  edit: withSelect((select) => {
    return {
      posts: select('core').getEntityRecords('postType', 'press')
    };
  })(({posts}) => {
    return <p>Content</p>;
  }),
});
Run Code Online (Sandbox Code Playgroud)

有没有办法在编辑器端访问自定义帖子的高级字段值或将该数据传递到块?

wordpress advanced-custom-fields wordpress-gutenberg gutenberg-blocks

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

如何创建一个返回第n个素数的方法?

我正在尝试编写一个返回第n个素数的方法.

我已经找到了解决方案,但问题在于我的方法.我创建了一大堆数字似乎处理速度超慢.(1..104729).to_a确切地说.我选择104729因为max n可以是10000而第10000个整数是104729.我正在寻找一种优化方法的方法.

104729值太大了吗?有没有办法写这个,所以我不是在创建一个大型数组?

这是方法:

def PrimeMover(num)

  def is_prime(x)
    i = 0
    nums = (2..x).to_a
    while nums[i] < nums.max
      if x % nums[i] != 0
        i += 1
      else
        return false
      end
    end
    return true
  end

  primes_arr = (3..104729).to_a.select {|y| is_prime(y)}

  primes_arr[num]

end
Run Code Online (Sandbox Code Playgroud)

ruby primes

4
推荐指数
2
解决办法
2061
查看次数