• I’m not a programmer so please excuse my ‘noviceness’.

    The site I run has two custom post types. One of them is ‘portfolio-category’ (slug).
    I wanted to have a page where I can show only the particular custom posts categorised by the custom taxonomy (a set of categories) for that post type. I looked everywhere but couldn’t find anything so I ‘hacked’ around and found one very silly way to make it work.

    <h3>Weddings</h3>
    <ul>
    <?php
    global $post;
    $args = array( 'post_type' => 'portfolio', 'portfolio-category' => 'wedding' );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :	setup_postdata($post); ?>
    <li class="_archive"><a href="<?php the_permalink(); ?>" class"_archivelink"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    Basically, I repeated this snippet for each category including the heading (static). Stupid, I know but not having learned PHP from ground up I have no idea how to make this more elegant. I can only guess that I could use Loop function.. That’s as far as I went and I’m giving up after spending pretty much all day on this.

    I’m sure there are experts out there who could advise me what to do?

    Thank you for your kind help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Is this custom taxonomy (portfolio-category) only used by your portfolio post type?
    If so, there are category archive lists are already created for you at (for example) https://www.url.com/portfolio-category/weddings/

    If you want to make a custom template for these archives you can copy archive.php and rename it to taxonomy-portfolio-category.php (I think, or taxonomy-portfolio_category.php). Should automatically pick it up.

    Thread Starter sobaksobak

    (@sobaksobak)

    sorry, I should have my question more clear.

    https://songyknox.com/portfolio-list/
    This is the page I created but using the following.

    <h1><?php the_title(); ?></h1>
    				<h3>Weddings</h3>
    					<ul>
    						<?php
    						global $post;
    						$args = array( 'post_type' => 'portfolio', 'portfolio-category' => 'wedding' );
    						$myposts = get_posts( $args );
    						foreach( $myposts as $post ) :	setup_postdata($post); ?>
    						<li class="_archive"><a href="<?php the_permalink(); ?>" class"_archivelink"><?php the_title(); ?></a></li>
    						<?php endforeach; ?>
    					</ul>
    
    				<h3>Baby</h3>
    					<ul>
    						<?php
    						global $post;
    						$args = array( 'post_type' => 'portfolio', 'portfolio-category' => 'baby' );
    						$myposts = get_posts( $args );
    						foreach( $myposts as $post ) :	setup_postdata($post); ?>
    						<li class="_archive"><a href="<?php the_permalink(); ?>" class="_archivelink"><?php the_title(); ?></a></li>
    						<?php endforeach; ?>
    					</ul>
    				<h3>Engagement</h3>
    					<ul>
    						<?php
    						global $post;
    						$args = array( 'post_type' => 'portfolio', 'portfolio-category' => 'engagement' );
    						$myposts = get_posts( $args );
    						foreach( $myposts as $post ) :	setup_postdata($post); ?>
    						<li class="_archive"><a href="<?php the_permalink(); ?>" class="_archivelink"><?php the_title(); ?></a></li>
    						<?php endforeach; ?>
    					</ul>
    				<h3>Catwalk</h3>
    					<ul>
    						<?php
    						global $post;
    						$args = array( 'post_type' => 'portfolio', 'portfolio-category' => 'catwalk' );
    						$myposts = get_posts( $args );
    						foreach( $myposts as $post ) :	setup_postdata($post); ?>
    						<li class="_archive"><a href="<?php the_permalink(); ?>" class="_archivelink"><?php the_title(); ?></a></li>
    						<?php endforeach; ?>
    					</ul>
    
    				<h3>Event</h3>
    					<ul>
    						<?php
    						global $post;
    						$args = array( 'post_type' => 'portfolio', 'portfolio-category' => 'event' );
    						$myposts = get_posts( $args );
    						foreach( $myposts as $post ) :	setup_postdata($post); ?>
    						<li class="_archive"><a href="<?php the_permalink(); ?>" class="_archivelink"><?php the_title(); ?></a></li>
    						<?php endforeach; ?>
    					</ul>
    
    				<h3>Interior</h3>
    					<ul>
    						<?php
    						global $post;
    						$args = array( 'post_type' => 'portfolio', 'portfolio-category' => 'interior-photography' );
    						$myposts = get_posts( $args );
    						foreach( $myposts as $post ) :	setup_postdata($post); ?>
    						<li class="_archive"><a href="<?php the_permalink(); ?>" class="_archivelink"><?php the_title(); ?></a></li>
    						<?php endforeach; ?>
    					</ul>
    				<h3>Portrait</h3>
    					<ul>
    						<?php
    						global $post;
    						$args = array( 'post_type' => 'portfolio', 'portfolio-category' => 'portrait' );
    						$myposts = get_posts( $args );
    						foreach( $myposts as $post ) :	setup_postdata($post); ?>
    						<li class="_archive"><a href="<?php the_permalink(); ?>" class="_archivelink"><?php the_title(); ?></a></li>
    						<?php endforeach; ?>
    					</ul>

    It’s repeating the same thing for every category. I’m sure there would be a way to make them dynamic for <h3> and the post listing for each category. I hope this makes sense.

    Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom post categories listing with loop?’ is closed to new replies.