Php 如何使用WordPress在主页中显示帖子?
我是WordPress主题开发的新手,我对用于在主页上显示我的帖子的WP功能有些怀疑:Php 如何使用WordPress在主页中显示帖子?,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我是WordPress主题开发的新手,我对用于在主页上显示我的帖子的WP功能有些怀疑: <?php if ( have_posts() ) : // Start the Loop. while ( have_posts() ) : the_post(); /* * Include the post format-specific template fo
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next post navigation.
twentyfourteen_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
在阅读文档时,我觉得:
1) 获取模板零件:将模板零件加载到模板中。所以我认为,通过这一行,我包括了一个用于显示帖子的模板部分(在我的主页中显示帖子的结构),对吗
2) 获取帖子格式()的具体方法是什么?获取帖子格式()
是确定帖子是否有任何帖子格式
它返回当前post格式类型的字符串值,这在多个方面都很有用。最强大的功能之一是根据post格式调用不同的模板零件文件,例如:
get\u template\u part('entry',get\u post\u format())
其中包括,例如,entry-aside.php
表示备用格式,或entry.php
表示标准格式
来源:获取模板部分('content',获取发布格式())代码>
是的,在主题目录中应该有一个模板文件content.php,用于通过get\u template\u part()
get\u post\u format()
返回帖子的帖子格式。这通常会在循环中调用,但如果提供了post ID,则可以在任何地方使用。
get\u template\u part()
确实会要求包含模板,这是PHP的include()
的更安全版本,因为如果找不到模板,它将自动失败
它遵循Wordpress的slug
模板文件名称空间
{slug}-{name}.php
您已将内容
定义为您的slug
,因此它将查找:
content-
{name}
.php
其中,作为第二个函数,将调用第二部分。post格式由post本身定义,具体取决于应用于post本身的元数据,即PostID 221是一个“引号”
这对于页面的“部分”来说非常有用,因为您的文章可能包含上传的视频或来自外部作者的引用,因此会引入一种外部格式,以便在整个站点中使用,因为get\u post\u format()
docs描述了可能的post\u format()的
列表:
- 侧面
- 聊天
- 画廊
- 链接
- 录像带
- 等
如果您实际上只是想获得帖子内容的直接模板部分,请调用:
the_content();
将查找名为single.php
的模板文件,但如果您想编写自己的定制content-*
模板文件:
//Calls content-customtemplate.php
get_template_part( 'content', 'customtemplate' );
如果要显示特定类别的帖子,则可以使用此代码
<?php
$ID = '6'; $NUMBEROFPOSTS = '5'; $catposts = get_posts('category='.$ID."&order=DESC&numberposts=".$NUMBEROFPOSTS);
$cnt2 = 0;
foreach($catposts as $item) :
$cnt2++;
$headlines .= '<div class="box1">';
$headlines .= '<div class="num">'.$cnt2.'</div>';
$headlines .= '<p><a href="'.get_permalink( $item->ID ).'">'.$item->post_title.'</a></p>';
$headlines .= '</div>';
endforeach;
echo $headlines;
?>
其中,$ID是一个类别ID,$NUMBEROFPOSTS是您希望显示主页特定类别中的帖子数量
在最后一个echo$variable name上,它将显示类别文章中的所有内容
//Calls content-customtemplate.php
get_template_part( 'content', 'customtemplate' );
<?php
$ID = '6'; $NUMBEROFPOSTS = '5'; $catposts = get_posts('category='.$ID."&order=DESC&numberposts=".$NUMBEROFPOSTS);
$cnt2 = 0;
foreach($catposts as $item) :
$cnt2++;
$headlines .= '<div class="box1">';
$headlines .= '<div class="num">'.$cnt2.'</div>';
$headlines .= '<p><a href="'.get_permalink( $item->ID ).'">'.$item->post_title.'</a></p>';
$headlines .= '</div>';
endforeach;
echo $headlines;
?>