Display wordpress sub-category posts on sub-category page


added $cat variable to query_posts array to get the current selected category posts.

$category = get_category( get_query_var( 'cat' ) );
$cat = $category->cat_name;

// only display product CPT
posts query_posts( array( 'post_type' => 'products', 'category_name' => $cat ) );

Creating a template for this is a solution, but I think it will only work efficiently if you have small number of categories.

Try using:

http://wordpress.org/plugins/wp-no-category-base/

http://wordpress.org/plugins/top-level-cats/

I think there are other available plugins to do this.

Try this

<?php 
            global $wp_query;
            $id = $wp_query->post->ID;
            $the_query = new WP_Query( $id );
            $count = 0;
            // The Loop
    query_posts('cat=22'); #enter your category id here
            if ( $the_query->have_posts() ) {
            while ($the_query->have_posts()) : the_post();
            $the_query->the_post();?>
            <div class="page-header"><h3><a href="<?php the_permalink();?>"><?php the_title();?></a></h3>    
            </div>
            <?php
            $count++;
    endwhile; 
            } 
    else 
    {
    ?>
            no posts found
            <?php 
    }
            /* Restore original Post Data */
            wp_reset_postdata();
            ?>

It's not entirely clear what you're looking for, since pages can't have single posts. They're two different things.

If you're looking for a way to create a template for a single custom post type, you can view an interactive version of the hierarchy here.

You have to name the file based on the id of the custom post type. So if the id of the post type is 'service', then 'single-service.php' would display the single post where post type equals service.

You can also place logic into your 'single.php' to say something like

if(get_post_type(get_the_ID()) == 'service'){
    // DO SERVICE-SPECIFIC STUFF HERE
}

This is usually for single custom post types that don't have their own template, but it can also be used as a fallback if that's something you're worried about.

Then you can use an else for regular post stuff and put stuff common to both outside of the if/else.

You can simply fetch it with the function :

<?php query_posts( 'cat=x' ); ?>

where "x" is your category ID

This function offert multiple advantages as you can filter and order the result


Get category 3 posts from 2004

<?php query_posts( 'cat=3&year=2004' ); ?>

Get category 1 posts from 2004 and order by date ASCENDING.

<?php query_posts( 'cat=1'.'&year=2004&orderby=date&order=asc'); ?>

Want to retrieve the actual category ? Simply use :

<?php 

$catId = get_cat_ID('MYCATEGORY'); //get current category id

query_posts( 'cat='.$catId ); // query the posts

if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<h1>
    <a href="<?php the_permalink(); ?>">
        <?php the_title(); ?>
    </a>
</h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
Comments


Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .