WordPress.org

Ready to get started?Download WordPress

Forums

Get all posts from custom taxonomy (1 post)

  1. godless_phoenix
    Member
    Posted 1 year ago #

    add_action( 'init', 'custom_gallery_post_type' );
    function custom_gallery_post_type() {
        register_post_type( 'custom_gallery',
            array(
                'labels' => array(
                    'name' => __( 'Gallery', 'custum_gallery' ),
                    'singular_name' => __( 'Gallery Item', 'custum_gallery' ),
                ),
                'has_archive' => true,
                'hierarchical' => true,
                'public' => true,
                'rewrite' => array( 'slug' => 'custum_gallery' ),
    			'taxonomies' => array('custom_gallery_category'),
    			'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
             )
        );
    	register_taxonomy('custom_gallery_category', 'custom_gallery',
    		array(
    			'hierarchical' => true,
    			'query_var' => true,
    			'rewrite' => true,
                'has_archive' => true,
                'hierarchical' => true,
                'public' => true
    			)
    		);
    }
    
    function customgallery( $atts, $content = null ) {
    	$content='<ul class="gallery_menu">';
    	$_categories = get_categories('taxonomy=custom_gallery_category');
    	foreach ($_categories as $_cat){
    		$content=$content.'
    			<li class="'.$_cat->name.'" >
    				<a href="'.get_term_link( $_cat->slug, 'custom_gallery_category' ).'" title="'.$_cat->name.'">'.$_cat->name.'</a>
    			</li>';
    		}
    	$content.='
    		</ul>
    		<div style="clear:both;"></div>
    		<ul class="gallery_item">'; 
    
    	$_item = new WP_Query(array('post_type' => 'custom_gallery', 'posts_per_page' => '-1'));
    		while ($_item->have_posts()) : $_item->the_post();
    		$content.='<li><a href="'.get_permalink().'" rel="bookmark" title="'.get_the_title().'"><img src="'.wp_get_attachment_image_src(get_post_thumbnail_id($_item->ID),'thumbnail')[0].'"></a>';
    		$content.='<a class="gallery_title" href="'.get_permalink().'" rel="bookmark" title="'.get_the_title().'">'.get_the_title().'</a></li>';
    
    		endwhile;
    
    		$terms = get_terms('custom_gallery_category');
    
    		$content.=$terms->slug;
    
    	return $content.'</ul>'.'<div style="clear:both;"></div>';
    }
    add_shortcode('customgallery', 'customgallery');

    its working good with any theme which supports featured image.
    i can get posts for sub-categories on index.php
    i can get single posts on single.php
    I want to get all taxonomy posts without using taxonomy.php or any external template. So is it possible to get all custom taxonomy posts on index.php?

    Example:
    wordpress/?custom_gallery_category=gallery-test-1
    Gives the result on index.php.
    I want to dosomething like that
    wordpress/?custom_gallery_category=is there a tag for this like 'ALL'

Topic Closed

This topic has been closed to new replies.

About this Topic