WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to display all posts of a custom post type? (6 posts)

  1. Julian245
    Member
    Posted 6 months ago #

    Hi,

    how can I display all posts of a custom post type on a page?

    I hope you can help me.

    Greetings,

    Julian

  2. alchymyth
    Forum Moderator
    Posted 6 months ago #

    create a page template http://codex.wordpress.org/Page_Templates
    add a loop http://codex.wordpress.org/The_Loop
    with a custom query http://codex.wordpress.org/Class_Reference/WP_Query
    using the 'post_type' parameter in the query http://codex.wordpress.org/Class_Reference/WP_Query#Type_Parameters

    you might want to start the code of the page template with a copy of index.php or page.php of your theme...

  3. Julian245
    Member
    Posted 6 months ago #

    Hi,

    I did what the Codex said, but it's still not working. If i go to elexode.nl/music (my archive page) The title says 'Page not found'.

    Maybe I made a mistake in my codes:

    Archive-music.php

    <?php
    
    /*
     Template Name: Music
     */
     get_header(); ?>
    
    <div class='eleven columns'>
    	<?php
    		$my_query = new WP_Query( "cat=3" );
    			if ( $my_query->have_posts() ) {
    				while ( $my_query->have_posts() ) {
    					$my_query->the_post();
    						the_content();
    				}
    			}
    		wp_reset_postdata();
    	?>
    </div>
    <div class='four columns offset-by-one'>
    	<?php get_sidebar('right-sidebar'); ?>
    </div>
    <?php get_footer(); ?>

    Music.php (to register the post type)

    <?php 
    
    function elex_posttype_music() {
    	$labels = array(
    		'name' => 'Music',
    		'singular_name' => 'Music',
    		'add_new' => 'Add Track',
    		'add_new_item' => 'Add New Track',
    		'edit_item' => 'Edit Track',
    		'new_item' => 'New Track',
    		'view_item' => 'View Track',
    		'search_items'=> 'Search Tracks',
    		'not_found' => 'No Tracks Are Found',
    		'not_found_in_trash'=> 'No Tracks Are Found In Trash',
    		'view' => 'View Track'
    	);
    
    	$args = array (
    		'labels'=> $labels,
    		'description' => 'This is the holding location for your tracks',
    		'public'=> true,
    		'has_archive' => true,
    		'exclude_from_search'=> false,
    		'show_ui' => true,
    		'rewrite'=> true,
    		'hierarchical' => true,
    		'menu_position'=> 5,
    		'supports' => array('thumbnail', 'title', 'editor')
    	);
    
    	register_post_type('music',$args);
    
    }
    
    add_action('init', 'elex_posttype_music')
    
    ?>

    functions.php
    include_once(TEMPLATEPATH . '/inc/posttypes/music.php');

  4. alchymyth
    Forum Moderator
    Posted 6 months ago #

    your query does not contain the parameter for 'post_type';

    try:

    $my_query = new WP_Query( "cat=3&post_type=Music" );

    do you need the cat parameter?

  5. Julian245
    Member
    Posted 6 months ago #

    Oh no, the cat par was a mistake.

    I edited my code, but it's still not working...

    I could share my project if you want?

  6. Julian245
    Member
    Posted 6 months ago #

    Its working, problem solved ;-)

Reply

You must log in to post.

About this Topic