Wordpress:在索引页上列出多个 post 类型
Wordpress: List multiple post types on Index Page
我正在使用 twothousand-sevente-theme 的子主题,因为我想更改一些内容。
但是我在实现这个想法时遇到了问题:
我创建了一个名为 'video' 的自定义 post 类型,但在索引页 (index.php) 上我想显示两种类型的 post,默认 post 类型和自定义 'video' post 类型,按发布日期排序。
因为我真的很喜欢 index.php 以最新的 post 为特色的方式,所以我真的不想创建自定义存档(我想我必须保留 get_template_part( ) 的功能)。我想做的就是在索引页上显示两种 post 类型。
<?php if ( have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
</header><!-- .page-header -->
<?php endif; ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) : ?>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/post/content', get_post_format() );
endwhile;
the_posts_pagination( array(
'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
) );
else :
get_template_part( 'template-parts/post/content', 'none' );
endif; ?>
我尝试了很多方法,但 none 对我的想法有用...
必须有一种方法可以 select wp-query 中的 post 类型并在索引页面上显示它们,按发布日期和默认内容模板排序。
喜欢 select 所有 post 的 post 和视频类型)用于 get_post_format() 函数(但我不是很喜欢 PHP)
我很感激任何想法。
此致,
马尔特
你可以这样做,在你的 index.php
将下面的代码放在要显示所有 post 标题和内容的位置。
<?php
$args = array(
'post_type' => array( 'post', 'video' ),
'orderby' => 'date',
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="smal-sec">
<h3><?php the_title();?></h3>
<?php the_content();?>
</div>
<?php
endwhile;
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
?>
从这个循环中我们将能够获得所有 post 的默认 post 和 "video" 自定义 post 类型。帖子将根据日期显示。
我正在使用 twothousand-sevente-theme 的子主题,因为我想更改一些内容。 但是我在实现这个想法时遇到了问题:
我创建了一个名为 'video' 的自定义 post 类型,但在索引页 (index.php) 上我想显示两种类型的 post,默认 post 类型和自定义 'video' post 类型,按发布日期排序。
因为我真的很喜欢 index.php 以最新的 post 为特色的方式,所以我真的不想创建自定义存档(我想我必须保留 get_template_part( ) 的功能)。我想做的就是在索引页上显示两种 post 类型。
<?php if ( have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
</header><!-- .page-header -->
<?php endif; ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) : ?>
<?php
/* Start the Loop */
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/post/content', get_post_format() );
endwhile;
the_posts_pagination( array(
'prev_text' => twentyseventeen_get_svg( array( 'icon' => 'arrow-left' ) ) . '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>' . twentyseventeen_get_svg( array( 'icon' => 'arrow-right' ) ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
) );
else :
get_template_part( 'template-parts/post/content', 'none' );
endif; ?>
我尝试了很多方法,但 none 对我的想法有用... 必须有一种方法可以 select wp-query 中的 post 类型并在索引页面上显示它们,按发布日期和默认内容模板排序。 喜欢 select 所有 post 的 post 和视频类型)用于 get_post_format() 函数(但我不是很喜欢 PHP)
我很感激任何想法。
此致, 马尔特
你可以这样做,在你的 index.php
将下面的代码放在要显示所有 post 标题和内容的位置。
<?php
$args = array(
'post_type' => array( 'post', 'video' ),
'orderby' => 'date',
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) :
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<div class="smal-sec">
<h3><?php the_title();?></h3>
<?php the_content();?>
</div>
<?php
endwhile;
endif;
wp_reset_query(); // Restore global post data stomped by the_post().
?>
从这个循环中我们将能够获得所有 post 的默认 post 和 "video" 自定义 post 类型。帖子将根据日期显示。