在页面上显示来自自定义分类的帖子

时间:2019-02-27 作者:Smnc

我试图在仪表板上按类别(我有3个类别)显示我的投资组合我可以创建新的类别/类型,但当我只想列出时"design category" 在公文包页面“找不到内容”

这些代码正常工作。php

function portfolio_s (){
    $labels = array(

    \'name\' => \'Portfolio\',
    \'singular_name\'=>\'Portfolio\',
    \'add_new\' => \'Add Item\',
    \'all_items\' => \'all Item\',
    \'add_new_item\' => \'Add new Item\',
    \'edit_item\' => \'edit Item\',
    \'new_item\' => \'new Item\',
    \'view_item\' => \'view Item\',
    \'search_item\' => \'search Item\',
    \'not_found\' => \'No Items found\',
    \'not_found_in_trash\' => \'No item\',
    \'parent_item_colon\' => \'Parent Item\',
    );

    $args=array(
    \'labels\' => $labels,
    \'public\' => true,
    \'has_archive\' => true,
    \'publicly_gueryable\' =>true,
    \'query_var\' => true,
    \'rewrite\' => true,
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,
    \'supports\' => array(
    \'title\',
    \'editor\',
    \'excerpt\',
    \'thumbnail\',
    \'revisions\',
    ),

    \'exclude_from_search\' => false
);
register_post_type( "portfolio", $args );
} 
add_action( \'init\', \'portfolio_s\' );

function portfolio_taxonomies() {

    $labels = array(
    \'name\' => \'Types\',
    \'singular_name\' => \'Type\',
    \'search_items\' => \'Search Types\',
    \'all_items\' => \'All Types\',
    \'parent_item\' => \'Parent Types\',
    \'parent_item_colon\' => \'Parent Types\',
    \'edit_item\' => \'edit Type\',
    \'update_item\' => \'update Type\',
    \'add_new_item\' => \'Add new Product\',
    \'new_item_name\' => \'All Types\',
    \'menu_name\' => \'Types\',
    );

    $args= array(
    \'hierarchical\'          => true,
    \'labels\' => $labels,
    \'show_ui\' =>true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'rewrite\' => array(\'slug\' => \'type\')

    );
    register_taxonomy(\'type\', array(\'portfolio\'), $args);
    }
    add_action(\'init\', \'portfolio_taxonomies\');
此处是公文包文件。

<div class="row">

<?php   $query = new WP_Query( array( \'slug\' => \'design\' ) );?>

<?php if($query->have_posts()):?>   

<?php while($query->have_posts()) : $query->the_post();?>   




            <div class="col-md-3 referans-oge">

            <a href="<?php the_permalink();?>"><?php the_post_thumbnail(\'thumbnail-large\', array(\'class\'=>\'img-fluid\'));?></a>



            </div>

<?php endwhile;?>       

<?php else:?>

Content not found

<?php endif;?>

1 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

大多数代码看起来都正常。但您在公文包模板中的WP\\u查询存在一个主要问题。您的查询如下所示:

<?php   $query = new WP_Query( array( \'slug\' => \'design\' ) );?>
如果你看看WP_Query docs, 您将看到没有调用任何参数slug. 所以很明显这是行不通的。

如果要显示portfolio 具有给定type, 那么您的查询应该如下所示:

$query = new WP_Query( array(
    \'post_type\' => \'portfolio\',
    \'posts_per_page\' => -1,
    \'tax_query\' => array(
        array( \'taxonomy\' => \'type\', \'field\' => \'slug\', \'terms\' => \'design\' ),
    ),
) );

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post