WordPress.org

Ready to get started?Download WordPress

Forums

How to upload image on publishing of a custom post (19 posts)

  1. Matt25
    Member
    Posted 3 years ago #

    Hi, I have set up a custom post type in wordpress called 'Bike' and I am trying to basically make it so that when I create a 'new bike' in wordpress it uploads a picture of it as a thumbnail when I list the bikes on the site.

    I tried using the 'add_theme_support( 'post-thumbnails', array('bike' );' but I cant seem to get it to work with the custom post type?

    My other thought was to create a function to upload the image specified in the meta box upon publishing the bike but I can't work out what I would put in the add_action call because this would call when you published a post but not my custom post would it?
    add_action('publish_post', 'image_upload');

    Could anyone point me in the right direction on how to do this? or how to make these things work?

    Cheers,
    Matt

  2. richarduk
    Member
    Posted 3 years ago #

    Are you writing a custom post with a thumbnail / featured image? And then displaying a list of posts or a list of thumbnails in a multiple-post page e.g. category.php ? And is that multiple-post page just for the custom post type 'bike'?

  3. Matt25
    Member
    Posted 3 years ago #

    In short, Yes.

    Basically what I want is that it is going to be like an e-commerce but without the e-commerce stuff in that It will all be categorised with the bikes different makes and things but it wont have a page to buy the bikes. So if I filtered out a all the Trek Hard tail bikes it would list all the Trek hard tails with a picture of it next each of them if that makes sense?
    Cheers,
    Matt

  4. richarduk
    Member
    Posted 3 years ago #

    Hmm. Not sure why you need a custom post for that? A category page would surely do the trick. E.g. if Trek Hard bikes are assigned to the category Trek Hard with an ID of 7, then category-7.php would be the category template file that you'd mess around with to show your thumbnails.

  5. Matt25
    Member
    Posted 3 years ago #

    I just thought it would be easier to just have 'add bike' on the dashboard rather than adding a post and it will make creating a blog if we wanted to easier in the long run. I have setup categories for the bikes custom post so I have the makes and things.
    Cheers,
    Matt

  6. richarduk
    Member
    Posted 3 years ago #

    You sound as if you're on the right track. It seems to me (not knowing exactly what your code looks like) that you might not be sending your custom posts to the right template, or that you're not putting the right code on that template.
    This (from my notes) might be helpful:

    SINGLE custom posts can be shown in their own template file by naming it with the custom post name prefixed by single- e.g. single-review.php
    If this doesn't seem to be working then resave your permalinks i.e. Settings / Permalinks / Save Changes
    You can also send your single posts to any template file you want from within single.php by deleting single-review.php and using something like:

    elseif (get_post_type() == 'review') {include(TEMPLATEPATH . '/oranges.php');}     See single.php for more details.

    MULTIPLE custom posts on one page can be shown on any existing theme file e.g. index.php category.php using something like either of these:

    <?php $loop1 = new WP_Query('order=DESC&posts_per_page=5&post_type=review'); ?>
    <?php if ($loop1->have_posts()) :  while ($loop1->have_posts()) : $loop1->the_post(); ?>
    
    <?php $index_loop = new WP_Query( array( 'post_type' => 'review', 'posts_per_page' => 10 ) ); ?>
    <?php if ($loop1->have_posts()) :  while ($loop1->have_posts()) : $loop1->the_post(); ?>

    To LINK to a specific multiple-post page that will show only posts of one custom post type create a category, and then add each post of that type to the category.
    If the category has an ID of 28 then create a category template called category-28.php and on that file use something like this before the loop:

    <?php   $review_loop = new WP_Query('order=DESC&posts_per_page=5&post_type=review');?>

    You can then link to that page from any other page using:
    <?php wp_list_categories('title_li=&include=28'); ?>
    See: http://codex.wordpress.org/Template_Tags/wp_list_categories

    If you want your default category template category.php to show posts from different custom post types depending on which link someone has arrived from, then use something like this:

    <?php
    	if (is_category(28))
    			{ $loop1 = new WP_Query('order=DESC&posts_per_page=5&post_type=review'); }
    		else
    			{ $loop1 = new WP_Query('order=DESC&posts_per_page=5&post_type=book_awards'); }
    		?>
    
    <?php if ($loop1->have_posts()) :  while ($loop1->have_posts()) : $loop1->the_post(); ?>

    If you want a multiple-post page such as category.php or index.php to show ALL post types then use something like this (where book_awards and review are custom post types and post is the default post type)

    <?php   $new_loop= new WP_Query(array('order' => DESC,  'post_type' => array('post', 'review', 'book_awards'), 'posts_per_page' => 10, 'paged' =>$paged));?>
    <?php if ($new_loop->have_posts()) :  while ($new_loop->have_posts()) : $new_loop->the_post(); ?>
  7. Matt25
    Member
    Posted 3 years ago #

    Awesome thanks for that, it helped a lot and I have the pages and catagories set up how I want them now.

    The thing I am trying to work out now is that when I display the bikes on a multiple post page I want to have an image of the bike next to each of them. It seems the easiest way to do this is to enable the post thumbnails but I can't get it to work with the custom post type, this is the code I have:

    add_theme_support( 'post-thumbnails', 'Bikes');
    set_post_thumbnail_size( 192, 169 );

    But it doesn't seem to be coming up in the UI when I add a bike and I can't work out why

    Thanks for the help,
    Matt

  8. richarduk
    Member
    Posted 3 years ago #

    I would try simple stuff first.

    In your template file, <?php the_post_thumbnail('thumbnail'); ?>

    In functions.php

    if( function_exists(add_theme_support) ){
    add_theme_support( 'post-thumbnails' );
    }

    and if that doesn't work

    if( function_exists(add_theme_support) ){
    add_theme_support( 'post-thumbnails', array( 'post', 'bike') );
    }

    Make sure that where it says bike it's the database entry that WP uses, not what's displayed in the admin screen

  9. Matt25
    Member
    Posted 3 years ago #

    Thanks, I have now got it to work. I do now have another problem though
    I need to make the image a link to the website of the bike but for some reason instead of going to Trek.co.uk it goes to http://wordpress.digital-spoon.co.uk/www.trek.co.uk
    again I can't work out why though, this is my code

    $loop = new WP_Query(array('post_type' => 'Bikes', 'posts_per_page' => 10));
    while ( $loop->have_posts() ) : $loop->the_post();
    
    	$custom = get_post_custom($post->ID);
    	$bike_url = $custom["bike_url"][0];
    	$bike_img = $custom["bike_img"][0];
            $bike_price = $custom['bike_price'][0];
    
    ?>
    	<div id="bike">
    	<h1 id="bike_id"><?php the_title(); ?></h1>
    
    	<a id="bike_img" href="<?php echo $bike_url ?>"><img alt="<?php echo $bike_url ?>"  height="169px" width="192px" src="<?php bloginfo('wpurl'); ?>/wp-content/uploads/<?php echo $bike_img ?>" /></a>
    	<?php the_content();
            echo $bike_price;
            echo $bike_url; ?>

    Thanks,
    Matt

  10. richarduk
    Member
    Posted 3 years ago #

    All I can suggest is visiting the settings/permalink page (which should be enought to reset permalinks) or saving the existing permalinks.

    :-)

  11. Matt25
    Member
    Posted 3 years ago #

    I have tried that but its still doing it, I just can't work out why wordpress would be inserting stuff into a hard coded url.
    Cheers,
    Matt

  12. richarduk
    Member
    Posted 3 years ago #

    Have you echoed that url elsewhere (I'm sure you have) just to check e.g. outside the loop? Have you made sure (again, I'm sure you have) that the variable doesn't exist in a different form somewhere else? Have you checked the .htaccess for redirects? Or an .htaccess in a higher folder (they have a habit of sneaking up there and making themselves invisible :-))

  13. Matt25
    Member
    Posted 3 years ago #

    I don't know much about .htaccess but this is all I have in mine:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    and I can't find any others. I have and it echos nothing outside of the loop so it can't be already being used.
    Cheers,
    Matt

  14. richarduk
    Member
    Posted 3 years ago #

    The top block of code looks identical to the bottom block. Try commenting it out or removing it.

    What about other urls? Are they affected?

  15. Matt25
    Member
    Posted 3 years ago #

    Yes I know, I tried deleting it but it appeared again for some reason.
    I have just tested and it seams any link i put into the template file does the exact same thing and has the site url put infront of it.
    Cheers,
    Matt

  16. richarduk
    Member
    Posted 3 years ago #

    That's the problem, then. I'd contact my host.

  17. Matt25
    Member
    Posted 3 years ago #

    I have contacted my host but then I went and checked my local version of the site and it happens on that aswell, so it can't be the host can it?
    Thanks, I really appreciate the help,
    Matt

  18. richarduk
    Member
    Posted 3 years ago #

    Unless anyone else has better ideas, it's going to have to be a process of elimination then. (Welcome to the world of web development :-))

    First, it looks to me like it might be a rewrite problem, and that might be because (from the url you gave) you appear to have a subdomain. I'd Google for any know difficulties or gotchas with WP on subdomains. Also, check that .htaccess has correct rewrite permissions for permalinks.

    Then I'd see if the default theme worked on the hosted site. If it didn't, then something's wrong on the hosted site. I'd probably try disabling plugins etc. and overwriting (FTP) existing files. If that didn't work, I'd probably delete and start again from scratch, just to save time going round in circles.

    If the default does work on the hosted site, I'd download that default and see if I could get it to work on my local computer. If I couldn't, something's wrong with the local computer.

    I personally don't like developing locally. Too many ifs and buts. I have an 'addon' domain on my host, password protected so that Google can't get to it, and that's my test site.

    Afraid you're on your own now - but let us know what the problem was!
    Best of luck with it!

  19. Matt25
    Member
    Posted 3 years ago #

    Thanks very much for the help, it seemed to have permanently slipped my mind about having to use http:// to get to an external site which was obviously stopping it. I have now put it into my code so you can't not have it,

    Cheers for the help,
    Matt

Topic Closed

This topic has been closed to new replies.

About this Topic