Wordpress 自定义帖子类型下拉列表在编辑器中的另一个帖子类型上


Wordpress custom post type dropdown list on another post type in editor

所以我创建了一个自定义帖子类型,我们称之为actors,另一个叫做movies。因此,当我编辑电影时,我希望有一个下拉列表,其中包含我所有演员的帖子,选择一个并在前端输出该 ID 以与另一个函数一起使用。这是可行的吗?我该怎么做

测试代码:

    <?php 
		$data_terms = get_terms('test_post', array(
            'orderby'    => 'count',
            'hide_empty' => 0,
            'parent'=> 4 // You need the Id of the parent actors taxonomy
             ) );
?>
<select>
     <?php foreach($data_terms as $term):  ?>
         <option>
             <?php echo $term->name; echo $term->ID ?>
         </option>
     <?php endforeach ?>
</select>

与 PHP 中的所有内容一样,要创建对象列表,您必须按照以下步骤操作。

首先,获取数据。

<?php 
        $data_terms = get_terms('your_custom_taxonomy', array(
            'orderby'    => 'count',
            'hide_empty' => 0,
            'parent'=>13 // You need the Id of the parent actors taxonomy
             ) );
?>

现在,您可以根据需要显示它。你想要一个列表。所以:

<select>
     <?php foreach($data_terms as $term):  ?>
         <option>
             <?php echo $term->name; echo $term->ID ?>
         </option>
     <?php endforeach ?>
</select>

当然,您不想显示它,但您可以在缓冲区中传递它并随意使用它。

希望他有帮助。

您需要在wordpress中创建自定义字段。

有关于wordpresscodex的文章,例如(它比代码阅读更多,所以没有发布示例)

或者如果你想使用插件,那么ACF是我过去使用过的插件之一