Last active
December 24, 2024 16:34
-
-
Save esaramago/e4ee367baa4f480a3e710a093985b74e to your computer and use it in GitHub Desktop.
Wordpress Block
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "$schema": "https://schemas.wp.org/trunk/block.json", | |
| "apiVersion": 3, | |
| "name": "THEME_NAME/BLOCK_NAME", | |
| "version": "0.1.0", | |
| "title": "BLOCK_TITLE", | |
| "description": "", | |
| "category": "THEME_NAME", | |
| "textdomain": "THEME_NAME", | |
| "keywords": [], | |
| "editorScript": "file:./index.js", | |
| "style": "file:./style-index.css", | |
| "render": "file:./render.php", | |
| "viewScriptModule": "file:./view.js", | |
| "supports": { | |
| "interactivity": true | |
| }, | |
| "attributes": { | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useBlockProps } from '@wordpress/block-editor' | |
| import ServerSideRender from '@wordpress/server-side-render' | |
| export default function Edit() { | |
| const blockProps = useBlockProps() | |
| return ( | |
| <div { ...blockProps }> | |
| <ServerSideRender | |
| block="THEME_NAME/BLOCK_NAME" | |
| /> | |
| </div> | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import metadata from './block.json' | |
| import { registerBlockType } from '@wordpress/blocks' | |
| import { useBlockProps, InnerBlocks } from '@wordpress/block-editor' | |
| import favicon from '../../images/favicon.js' | |
| import './style.scss' | |
| import Edit from './edit' | |
| registerBlockType(metadata.name, { | |
| icon: favicon, | |
| edit: Edit, | |
| save: () => { | |
| const blockProps = useBlockProps.save() | |
| return ( | |
| <div { ...blockProps }> | |
| <InnerBlocks.Content /> | |
| </div> | |
| ) | |
| }, | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Get post meta attribute | |
| global $post; | |
| $POST_META = get_post_meta( $post->ID, 'POST_META', true ); | |
| // Get array of custom post type posts | |
| $data = new WP_Query(array( | |
| 'post_type' => 'CUSTOM_POST_TYPE', | |
| 'orderby' => 'id', | |
| 'order' => 'DESC', | |
| 'meta_key' => 'META_KEY_1', | |
| 'meta_key' => 'META_KEY_2', | |
| 'post_status' => 'publish', | |
| 'posts_per_page' => -1 | |
| )); | |
| ?> | |
| <div | |
| <?php echo get_block_wrapper_attributes(); ?> | |
| > | |
| <!-- Echo post meta --> | |
| <p><?php echo $POST_META ?></p> | |
| <!-- Print array of custom post type posts --> | |
| <?php if( $data->have_posts() ): ?> | |
| <?php while( $data->have_posts() ) : $data->the_post(); ?> | |
| <p><?php echo get_field('ATTRIBUTE'); ?></p> | |
| <?php endwhile; ?> | |
| <?php else: ?> | |
| <p>EMPTY STATE</p> | |
| <?php endif; | |
| wp_reset_postdata(); // Restore original Post Data | |
| ?> | |
| </div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment