如何通过自定义帖子类型获取帖子,然后显示自定义域?

时间:2014-02-25 作者:user31344

我有食谱的自定义帖子类型,在其中我设置了高级自定义字段(来自插件)

在这个自定义帖子类型中,我有几个类别。两个问题:

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 */

1 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

This might be a good read for you. 我不确定你到底想做什么,所以我将以一种简单的格式来阐述这一点。

此查询将提取该职位类型下的所有职位recipes 其中自定义字段side-dishes 存在。然后,您可以按自己的意愿循环并显示它们。

$recipes = new WP_Query(
    array(
        \'post_type\' => \'recipes\',
        \'posts_per_page\' => 8,
        \'orderby\' => \'rand\',
        \'post_status\' => \'publish\',
        \'meta_query\' => array(
            array(
                \'key\' => \'side-dishes\'
                \'compare\' => \'EXISTS\'
            )
        )
    )
);
这第二个查询将提取分类法下的所有帖子my_taxonomy_name_here 您需要用您的分类法替换它。它查找包含slug的类别my_category_slug_here 您需要将其替换为类别slug。你可以用ID替换“slug”,如果你愿意的话,也可以根据ID来提取它。

$recipes = new WP_Query(
    array(
        \'post_type\' => \'recipes\',
        \'posts_per_page\' => 8,
        \'orderby\' => \'rand\',
        \'post_status\' => \'publish\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'my_taxonomy_name_here\',
                \'field\' => \'slug\',
                \'terms\' => \'my_category_slug_here\'
            )
        )
    )
);
然后,您可以像普通循环一样循环遍历每个帖子:

<?php if($recipes->have_posts()) : ?>
    <?php while($recipes->have_posts()) : $recipes->the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
    <?php endwhile; ?>
<?php endif; wp_reset_query(); ?>

Documentation on WP_Query

Documentation on Meta_Query

Documentation on Tax_Query

结束

相关推荐

如何通过自定义帖子类型获取帖子,然后显示自定义域? - 小码农CODE - 行之有效找到问题解决它

如何通过自定义帖子类型获取帖子,然后显示自定义域?

时间:2014-02-25 作者:user31344

我有食谱的自定义帖子类型,在其中我设置了高级自定义字段(来自插件)

在这个自定义帖子类型中,我有几个类别。两个问题:

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 */

1 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

This might be a good read for you. 我不确定你到底想做什么,所以我将以一种简单的格式来阐述这一点。

此查询将提取该职位类型下的所有职位recipes 其中自定义字段side-dishes 存在。然后,您可以按自己的意愿循环并显示它们。

$recipes = new WP_Query(
    array(
        \'post_type\' => \'recipes\',
        \'posts_per_page\' => 8,
        \'orderby\' => \'rand\',
        \'post_status\' => \'publish\',
        \'meta_query\' => array(
            array(
                \'key\' => \'side-dishes\'
                \'compare\' => \'EXISTS\'
            )
        )
    )
);
这第二个查询将提取分类法下的所有帖子my_taxonomy_name_here 您需要用您的分类法替换它。它查找包含slug的类别my_category_slug_here 您需要将其替换为类别slug。你可以用ID替换“slug”,如果你愿意的话,也可以根据ID来提取它。

$recipes = new WP_Query(
    array(
        \'post_type\' => \'recipes\',
        \'posts_per_page\' => 8,
        \'orderby\' => \'rand\',
        \'post_status\' => \'publish\',
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'my_taxonomy_name_here\',
                \'field\' => \'slug\',
                \'terms\' => \'my_category_slug_here\'
            )
        )
    )
);
然后,您可以像普通循环一样循环遍历每个帖子:

<?php if($recipes->have_posts()) : ?>
    <?php while($recipes->have_posts()) : $recipes->the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
    <?php endwhile; ?>
<?php endif; wp_reset_query(); ?>

Documentation on WP_Query

Documentation on Meta_Query

Documentation on Tax_Query

相关推荐