Custom Post Type Page Attributes (order)


#1

Hello

On my new theme I’m coding into WordPress, I’m using custom post types for the homepage sliders.

I gave my custom post type the ability to edit the page attributes like order. But, for some reason, when I query them on the homepage, they don’t display in the order I specified within the custom post type.

If I view the custom post type page, it lists them in the order that I specified, but in the slider, it doesn’t show them in the order that I set them as. It still lists them according to date.

Does anybody know how I can fix this?

Here’s my code:

<?php $loop = new WP_Query( array( 'post_type' => 'slider_item' ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
	
  • <?php the_post_thumbnail("full", array('class' => 'left')); ?>

    <?php the_title(); ?>

    <?php the_content(); ?>

  • <?php endwhile; ?>

    I don’t know if that has anything to do with it but just posting it just in case.


    #2

    Try adding “orderby=menu_order” in your query string.

    So:

    
    <?php $loop = new WP_Query( 'post_type=slider_item&orderby=menu_order' ) ); ?>
    
    

    http://codex.wordpress.org/Function_Reference/query_posts


    #3

    Hmm

    That changed the custom post type from being displayed to just regular posts being displayed?


    #4

    Try putting back as an array like you had it.

    
    <?php $loop = new WP_Query( array( 'post_type' => 'slider_item', 'orderby' => 'menu_order' ) ); ?>
    
    

    #5

    Ok… I changed it to

    <?php $loop = new WP_Query( array( 'post_type' => 'slider_item', 'orderby' => 'menu_order' ) ); ?>
    

    Now it does list it by order, but backwards for some reason? If I make a slide order 1 out of 3, it shows that one last, and the 3rd one first, and 2nd one second. So basically 321 instead of 123.

    Know how I can fix this?


    #6

    OK I got it

    I changed the code to:

    <?php $loop = new WP_Query( array( 'post_type' => 'slider_item', 'orderby' => 'menu_order', 'order' => 'ASC' ) ); ?>
    

    Thanks for your help :slight_smile:


    #7
    Ok.. I changed it to
    <?php $loop = new WP_Query( array( 'post_type' => 'slider_item', 'orderby' => 'menu_order' ) ); ?>
    

    Now it does list it by order, but backwards for some reason? If I make a slide order 1 out of 3, it shows that one last, and the 3rd one first, and 2nd one second. So basically 321 instead of 123.

    Know how I can fix this?

    I think you need to set the order parameter to ASC (or possibly DESC)

    <?php $loop = new WP_Query( array( 'post_type' => 'slider_item', 'orderby' => 'menu_order' , 'order'=>'ASC') ); ?>

    See this page for more details http://codex.wordpress.org/Function_Reference/query_posts#Order_Parameters

    EDIT: Beat me to it :slight_smile:


    #8

    Haha thanks anyway :slight_smile:

    Now does anybody know how to resize thumbnails?

    When I set a featured image, it asks the size I want to use. Is there anyway to overwrite this when echoing the_post_thumbnail to change the size?

    I read it was supposed to be like:

    <?php
    the_post_thumbnail(array(960, 300));
    ?>
    

    But this doesn’t resize it? Am I missing something or doing something wrong or is that not what it’s supposed to do or what?


    #9

    No, that is what you’re supposed to do.

    When Wordpress resizes the image it will keep the aspect ratio of the original image. So if the ratio of the original image is different than 32:10, the image will just resize to a width of 960px and an auto height rather than distort or clip the image. Other than that, I have no idea what your problem is.


    #10

    OK so there’s absolutely no way to resize thumbnails or remove the height="" and width="" from the code that WP outputs through the_post_thumbnail() function?

    Also, I’m trying to add a custom section to pages where they can enter a page sub title. Here’s the code I have for the add_meta_box:

    <?php
    
    function admin_init_page_options(){  
            add_meta_box("pageInfo-meta", "Page Sub Title", "page_title", "page", "normal", "high");  
        }  
    
    function page_title(){  
    	global $post;
    	$nav_span = get_post_meta($post->ID, 'page_title', true);
    	echo 'Page Sub Title:';
    }
    
    function save_page_title($post_id) {
    	global $post;
    	
    	if(isset($_POST['post_type']) && ($_POST['post_type'] == "page")) {
    		$data = $_POST['page_title'];
    		update_post_meta($post_id, 'page_title', $data);
    	}
    }
    add_action('admin_init', "admin_init_page_options");
    add_action('save_post', 'save_page_title');
    ?>
    

    It’s not adding when I go to edit a page. Any clue why?

    This is the code for adding custom sections to my custom post types. Do I need to change $post to $page or something?


    #11

    How about google? Or the wordpress codex?

    There really are 99.9 % of all the answers to your questions. You have to do some research yourself, you know - we cannot built your theme.

    Don’t take it personal, it is ok to ask questions - but I got the feeling the community here builds your theme. :slight_smile:


    #12

    All you’ve done is say to Google or check the codex. Trust me, I check the codex. That’s always my first resource but it doesn’t answer all of my questions. I tried using the code from the WordPress codex and it hasn’t worked for me.

    So if you’d maybe like to help me instead of telling me to check the codex 50 times?


    #13

    http://codex.wordpress.org/Function_Reference/add_image_size => http://codex.wordpress.org/Function_Reference :slight_smile:


    #14

    haha thanks :slight_smile:


    #15

    thanks for you post CadenGrant, Your post was helpful to me