wp query - Show posts from a custom taxonomy on a page

admin2025-06-02  1

I'm trying to show my portfolios by category (i have 3 categories) On Dashboard i can create new categories/types but when i want list only "design category" on portfolio page "content not found"

these codes in function.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');

portfolio file here.

<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;?>

I'm trying to show my portfolios by category (i have 3 categories) On Dashboard i can create new categories/types but when i want list only "design category" on portfolio page "content not found"

these codes in function.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');

portfolio file here.

<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;?>
Share Improve this question edited Feb 28, 2019 at 0:37 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Feb 27, 2019 at 23:52 SmncSmnc 134 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Most of your code looks OK. But there is a major problem with your WP_Query in portfolio template. Your query looks like this:

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

And if you take a look at WP_Query docs, you'll see there is no parameter called slug. So it's pretty clear that it can't work.

If you want to display portfolio posts with given type, then your query should look like this:

$query = new WP_Query( array(
    'post_type' => 'portfolio',
    'posts_per_page' => -1,
    'tax_query' => array(
        array( 'taxonomy' => 'type', 'field' => 'slug', 'terms' => 'design' ),
    ),
) );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748856318a314289.html

最新回复(0)