WordPress.org

Support

Support » How-To and Troubleshooting » Saving and Display Custom Post Type information

Saving and Display Custom Post Type information

  • Having a couple of problems with custom post types, I’m trying to create a type called gallery that well display title, content and image files.

    I’ve got the post type registered and added a text/meta box to add the url of the image file. But I’m having two problems.

    One the url of the image file doesn’t save when the post is updated or published.

    Here is the code from the functions file that registers the post type and is suppose to save the url.

    add_action('init', 'gallery_register');
    
    function gallery_register() {
        	$args = array(
            	'label' => __('Gallery'),
            	'singular_label' => __('Gallery'),
            	'public' => true,
            	'show_ui' => true,
            	'capability_type' => 'post',
            	'hierarchical' => false,
            	'rewrite' => true,
            	'supports' => array('title', 'editor', 'thumbnail')
            );
    
        	register_post_type( 'gallery' , $args );
    }
    
    add_action("admin_init", "admin_init");
    add_action('save_post', 'save_imageurl');
    
    function admin_init(){
    		add_meta_box("prodInfo-meta", "Gallery Options", "meta_options", "gallery", "low");
    	}
    
    	function meta_options(){
    		global $post;
    		$custom = get_post_custom($post->ID);
    		$imageurl = $custom["imageurl"][0];
    ?>
    	<label>Image URL:</label><input size="75" name="imageurl" value="<?php echo $imageurl; ?>" />
    <?php
    	}
    
    function save_imageurl(){
    	global $post;
    	update_post_meta($post->ID, "imageurl", $_POST["imageurl"]);
    }

    Thought I don’t think that the url is saving, because when you go into the post nothing shows up in the url text field.

    The second problem, is this the way to get the image url out of the data base for use on the page template?

    <?php
    
    query_posts(array( 'post_type' => array('gallery') ));
    
    if ( have_posts() ) : while ( have_posts() ) : the_post();?>
    
    <?php
         $custom = get_post_custom($post->ID);
         $imageurl = "$". $custom["imageurl"][0];  
    
    ?> 
    
    	<?php the_content();?>
    
    		<img src="<?php echo $imageurl;?>">
    
    <?php endwhile; else: ?>
    	<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    Thanks

  • The topic ‘Saving and Display Custom Post Type information’ is closed to new replies.
Skip to toolbar