我有食谱的自定义帖子类型,在其中我设置了高级自定义字段(来自插件)
在这个自定义帖子类型中,我有几个类别。两个问题:
1)如何从自定义类别获取所有帖子?我知道如何显示自定义字段,但不知道如何调用自定义类别中的所有帖子,然后我可以显示它的标题、图像和链接。
2)在自定义帖子类型场景中,我最好还是设置自定义类别,还是使用通用帖子类别?
我尝试了get\\u帖子,然后尝试了按自定义类别获取帖子。例如
<?php
$args = array(
\'posts_per_page\' => 8,
\'orderby\' => \'rand\',
\'post_type\' => \'recipes\',
\'type\' => \'side-dishes\',
\'post_status\' => \'publish\'
);
$show_albums = get_posts( $args );
?>
我不知道具体的实现,因为post\\u类型是一个自定义的post,我想我必须为每个post做一些工作,然后才能使用\\u字段(来自ACF)
========================================================================接受以下建议后,请确定我所做的是否正确?
下面是一个查询-似乎有效:
<?php
// The Query
$the_query = new WP_Query($args = array(
\'post_type\' => \'recipes\',
\'custom_cat\' => \'side-dishes\'
) );
// The Loop
if ( $the_query->have_posts() ) {
echo \'<ul>\';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo \'<li>\' . get_the_title() . \'</li>\';
}
echo \'</ul>\';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();?>
我注册了分类法,但让我困惑的是,我注册的帖子类型是recipes,register\\u分类法不应该是“recipes”而不是“custom\\u cat”?“”
register_taxonomy( \'custom_cat\',
array(\'recipes\'), /* if you change the name of register_post_type( \'custom_type\', then you have to change this */
array(\'hierarchical\' => true, /* if this is true, it acts like categories */
\'labels\' => array(
\'name\' => __( \'Recipe Categories\', \'bonestheme\' ), /* name of the custom taxonomy */
\'singular_name\' => __( \'Recipe Category\', \'bonestheme\' ), /* single taxonomy name */
\'search_items\' => __( \'Search Recipe Categories\', \'bonestheme\' ), /* search title for taxomony */
\'all_items\' => __( \'All Recipe Categories\', \'bonestheme\' ), /* all title for taxonomies */
\'parent_item\' => __( \'Parent Recipe Category\', \'bonestheme\' ), /* parent title for taxonomy */
\'parent_item_colon\' => __( \'Parent Custom Category:\', \'bonestheme\' ), /* parent taxonomy title */
\'edit_item\' => __( \'Edit Custom Category\', \'bonestheme\' ), /* edit custom taxonomy title */
\'update_item\' => __( \'Update Custom Category\', \'bonestheme\' ), /* update title for taxonomy */
\'add_new_item\' => __( \'Add New Custom Category\', \'bonestheme\' ), /* add new title for taxonomy */
\'new_item_name\' => __( \'New Custom Category Name\', \'bonestheme\' ) /* name title for taxonomy */
),
\'show_admin_column\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'custom-slug\' ),
)
);
以下是注册职位类型:
function create_recipe() {
// creating (registering) the custom type
register_post_type( \'recipes\', /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
// let\'s now add all the options for this post type
array( \'labels\' => array(
\'name\' => __( \'Recipes\', \'bonestheme\' ), /* This is the Title of the Group */
\'singular_name\' => __( \'Recipe\', \'bonestheme\' ), /* This is the individual type */
\'all_items\' => __( \'All Recipes\', \'bonestheme\' ), /* the all items menu item */
\'add_new\' => __( \'Add New Recipe\', \'bonestheme\' ), /* The add new menu item */
\'add_new_item\' => __( \'Add Recipe\', \'bonestheme\' ), /* Add New Display Title */
\'edit\' => __( \'Edit\', \'bonestheme\' ), /* Edit Dialog */
\'edit_item\' => __( \'Edit Recipe\', \'bonestheme\' ), /* Edit Display Title */
\'new_item\' => __( \'New Recipe Type\', \'bonestheme\' ), /* New Display Title */
\'view_item\' => __( \'View Recipe\', \'bonestheme\' ), /* View Display Title */
\'search_items\' => __( \'Search Recipes\', \'bonestheme\' ), /* Search Custom Type Title */
\'not_found\' => __( \'Nothing found in the Database.\', \'bonestheme\' ), /* This displays if there are no entries yet */
\'not_found_in_trash\' => __( \'Nothing found in Trash\', \'bonestheme\' ), /* This displays if there is nothing in the trash */
\'parent_item_colon\' => \'\'
), /* end of arrays */
\'description\' => __( "Recipes section for It\'s Just food", \'bonestheme\' ), /* Custom Type Description */
\'public\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => false,
\'show_ui\' => true,
\'query_var\' => true,
\'menu_position\' => 4, /* this is what order you want it to appear in on the left hand side menu */
\'menu_icon\' => get_stylesheet_directory_uri() . \'/library/images/custom-post-icon.png\', /* the icon for the custom post type menu */
\'rewrite\' => array( \'slug\' => \'recipes\', \'with_front\' => false ), /* you can specify its url slug */
\'has_archive\' => \'recipes\', /* you can rename the slug here */
\'capability_type\' => \'post\',
\'hierarchical\' => false,
/* the next one is important, it tells what\'s enabled in the post editor */
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'sticky\')
) /* end of options */
); /* end of register post type */