DotDragnet
February 08, 2012, 08:06:00 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Mobile users - Our forum is Tapatalk enabled. http://www.tapatalk.com/
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: wordpress custom post type  (Read 424 times)
SLEE
Hero Member
*****
Posts: 816



View Profile WWW Awards
« on: July 30, 2010, 11:19:08 PM »

OK so i have used the following code to create a custom post type:


add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type('tfh_news',
   array(
   'label' => __('News'),
   'singular_label' => __('News'),
   'public' => true,
   'show_ui' => true,
   'capability_type' => 'post',
   'hierarchical' => false,
   'rewrite' => false,
   'query_var' => false,
   'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail')
));
}
 
This works fine but I cannot for the life of me figure out how to create a page template to output a list of them.
I have tried every example i can find but no joy please can someone show me how to do it

Thanks
Logged

SLEE - still the most confusing ddner...

Follow me on Twitter
sarahA
DDN Contribs
Hero Member
*****
Posts: 2123



View Profile WWW Awards
« Reply #1 on: August 01, 2010, 08:38:17 AM »

not used them yet (although certainly will be giving them a try as I can see a possible solution for something in them!)

According to http://codex.wordpress.org/Function_Reference/register_post_type singular_label doesn't exist as an argument. You need to change 'label' to 'labels' and feed the plural and singular values in via an array (http://codex.wordpress.org/Custom_Post_Types). Any reason why you've set rewrite and query_var to false? Don't know if it affects things either way, just wondered smile

In regards to output - daft question but have you used the basic example at http://codex.wordpress.org/Custom_Post_Types and tried to get that working first? and then used that to create your own code? I usually find (not always, bear in mind the Codex isn't 100% accurate all of the time, especially not with new pages!) that doing that, getting that to work and then working from there is the best method.

If if I get time later I may have a play with this as it'll be handy to know, so if you're still having problems I may (note may!) have a bit more experience and get things working myself smile
Logged

SLEE
Hero Member
*****
Posts: 816



View Profile WWW Awards
« Reply #2 on: August 03, 2010, 09:20:22 AM »

All sorted I put this in my functions file:

Code:
/* custom post type for news */


add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type('tfh_news',
array(
'label' => __('News'),
'singular_label' => __('News'),
'public' => true,
'show_ui' => true,
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'news', 'with_front' => false ),
'supports' => array('title', 'editor', 'excerpt', 'author', 'thumbnail'),
'menu_position' => NULL,
'can_export' => true,

));
}

and to do the output i used:
Code:
<?php
 
$loop = new WP_Query( array( 'post_type' => 'tfh_news''posts_per_page' => 10 ) );
while ( 
$loop->have_posts() ) : $loop->the_post(); ?>


Logged

SLEE - still the most confusing ddner...

Follow me on Twitter
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!