Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Custom Post-Types are awesome and a PAIN at the same time. Basically, I need to have a landing page, with the list of the direct child elements of the post type (think product categories), but also some information copy at the top as well.

I initially created a top-level post called "Products" and have the hierarchy tree underneath that, but that was making the link http://example.com/products/products which is obviously not what I want.

I know I can create an archive page and a custom template to display JUST the child elements, but I'm not sure how add the extra content (I.E. the informational content) at the top of the page without hard-coding it into the template.

Any way to do this?

share|improve this question

1 Answer 1

You can hook in a widget area for content before any type of loop.

You can do this from your functions file using a hook like loop_start and include a conditional tag after the function otherwise you can register the widget in functions and add the template tag directly to a template.

register_sidebar(array(
'name'          => __( 'Intro Widget', 'theme_text_domain' ),
'id'            => 'intro-widget',
'before_widget' => '<div class=intro-widget">',
'after_widget'  => '</div>',
));


add_action( 'loop_start', 'intro_widget' );
function intro_widget() {
if ( is_post_type_archive() && is_active_sidebar( 'intro-widget' ) ) { 
dynamic_sidebar('intro-widget', array(
'before' => '<div class="intro-widget">',
'after' => '</div>',
) );
    }
}

You might need to change the conditional tag.

If you call the sidebar directly from your template file, you won't need the hook.

<?php if ( is_post_type_archive() && is_active_sidebar( 'intro-widget' ) ) : ?>
<ul id="sidebar">
    <?php dynamic_sidebar( 'intro-widget' ); ?>
</ul>
<?php endif; ?>

Here's a complete custom post type archive file which includes the widget before the loop.

<?php
/**
* Adds the Full Width Custom Post Type Archive Page
*/

get_header(); ?>

<section id="primary" class="content-area">
    <div id="content" class="site-content" role="main">
    <?php if ( is_post_type_archive() && is_active_sidebar( 'intro-widget' ) ) : ?>
   <ul id="sidebar">
<?php dynamic_sidebar( 'intro-widget' ); ?>
</ul>
<?php endif; ?>
    <?php if ( have_posts() ) : ?>

        <header class="archive-header">
            <h1 class="archive-title"><?php printf( __( 'Portfolio Archives %s', 'wpsites' ), single_cat_title( '', false ) ); ?></h1>

            <?php
                // Show an optional term description.
                $term_description = term_description();
                if ( ! empty( $term_description ) ) :
                    printf( '<div class="taxonomy-description">%s</div>', $term_description );
                endif;
            ?>
        </header><!-- .archive-header -->

        <?php
                // Start the Loop.
                while ( have_posts() ) : the_post();


                get_template_part( 'content', get_post_format() );

                endwhile;

                wpsites_page_nav();

            else :
                // If no content, include the "No posts found" template.
                get_template_part( 'content', 'none' );

            endif;
        ?>
    </div><!-- #content -->
</section><!-- #primary -->

<?php
get_footer();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.