在模板中包含特定页面

时间:2011-08-16 作者:randyttp

如何将特定页面包含到模板中?

例如,在我的网站主页上,我希望有大约3个不同的内容区域,客户可以根据需要定期更新。我想最好的方法可能是将页面嵌套到模板中,他可以编辑页面。

我如何设置?wordpress codex、wordpress论坛和google都没有给我任何解决方案。

我知道有一个插件允许这样做,但它与3.1及以上版本不兼容。无论如何,我不喜欢使用插件的想法,我想wordpress应该有一个简单的模板标签系统设置,错了。。。。PHP可能就是答案所在。

谢谢你的帮助

p、 另外,如果你知道除了使用嵌套页面之外还有更好的方法,请告诉我。我只是一个wordpress noob,我想这将是一个完成任务的简单方法。

2 个回复
最合适的回答,由SO网友:Drew Gourley 整理而成

另一个完成类似任务的好方法是利用WordPressCustom Post Types.

您可以在WordPress后端设置3种不同类型的功能,以便于使用。

首先,设置自定义帖子类型要做到这一点,请打开主题的功能。php文件,并包括以下内容:

<?php
function custom_register_post_types() {
    $labels = array(
        \'name\' => _x(\'People\', \'post type general name\'),
        \'singular_name\' => _x(\'Person\', \'post type singular name\'),
        \'add_new\' => __(\'Add New\'),
        \'add_new_item\' => __(\'Add New Person\'),
        \'edit_item\' => __(\'Edit Person\'),
        \'new_item\' => __(\'New Person\'),
        \'view_item\' => __(\'View Person\'),
        \'search_items\' => __(\'Search People\'),
        \'not_found\' => __(\'No People found\'),
        \'not_found_in_trash\' => __(\'No People found in Trash\'),
        \'menu_name\' => \'People\'
    );
    $rewrite = array(
        \'slug\' => \'people\'
    );
    $supports = array(\'title\',\'editor\');
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'show_in_menu\' => true,
        \'query_var\' => \'people\',
        \'rewrite\' => $rewrite,
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'supports\' => $supports
    );
    register_post_type(\'people\',$args);
    $labels = array(
        \'name\' => _x(\'Places\', \'post type general name\'),
        \'singular_name\' => _x(\'Place\', \'post type singular name\'),
        \'add_new\' => __(\'Add New\'),
        \'add_new_item\' => __(\'Add New Place\'),
        \'edit_item\' => __(\'Edit Place\'),
        \'new_item\' => __(\'New Place\'),
        \'view_item\' => __(\'View Place\'),
        \'search_items\' => __(\'Search Places\'),
        \'not_found\' => __(\'No Places found\'),
        \'not_found_in_trash\' => __(\'No Places found in Trash\'),
        \'menu_name\' => \'Places\'
    );
    $rewrite = array(
        \'slug\' => \'places\'
    );
    $supports = array(\'title\',\'editor\');
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'show_in_menu\' => true,
        \'query_var\' => \'places\',
        \'rewrite\' => $rewrite,
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'supports\' => $supports
    );
    register_post_type(\'places\',$args);
    $labels = array(
        \'name\' => _x(\'Things\', \'post type general name\'),
        \'singular_name\' => _x(\'Thing\', \'post type singular name\'),
        \'add_new\' => __(\'Add New\'),
        \'add_new_item\' => __(\'Add New Thing\'),
        \'edit_item\' => __(\'Edit Thing\'),
        \'new_item\' => __(\'New Thing\'),
        \'view_item\' => __(\'View Thing\'),
        \'search_items\' => __(\'Search Things\'),
        \'not_found\' => __(\'No Things found\'),
        \'not_found_in_trash\' => __(\'No Things found in Trash\'),
        \'menu_name\' => \'Things\'
    );
    $rewrite = array(
        \'slug\' => \'things\'
    );
    $supports = array(\'title\',\'editor\');
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'show_in_menu\' => true,
        \'query_var\' => \'things\',
        \'rewrite\' => $rewrite,
        \'has_archive\' => true,
        \'hierarchical\' => false,
        \'supports\' => $supports
    );
    register_post_type(\'things\',$args);
}
add_action(\'init\', \'custom_register_post_types\', 0);
?>
我已经写了一篇关于这段代码细节的参考文献here.您也可以在codex.

这将要做的是在WordPress的主左侧导航中设置3个新菜单,为这些类型的每种类型在界面中提供自己的空间。在这种情况下,人、地方和事物。(从逻辑上进行自定义以满足您的需要)现在,这些设置只显示标题栏和编辑器窗格。

在你的主页上查询要使用的帖子类型,我喜欢设置新的查询对象,以保持所有内容都很好和独立。此外,您还可以在其他循环中调用对象,这有时很方便。将这些(或定制的适合您需要的东西)混合到您的模板中,以将它们带到所需的页面中。

<?php
$args = array( \'post_type\' => \'people\', \'numberposts\' => \'1\' );
$people = new WP_Query($args);
if ( $people->have_posts() ) : 
    while ( $people->have_posts() ) : 
        $people->the_post(); 
        global $more;
        $more = 0;
?>

<div id="people-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post-entry">
        <div class="post-title">
            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
        </div>
        <div class="post-content">
            <?php edit_post_link( \'Edit\', \'<h5 class="edit-link">\', \'</h5>\' ); ?>
            <?php the_content(); ?>
        </div>
    </div>
</div>

<?php
    endwhile;
endif;
wp_reset_query();
?>

<?php
$args = array( \'post_type\' => \'places\', \'numberposts\' => \'1\' );
$places = new WP_Query($args);
if ( $places->have_posts() ) : 
    while ( $places->have_posts() ) : 
        $places->the_post(); 
        global $more;
        $more = 0;
?>

<div id="places-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post-entry">
        <div class="post-title">
            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
        </div>
        <div class="post-content">
            <?php edit_post_link( \'Edit\', \'<h5 class="edit-link">\', \'</h5>\' ); ?>
            <?php the_content(); ?>
        </div>
    </div>
</div>

<?php
    endwhile;
endif;
wp_reset_query();
?>

<?php
$args = array( \'post_type\' => \'things\', \'numberposts\' => \'1\' );
$things = new WP_Query($args);
if ( $things->have_posts() ) : 
    while ( $things->have_posts() ) : 
        $things->the_post(); 
        global $more;
        $more = 0;
?>

<div id="things-<?php the_ID(); ?>" <?php post_class(); ?>>
    <div class="post-entry">
        <div class="post-title">
            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
        </div>
        <div class="post-content">
            <?php edit_post_link( \'Edit\', \'<h5 class="edit-link">\', \'</h5>\' ); ?>
            <?php the_content(); ?>
        </div>
    </div>
</div>

<?php
    endwhile;
endif;
wp_reset_query();
?>
这些只是一些快速而肮脏的示例,说明了如何设置查询和循环。这些将只设置并显示每个自定义帖子类型中的最新帖子或热门帖子。

有关WP\\U查询类的更多说明,请参见here. 其他一切都是HTML和CSS。

享受

SO网友:Rarst

将其视为包含页面有点不合适。可以将其视为检索和包含页面内容。

$page = get_page_by_title(\'About The Tests\');
$content = apply_filters(\'the_content\', $page->post_content);
echo $content;
另一种方法是为此设置特殊的侧栏,让客户端通过小部件添加/删除/内容。但这样做很容易被滥用。

结束