如何制作一个下拉列表控件来获取自定义帖子类型的名称?

发布于 2025-01-12 01:02:40 字数 82 浏览 1 评论 0原文

如何制作一个下拉列表控件来获取自定义帖子类型的名称?在我的项目中,我想选择一个帖子类型名称并在我的自定义古腾堡块的下拉选择器中获取它!..我该怎么做?

How can I make a dropdown list control that fetches names of custom posts types? In my project I want to select a post type name and fetch it in drop down selector in my custom Gutenberg Block!.. How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜未央樱花落 2025-01-19 01:02:40

如果为古腾堡块的 edit() 函数创建下拉列表(选择),则可以通过 useSelect() 使用 getPostTypes() 检索已注册的帖子类型JavaScript 中的。一个例子是 下拉菜单在查询块中选择帖子类型。

下面是一个简化的示例,它使用 显示所有可查看帖子类型的列表,并允许将选定的帖子类型保存为块属性。

block.json

{
    ...
    "attributes": {
        "postType": {
            "type": "string",
            "default": "post"
        }
    }
}

edit.js

import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { SelectControl } from '@wordpress/components';
import { useBlockProps } from '@wordpress/block-editor';


export default function Edit({ attributes, setAttributes }) {
    // postType defined in block.json
    const { postType } = attributes;

    // useSelect to retrieve all post types
    const postTypes = useSelect(
        (select) => select(coreStore).getPostTypes({ per_page: -1 }), []
    );

    // Options expects [{label: ..., value: ...}]
    var postTypeOptions = !Array.isArray(postTypes) ? postTypes : postTypes
        .filter(
            // Filter out internal WP post types eg: wp_block, wp_navigation, wp_template, wp_template_part..
            postType => postType.viewable == true)
        .map(
            // Format the options for display in the <SelectControl/>
            (postType) => ({
                label: postType.labels.singular_name,
                value: postType.slug, // the value saved as postType in attributes
            })
        );

    return (
        <div {...useBlockProps()}>
            <SelectControl
                label="Select a Post Type"
                value={postType}
                options={postTypeOptions}
                onChange={(value) => setAttributes({ postType: value })}
            />
        </div>
    );
}

使用 JavaScript 作为编辑器而不是回退到 PHP 的优点是,您可以使用 < 等古腾堡控件来保持 UI 一致;选择控制/>

设置侧边栏可能是放置的好地方,具体取决于您的目标。

If creating a dropdown list (select) for the edit() function of a Gutenberg block, registered post types can be retrieved with getPostTypes() via useSelect() in JavaScript. An example of this is the dropdown in the Query Block to select a Post Type.

Below is a simplified example that uses a <SelectControl/> to display a list of all viewable post types, and enables a selected postType to be saved the blocks attributes.

block.json

{
    ...
    "attributes": {
        "postType": {
            "type": "string",
            "default": "post"
        }
    }
}

edit.js

import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { SelectControl } from '@wordpress/components';
import { useBlockProps } from '@wordpress/block-editor';


export default function Edit({ attributes, setAttributes }) {
    // postType defined in block.json
    const { postType } = attributes;

    // useSelect to retrieve all post types
    const postTypes = useSelect(
        (select) => select(coreStore).getPostTypes({ per_page: -1 }), []
    );

    // Options expects [{label: ..., value: ...}]
    var postTypeOptions = !Array.isArray(postTypes) ? postTypes : postTypes
        .filter(
            // Filter out internal WP post types eg: wp_block, wp_navigation, wp_template, wp_template_part..
            postType => postType.viewable == true)
        .map(
            // Format the options for display in the <SelectControl/>
            (postType) => ({
                label: postType.labels.singular_name,
                value: postType.slug, // the value saved as postType in attributes
            })
        );

    return (
        <div {...useBlockProps()}>
            <SelectControl
                label="Select a Post Type"
                value={postType}
                options={postTypeOptions}
                onChange={(value) => setAttributes({ postType: value })}
            />
        </div>
    );
}

The advantage of using JavaScript for the Editor instead of falling back to PHP is you can keep the UI consistent by using Gutenberg controls like <SelectControl/>.

The Settings Sidebar may be a good place to put your <SelectControl/> depending on your goal.

人生百味 2025-01-19 01:02:40
$post_types   = get_post_types( [
       'public'   => true,
        '_builtin' => false,
], 'objects', 'and' );

 <select name="post-types" class="form-control">
    <?php
    foreach ( $post_types as $post_type ) {
        ?>
            <option value="<?php echo esc_attr($post_type->name); ?>">
                <?php echo esc_html($post_type->label) ?>
            </option>
        <?php
    }
    ?>
</select> 

如果您想传递变量(在本例中为:CPT)来阻止脚本,您可以使用 wp_localize_scripts :

wp_enqueue_script('gutenberg-select-cpt-block', $pathToScript, [], null, 
   true);
wp_localize_script(
  'gutenberg-select-cpt-block',
  YOURJSOBJECT,
  ['post_types' => $post_types] <--- array of data you want to pass
);
$post_types   = get_post_types( [
       'public'   => true,
        '_builtin' => false,
], 'objects', 'and' );

 <select name="post-types" class="form-control">
    <?php
    foreach ( $post_types as $post_type ) {
        ?>
            <option value="<?php echo esc_attr($post_type->name); ?>">
                <?php echo esc_html($post_type->label) ?>
            </option>
        <?php
    }
    ?>
</select> 

If you want to pass variables ( in this example : CPT ) to block scripts , you can use wp_localize_scripts :

wp_enqueue_script('gutenberg-select-cpt-block', $pathToScript, [], null, 
   true);
wp_localize_script(
  'gutenberg-select-cpt-block',
  YOURJSOBJECT,
  ['post_types' => $post_types] <--- array of data you want to pass
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文