WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] $post Object Disappeared (3 posts)

  1. roflman79
    Member
    Posted 3 years ago #

    I have created a custom post type called 'fabrics' and then a template called 'fabric.php'. Then in 'functions.php' i have added the following code:

    register_post_type('fabrics', array(
    	'label' => __('Fabrics'),
    	'singular_label' => __('Fabric'),
    	'public' => true,
    	'show_ui' => true,
    	'capability_type' => 'page',
    	'query_var' => false,
    	'_builtin' =>  false,
    	'rewrite' => array("slug" => "fabrics"),
    	'supports' => array('title', 'editor', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes', 'revisions')
    ));
    
    add_action("template_redirect", 'my_template_redirect');
    
    function my_template_redirect()
    {
    	global $wp;
    	global $wp_query;
    	if ($wp->query_vars["post_type"] == "fabrics")
    	{
    		if (have_posts())
    		{
    			include(TEMPLATEPATH . '/fabric.php');
    			die();
    		}
    		else
    		{
    			$wp_query->is_404 = true;
    		}
    	}
    }

    This works fine but i can't access anything in the $post object and because I have custom fields I can't get $post->ID for some strange reason. What am i doing wrong?

  2. keesiemeijer
    moderator
    Posted 3 years ago #

    Try:
    global $post; // if outside the loop

  3. roflman79
    Member
    Posted 3 years ago #

    That's fixed it! Thanks so much Keesie.

    I put it after get_header() in my template and hey presto!

Topic Closed

This topic has been closed to new replies.

About this Topic