Forums

[resolved] Another WP 3.1 CPT Archive Pagination Problem (24 posts)

  1. hoss9009
    Member
    Posted 11 months ago #

    K... here's my CPT:

    register_post_type('web', array('label' => 'Websites','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => true,'rewrite' => array('slug' => 'portfolio/web', 'has_archive' => 'archive-web'),'query_var' => true,'menu_position' => 5,'supports' => array('title','thumbnail'),'labels' => array (
      'name' => 'Websites',
      'singular_name' => 'Website',
      'menu_name' => 'Websites',
      'add_new' => 'Add a Website',
      'add_new_item' => 'Add a New Website',
      'edit' => 'Edit',
      'edit_item' => 'Edit Website',
      'new_item' => 'New Website',
      'view' => 'View Website',
      'view_item' => 'View Website',
      'search_items' => 'Search Websites',
      'not_found' => 'No Websites Found',
      'not_found_in_trash' => 'No Websites Found in Trash',
      'parent' => 'Parent Website',
    ),) );

    Here's my loop:

    <?php $loop = new WP_Query( array( 'post_type' => 'web', 'posts_per_page' => 1 ) ); ?>
    <?php if ( $loop->have_posts() ) : ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="portfolio-item">
        <div class="portfolio-thumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="Portfolio item: <?php the_title_attribute(); ?>"><?php the_post_thumbnail('portfolio-thumb'); ?></a></div>
        <h4 class="portfolio-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Portfolio item: <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
    </div>
    <?php endwhile; ?>
    <?php wp_pagenavi( array( 'query' => $loop ) ); ?>
    <?php wp_reset_postdata(); // reset the query ?>
    <?php endif; ?>

    I am using archive-{posttype}.php and am not using page templates. I'm trying to go 100% 3.1.
    As you can see I want my CPT w/ the slug page/post-type and I don't know if this is the problem, but I have killed the page for testing and I still can't get pagination to work.

    Please help!?!

    Thanks

  2. hoss9009
    Member
    Posted 11 months ago #

    ... and yes... I made sure I was saving permalinks. :)

  3. keesiemeijer
    moderator
    Posted 11 months ago #

    see if this helps:

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $loop = new WP_Query( array( 'post_type' => 'web', 'posts_per_page' => 1, 'paged' => $paged ) ); ?>
  4. hoss9009
    Member
    Posted 11 months ago #

    I did this...

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $loop = new WP_Query( array( 'post_type' => 'web', 'posts_per_page' => 1, 'paged' => $paged ) ); ?>
    
    <?php if ( $loop->have_posts() ) : ?>
    
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="portfolio-item">
    
        <div class="portfolio-thumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="Portfolio item: <?php the_title_attribute(); ?>"><?php the_post_thumbnail('portfolio-thumb'); ?></a></div>
    
        <h4 class="portfolio-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Portfolio item: <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
    
    </div>	
    
    <?php endwhile; ?>
    <?php wp_pagenavi( array( 'query' => $loop ) ); ?>
    
    <?php wp_reset_postdata(); // reset the query ?>
    
    <?php endif; ?>

    then saved permalinks.... no go.
    :(

  5. keesiemeijer
    moderator
    Posted 11 months ago #

    try first to see if pagination is working with next_posts_link and previous posts link In stead of wp_pagenavi pagination.

  6. hoss9009
    Member
    Posted 11 months ago #

    Interesting... I put them right next to the wp_pagenavi(which is working - i think), and I get nothing. I even put a comma between the two... all I see is the comma.

  7. keesiemeijer
    moderator
    Posted 11 months ago #

    from twentyten:

    <div id="nav-below" class="navigation">
    					<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
    					<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
    				</div><!-- #nav-below -->

    try it with this.

  8. hoss9009
    Member
    Posted 11 months ago #

    still no go. :(

  9. keesiemeijer
    moderator
    Posted 11 months ago #

    You don't see any links? Did you put them in the right template file? do you have more than one cpt "web" Post published. is your template file archive-web.php? Do you only see cpt "web" Posts on this page?

  10. hoss9009
    Member
    Posted 11 months ago #

    Here's the loop now....

    [Code moderated as per the Forum Rules. Please use the pastebin]

    here's a link.

  11. keesiemeijer
    moderator
    Posted 11 months ago #

    try registering this way: example

  12. keesiemeijer
    moderator
    Posted 11 months ago #

    and put this in stead of the html <body> tag so we can see what template file is being used and more:
    <body <?php body_class($class); ?>>

  13. hoss9009
    Member
    Posted 11 months ago #

    Man oh man... you got it working!!!

    A few things...

    1. The slug is set to true, so "websites" is now a working url on my site along w/ the "portfolio/web". The loop for register_post_type('web',$args); works on both pages. This is good to know, but leads me to #2 on my list.
    2. There is no "slug" (I know it's a rewrite array) and I need it "portfolio/web". I tried changing it and I get the 404 when the select the 2nd page.
    3. When I implemented the registration that you gave me, it only produces the wp_pagenavi on the 1st page. The twentyten nav and wp_pagenavi appear on the 2nd page. Odd.
    4. I did implement the body_class. Thank you for that.

    Link.

    Thoughts?

    Thank you so much for everything thus far.

    PS. Is it wishful thinking that the WP would understand "parent/child" permalinks for CPTs with the currect released build (3.1)?

  14. hoss9009
    Member
    Posted 11 months ago #

    Also, I notice that the single pages of the CPT is:
    http://www.capichedesignstudio.com/web/asfasf/

    If possible, I'm wanting it:
    http://www.capichedesignstudio.com/portfolio/web/asfasf/.

  15. keesiemeijer
    moderator
    Posted 11 months ago #

    use this for the rewriting:

    'rewrite' => array('slug' => 'portfolio/web'),

    change this in your custom post archive template:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $loop = new WP_Query( array( 'post_type' => 'web', 'posts_per_page' => 1, 'paged' => $paged ) ); ?>
    
    <?php if ( $loop->have_posts() ) : ?>
    
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>

    into a normal loop:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    and put this in your theme's functions.php:

    function my_custom_posts_per_page( &$q ) {
    	    if ( $q->is_archive ) // any archive
    	    if($q->query_vars['post_type'] == 'web'){  //custom post type "web" archive
            $q->set( 'posts_per_page', 1 );
    	    }
    	   	return $q;
    	}
    
    add_filter('parse_query', 'my_custom_posts_per_page');

    and re-save your permalink structure.

    your permalinks will be
    archive: http://www.capichedesignstudio.com/portfolio/web/
    single posts: http://www.capichedesignstudio.com/portfolio/web/asfasf/

  16. hoss9009
    Member
    Posted 11 months ago #

    You're amazing.

    It works. 100%. Perfect.

    Can you explain the function? Please?

  17. hoss9009
    Member
    Posted 11 months ago #

    One glitch... my sidebar nav doesn't recognize what page it's on. Thoughts?

  18. keesiemeijer
    moderator
    Posted 11 months ago #

    The WordPress posts per page setting is global, but plugins or functions can alter it based on the type of page you’re viewing. this function alters the query.

    What kind of menu are you using? maybe this is helpfull
    http://codex.wordpress.org/Conditional_Tags#A_Post_Type_Archive

    if(is_post_type_archive( 'web' )){
    // do stuff on "web" archive page
    }
  19. hoss9009
    Member
    Posted 11 months ago #

    It's the sidenav. It's a menu that is generated by WP. I have a class setup for current-page-item or anything w/ "current" in it.

    I'll dig a big, but I'm not too familiar w/ is_post_type_archive.

    Thanks!

  20. keesiemeijer
    moderator
    Posted 11 months ago #

    try this plugin: http://wordpress.org/extend/plugins/cpt-archives-in-nav-menus/

    [edit] works only with WordPress 3.2

  21. hoss9009
    Member
    Posted 11 months ago #

    Hmmmm... not sure if that would work for me... I installed it and looks like it only works for individual posts of archives? Or am I reading it incorrectly?

  22. keesiemeijer
    moderator
    Posted 11 months ago #

    It's something they want to introduce for 3.2: http://core.trac.wordpress.org/ticket/16075

    The only way to do it now is hardcode the menu on custom post type archive pages:

    if(is_post_type_archive()){
    // hardcode links
    } else {
     wp_nav_menu();
    }
  23. hoss9009
    Member
    Posted 11 months ago #

    OK... thank you!!! :)

    Stoked for the final 3.2.

  24. fadedpictures
    Member
    Posted 11 months ago #

    try this function in your functions.php file and then add the name of your CPT to the title tag of the menu item in your menu. So web CPT title = web.

    // setup proper class for custom post type menu items based on the menus title tag
    function current_type_nav_class($classes, $item) {
    	$post_type = get_query_var('post_type');
    	if ($item->attr_title != '' && $item->attr_title == $post_type) {
    		array_push($classes, 'current-menu-item');
    	};
    	return $classes;
    }
    add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2 );

    Then use the css class of current-menu-item to highlight it correctly.

Reply

You must log in to post.

About this Topic