Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Custom Post Type Active Page Problem WordPress

  • SOLVED

<strong>My situation:</strong>
- I've got 2 pages; "A" is a page w/ parent page (subnav in the sidebar); "B" is a page w/ custom posts displayed.
- "A" 's subnav is a menu generated in the back end w/ a few custom links. The page and subnav recognize what page (parent or child) the user is looking at.
- "B" is actually a child page (w/ subnav). It's parent automatically redirects (via a [[LINK href="http://wordpress.org/extend/plugins/speedy-page-redirect/"]]plugin[[/LINK]]) to "B".

<strong>My Problem:</strong>
My subnav on "B" is not recognizing that it's the child page. WordPress is not generating the child/parent CSS, nor is it generating the correct <em>title</em> in the header.
I get the feeling that it's my custom post type.
I don't think it's the plugin, only because the plugin's php doesn't seem to effect the resulting forward, nor should the plugin be effecting WordPress's ability to recognize it's own pages.
Also, EVERY PAGE on the site is using the same <em>header.php</em>.

If code is needed, I'd be more than happy to give it to you... same goes for a URL.

Thanks,
Eric

Answers (1)

2011-06-05

Ivaylo Draganov answers:

Hello,

this might be an easy fix but you have to provide some code(URLs are also welcome). How does page B load the custom post types? Is it via a page template?

And what's the purpose of having a custom post type inside a page in the first place?


Eric Munoz comments:

Yes, page template.
Here's code:

<?php
/*
Template Name: Web Portfolio
*/
?>
<?php get_header(); ?>
<div class="wrap">
<div id="equal-columns-wrap">
<!-- ||||SIDEBAR|||| -->
<div id="sidebar">
<div id="sidebar-border">
<?php include (TEMPLATEPATH . '/portfolio-sidebar.php'); ?>
</div>
</div>
<!-- ||||SIDEBAR|||| -->
<!-- ||||CONTENT|||| -->
<div id="content">
<h2 id="web-header">Web &amp; Interactive</h2>
<h3 id="web-h3" class="darkblue">A Few Samples of Our Work</h3>
<!-- begin portfolio -->
<div id="portfolio">
<?php //$loop = new WP_Query( array( 'post_type' => 'web', 'posts_per_page' => 6 ) ); ?>
<?php //while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php query_posts('post_type=web&posts_per_page=6&paged='.$paged); if (have_posts()) : ?>
<?php while (have_posts()) : 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(); ?>
<?php //wp_reset_postdata(); // reset the query ?>
<?php endif; ?>
<div class="clear"></div>
</div><!-- end portfolio -->
</div><!-- ||||CONTENT|||| -->
</div><!-- closed equal-columns-wrap div -->
</div><!-- end the content wrap -->
<div class="clear"></div>
<?php get_footer(); ?>


A little sloppy, I know, and that may be the problem... trying to do this quickly... but take your time. The main purpose of having the CPT on a page is for permalink structure.


Eric Munoz comments:

Also, just for the heck of it... here's the sidebar:

<?php
/*
Template Name: Portfolio Sidebar
*/
?>
<?php wp_nav_menu( array('menu' => 'Portfolio Sidebar Navigation' )); ?>

<h3 class="side-h3"><a href="<?php echo get_option('home'); ?>/portfolio/web/">Web Development</a></h3>
<?php $loop = new WP_Query( array( 'post_type' => 'web', 'posts_per_page' => 1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="side-thumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="Portfolio item: <?php the_title_attribute(); ?>"><?php the_post_thumbnail('about-sidebar-thumb'); ?></a></div>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>

<h3 class="side-h3"><a href="<?php echo get_option('home'); ?>/portfolio/print/">Graphic Design</a></h3>
<?php $loop = new WP_Query( array( 'post_type' => 'print', 'posts_per_page' => 1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="side-thumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="Portfolio item: <?php the_title_attribute(); ?>"><?php the_post_thumbnail('about-sidebar-thumb'); ?></a></div>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>

<h3 class="side-h3"><a href="<?php echo get_option('home'); ?>/portfolio/brand/">Branding &amp; Identity</a></h3>
<?php $loop = new WP_Query( array( 'post_type' => 'brand', 'posts_per_page' => 1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="side-thumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="Portfolio item: <?php the_title_attribute(); ?>"><?php the_post_thumbnail('about-sidebar-thumb'); ?></a></div>
<?php endwhile; ?>
<?php wp_reset_postdata(); // reset the query ?>

<?php if (!function_exists('dynamic_sidebar') ||
!dynamic_sidebar('Portfolio Sidebar')) :
?><?php endif; ?>


Again, the nav is the same for "A", built in the "Menu" admin panel.


Ivaylo Draganov comments:

Try adding this after <em><?php endif; ?></em> in the page template:


<?php wp_reset_query(); // reset the query back to the original one for this page so that WP recognizes it ?>


My bet is that this is the culprit. The queries in the sidebar are new query objects, so they don't affect the main query.


Eric Munoz comments:

Gah!! I was really hoping it would work...
No go. :(

I tried it two ways...
1.<?php endwhile; ?><?php wp_pagenavi(); ?>
<?php wp_reset_query(); // reset the query back to the original one for this page so that WP recognizes it ?>
<?php endif; ?><?php //wp_reset_postdata(); // reset the query ?>
<div class="clear"></div>
</div><!-- end portfolio -->

2.<?php endwhile; ?><?php wp_pagenavi(); ?>

<?php endif; ?><?php //wp_reset_postdata(); // reset the query ?>
<div class="clear"></div>
</div><!-- end portfolio -->


Other thoughts?


Eric Munoz comments:

I realized that I have another page ("C") that is working just fine... problem is that there's no side nav, but that doesn't sound like it's the problem. There is no child to this page, but it does have CPTs on it.

I will say that the <em>title</em> DOES give the correct name for the page and each of its custom posts (<em>single.php</em>, too).

Here's that code...
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<div class="wrap">
<div id="equal-columns-wrap">
<!-- ||||SIDEBAR|||| -->
<div id="sidebar">
<div id="sidebar-border">
<?php include (TEMPLATEPATH . '/sidebar.php'); ?>
</div>
</div>
<!-- ||||SIDEBAR|||| -->
<!-- ||||CONTENT|||| -->
<div id="content">
<h2 id="news-h2">Capiche News</h2>
<?php $loop = new WP_Query( array( 'post_type' => 'news', 'posts_per_page' => 5 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="news-post">
<div class="news-thumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail('news-thumb'); ?></a></div>
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<div class="news-date">- <?php the_time('m/j/Y') ?></div><div class="news-category"><?php echo get_the_term_list( $post->ID, 'categories', 'Categories - ', ', ','' ); ?></div>
<div class="news-excerpt"><p><?php echo limit_words(get_the_excerpt(), '50'); ?></p></div>
<div class="read-more"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">read more</a></div>
<div class="clear"></div>
</div>
<?php endwhile; ?>
<div id="pagination"><?php wp_pagenavi(); ?></div>
<?php wp_reset_postdata(); // reset the query ?>
<div class="clear"></div>
</div><!-- ||||CONTENT|||| -->
</div><!-- closed equal-columns-wrap div -->
</div><!-- end the content wrap -->
<div class="clear"></div>
<?php get_footer(); ?>


Ivaylo Draganov comments:

So why not use the same code then? The code for page "C" is using a new query object and so it does not mess with the original query. But for <em>wp_pagenavi</em> to work I think you ought to pass it the correct query to paginate:

<?php wp_pagenavi( array( 'query' => $loop ) ); ?>


I see you've probably tried using similar code (as it's commented out right now). Was there a problem? Try using this code for page "B":
[[LINK href="http://pastebin.com/wg7f9gH4"]]http://pastebin.com/wg7f9gH4[[/LINK]]


Eric Munoz comments:

1. So you think I should use "C" for "B"?
2. I copied and pasted your code and got this error:
Parse error: syntax error, unexpected T_ENDIF in ...../wp-content/themes/thetheme/web.php on line 61
3. Yes I had a problem w/ pagination. Apparently (if you know more than me, fill me in, please), WP is super compatible w/ CPT pagination. So I used a [[LINK href="http://ratvars.com/custom-post-type-archives"]]plugin[[/LINK]] (sorry, I should've disclosed that sooner) to get the wp-pagenavi to work.
4. If you think "C" will work for "B", I'm up for trying it, but if I remember correctly I had problems w/ that... but I can't remember why. :S Sorry.

Thoughts?

PS... thanks a ton for all your help, thus far.


Ivaylo Draganov comments:

Excuse me, there was a syntax error. Try the fixed code:
[[LINK href="http://pastebin.com/wg7f9gH4"]]http://pastebin.com/wg7f9gH4[[/LINK]]

<em>wp_pagenavi</em> by default paginates the main WP query (normal archive pages). If you want to use it with a separate loop(the "new WP_Query" thing) then you need to pass the query object to the function.


Eric Munoz comments:

No go. :(
Should I just switch out "B" for "C"?


Ivaylo Draganov comments:

I need to know what's going on in order to help you :)
I'd like to take a look if you don't mind granting me access to your WP administration.

my email is dragunoff at gmail dot com (or you can use WP Question's messaging system)


Eric Munoz comments:

OK... I set up as a user.
Please post changes on here.


Ivaylo Draganov comments:

I've found the culprit. The page's slug is the same as the slug for the custom post type archive and that's causing a conflict inside WP. I advise you not to use a page template for listing your custom post types. Use the built-in CPT archives templates.

To do this create a file named <em>archive-{post_type}.php</em>(replacing {post-type} with the post type's registration name) for each of your CPT's. You can paste the code from your <em>archive.php</em> in the new files as a start. Then, ditch the old pages and visit the same url again to see whether the built-in CPT archives work.

Let me know how it went.


Eric Munoz comments:

OK

I changed <em>web.php</em> to <em>archive-web.php</em>

1. I'm not sure if you what you're referring to when you say:
<em><strong>....replacing {post-type} with the post type's registration name...</strong></em>
Do I change my CPT registration in the <em>functions.php</em>?
Do I ditch my archive plugin?
Do I delete the pages?
Do I not use any page templates for the CPTs?

Sorry... just changing the name of the files and didn't work.


Eric Munoz comments:

GOT IT!!!!

1. I changed web.php to archive-web.php.
2. I reset the page template to the same exact one.
3. I ditched the plugin.
4. I didn't change a thing to my loop.


Ivaylo Draganov comments:

> I changed web.php to archive-web.php

Yes, that's correct. Now you should paste the contents of <em>index.php</em> to that new file as a start and modify from there on. No need for any loop magic :)

> Do I change my CPT registration in the functions.php

The rewrite slug seems a little odd (using a forward slash), but keep it like that and see how it goes. (compare it with your 'news' post type)

> Do I ditch my archive plugin?

Yes. Since WP 3.1 it is no longer necessary (check the message at the top of [[LINK href="http://wordpress.org/extend/plugins/custom-post-type-archives/"]]the plugin's page[[/LINK]])

> Do I delete the pages?

Yes. Or at least change their slugs to something else, so they don't conflict with the CPT's slug.

> Do I not use any page templates for the CPTs?

You shouldn't use page templates for that. WP now supports CPT's without any plugins or hacks.


Eric Munoz comments:

I'll do my best to implement what you've said.
I know the less "ghetto rigging", the better.


Ivaylo Draganov comments:

Yes, I see you are not familiar with the intricate workings of WP. As I look at your site, I think of other possible ways to build it. This approach is also fine, but you should work towards less redundancy in the code you use. Think of more generic templates with some clever conditionals. Keep going, the site looks good :-)


Eric Munoz comments:

OK
I deleted the page, and everything went away. I restored it, and it went back to working.

Also, I still have to use the page templates.

How do I fix both of these?

The reason for the odd slug, is because they want to be able to click on "Portfolio" and have it redirect to web. I used the pages to create some order because I'm not up to speed on WP 3.1 and CPTs.

Thanks a ton!!

PS. My goal this week is to go through each page and clean up the code. :)


Eric Munoz comments:

OK I FINALLY GOT IT!!!
This post helped me: [[LINK href="http://wordpress.org/support/topic/custom-post-type-category-archives-not-working"]]http://wordpress.org/support/topic/custom-post-type-category-archives-not-working[[/LINK]]

I forgot to resave my permalinks!!!

Everything is working in a WP 3.1 fashion!


Eric Munoz comments:

Hey Ivalyo,

I've got a pagination problem. The pagination plugin works, but I'm getting the 404.

Thoughts?


Eric Munoz comments:

The pagination looks like it may be answered here... [[LINK href="http://wordpress.org/support/topic/another-wp-31-cpt-archive-pagination-problem"]]http://wordpress.org/support/topic/another-wp-31-cpt-archive-pagination-problem[[/LINK]]