WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type Link Template Tag (2 posts)

  1. smnsaysdesign
    Member
    Posted 1 year ago #

    Hi All,

    I'm using Custom Post Types instead of categories in my post lists + archives and etc.

    I was able to figure out how to output "singular_name" but now I need it to be wrapped in a link that the user can click on to be taken to the archive page of that particular post type.

    So basically, if someone clicks on the "FOOD & DRINK" post type name I need it to direct them to that particular list of post types just as you would with the category link tag.

    I've been trying to figure this out for a bit, so please forgive my writing. I look forward to your help. Thanks so much.

  2. Connor Miles
    Member
    Posted 1 year ago #

    Hi smnsaysdesign,

    im sure there are many ways to do this, but here is my solution.

    create a new file in your theme folder called your-custom-post-type-archives.php

    add the following code in that file :

    <?php /* Template Name: Custom Post Type Archive */
    get_header(); ?>
    
    <?php
    global $query_string;
    query_posts($query_string . "post_type=YOUR-CUSTOM-POST-TYPE&post_status=publish&posts_per_page=10");
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    <?php the_excerpt(); ?>
    
    <?php endwhile;
    endif; ?>
    <div class="navigation">
    	<div class="alignleft"><?php next_posts_link('Previous entries') ?></div>
    	<div class="alignright"><?php previous_posts_link('Next entries') ?></div>
    </div>
    <?php wp_reset_query(); ?>
    
    <?php get_sidebar(); ?>
    <?php get_footer();?>

    REMEMBER TO REPLACE YOUR-CUSTOM-POST-TYPE WITH THE NAME OF YOUR CUSTOM POST TYPE

    and then create a new page in wp dashboard and select Custom Post Type Archive as the page template.

    Then you should have what you want.

    Good luck

Topic Closed

This topic has been closed to new replies.

About this Topic