将页面显示为自定义邮政类型登录页面

时间:2014-02-14 作者:Mike Legacy

自定义帖子类型是可怕的,同时也是一种痛苦。基本上,我需要有一个登录页,包含帖子类型(想想产品类别)的直接子元素列表,但顶部也有一些信息副本。

我最初创建了一个名为“Products”的顶级帖子,并在其下面有层次结构树,但这就是链接http://example.com/products/products 这显然不是我想要的。

我知道我可以创建一个归档页面和一个自定义模板来只显示子元素,但我不知道如何在页面顶部添加额外内容(即信息内容),而不将其硬编码到模板中。

有什么办法吗?

1 个回复
SO网友:Brad Dalton

您可以在任何类型的循环之前在小部件区域中挂接内容。

您可以使用类似钩子的loop\\u start从函数文件执行此操作,并在函数后包含一个条件标记,否则您可以在函数中注册小部件,并将模板标记直接添加到模板中。

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>\',
) );
    }
}
您可能需要更改条件标记。

如果直接从模板文件调用侧栏,则不需要挂钩。

<?php if ( is_post_type_archive() && is_active_sidebar( \'intro-widget\' ) ) : ?>
<ul id="sidebar">
    <?php dynamic_sidebar( \'intro-widget\' ); ?>
</ul>
<?php endif; ?>
这里有一个完整的自定义post类型归档文件,其中包括循环之前的小部件。

<?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();

结束

相关推荐

将页面显示为自定义邮政类型登录页面 - 小码农CODE - 行之有效找到问题解决它

将页面显示为自定义邮政类型登录页面

时间:2014-02-14 作者:Mike Legacy

自定义帖子类型是可怕的,同时也是一种痛苦。基本上,我需要有一个登录页,包含帖子类型(想想产品类别)的直接子元素列表,但顶部也有一些信息副本。

我最初创建了一个名为“Products”的顶级帖子,并在其下面有层次结构树,但这就是链接http://example.com/products/products 这显然不是我想要的。

我知道我可以创建一个归档页面和一个自定义模板来只显示子元素,但我不知道如何在页面顶部添加额外内容(即信息内容),而不将其硬编码到模板中。

有什么办法吗?

1 个回复
SO网友:Brad Dalton

您可以在任何类型的循环之前在小部件区域中挂接内容。

您可以使用类似钩子的loop\\u start从函数文件执行此操作,并在函数后包含一个条件标记,否则您可以在函数中注册小部件,并将模板标记直接添加到模板中。

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>\',
) );
    }
}
您可能需要更改条件标记。

如果直接从模板文件调用侧栏,则不需要挂钩。

<?php if ( is_post_type_archive() && is_active_sidebar( \'intro-widget\' ) ) : ?>
<ul id="sidebar">
    <?php dynamic_sidebar( \'intro-widget\' ); ?>
</ul>
<?php endif; ?>
这里有一个完整的自定义post类型归档文件,其中包括循环之前的小部件。

<?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();

相关推荐