php 具有高级自定义字段块功能的自定义Gutenberg块的源代码 Posted 2021-05-04
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 具有高级自定义字段块功能的自定义Gutenberg块的源代码相关的知识,希望对你有一定的参考价值。
<?php
/* ACF Blocks requires ACF Pro Ver. 5.8 or higher */
/* save this file to /lib/ directory in your child theme folder */
/* if the directory doesn't exist, create it */
function vfcg_register_blocks() {
if( ! function_exists('acf_register_block') )
return;
acf_register_block( array(
'name' => 'boat-captain',
'title' => __( 'Captain', 'captainname' ),
'render_template' => get_stylesheet_directory() . '/lib/blocks/block-boat-captain.php',
'category' => 'formatting',
'icon' => 'admin-users',
'mode' => 'edit',
'keywords' => array( 'profile', 'captain', 'acf' ) ));
}
add_action('acf/init', 'vfcg_register_blocks' );
<?php
/**
* Boat Captain block
*
* @package CaptainName
* @author Victor M. Font Jr.
* @since 1.0.0 (CSS Grid layout)
* @license GPL-2.0+
**/
// retrieve the field content from the database
$name = get_field( 'captain_name' );
$website_name = get_field( 'website_name' );
$website_url = get_field( 'website_url' );
$photo = get_field( 'photo' );
$description = get_field( 'description' );
// display the HTML
echo '<div class="captain-separator"> </div>';
echo '<div class="captain">';
echo ' <div class="captain-photo">';
if( !empty( $photo ) )
echo wp_get_attachment_image( $photo['ID'], 'medium', null, array( 'class' => 'aligncenter' ) );
echo '</a>';
echo ' </div>';
echo ' <div class="captain-details">';
if( !empty( $name ) )
echo '<h2>' . esc_html( $name ) . '</h2>';
if( !empty( $website_name ) )
echo '<p><a rel="noreferrer noopener" href="' . $website_url . '" target="_blank">' . esc_html( $website_name ) . '</a></p>';
echo '<div class="captain-description"><p>' . apply_filters( 'vfcg_the_content', $description ) . '</p></div>';
echo ' </div>';
echo '</div>';
<?php
/**
* Boat Captain block
*
* @package CaptainName
* @author Victor M. Font Jr.
* @since 1.0.0 (Genesis Column Classes layout)
* @license GPL-2.0+
**/
// retrieve the field content from the database
$name = get_field( 'name' );
$website_name = get_field( 'website_name' );
$website_url = get_field( 'website_url' );
$photo = get_field( 'photo' );
$description = get_field( 'description' );
// display the HTML
echo '<div class="captain-separator"> </div>';
echo '<div class="captain">';
echo ' <div class="one-third first">';
// if no photo, insert non-breaking space
if( !empty( $photo ) ) {
echo wp_get_attachment_image( $photo['ID'], 'medium', null, array( 'class' => 'captain-photo aligncenter' ) );
} else {
echo '<div> </div>';
}
echo '</a>';
echo ' </div>';
echo ' <div class="two-thirds">';
if( !empty( $name ) )
echo '<h2>' . esc_html( $name ) . '</h2>';
if( !empty( $website_name ) )
echo '<p><a rel="noreferrer noopener" href="' . $website_url . '" target="_blank">' . esc_html( $website_name ) . '</a></p>';
echo '<div class="captain-description"><p>' . apply_filters( 'vfcg_the_content', $description ) . '</p></div>';
echo ' </div>';
echo '</div>';
.captain {
display: grid;
grid-template-columns: '1fr';
grid-template-areas: "captain-photo" "captain-details";
}
.captain-photo {
grid-area: captain-photo;
}
.captain-details {
grid-area: captain-details;
}
@media only screen and (min-width: 960px) {
.captain {
grid-template-columns: '1fr 2fr';
grid-template-areas: "captain-photo captain-details";
grid-gap: 30px;
}
}
<?php
// add this line of code to functions.php to load the above file.
//load acf gutenberg blocks
require_once get_stylesheet_directory() . '/lib/acf_blocks.php';
以上是关于php 具有高级自定义字段块功能的自定义Gutenberg块的源代码的主要内容,如果未能解决你的问题,请参考以下文章
如何向 WordPress Gutenberg 块添加自定义块?
Racktables的自定义字段高级设置
高级自定义字段/PHP 问题
php PHP - Wordpress - 搜索 - wordpress自定义搜索功能,包含ACF /高级自定义字段和分类法以及拆分表达式
php PHP - Wordpress - 搜索 - wordpress自定义搜索功能,包含ACF /高级自定义字段和分类法以及拆分表达式
php PHP - Wordpress - 搜索 - wordpress自定义搜索功能,包含ACF /高级自定义字段和分类法以及拆分表达式