我使用此代码在中的主页上显示来自其他页面的帖子front-page.php
模板:
<?php
$args = array(
\'post_type\' => array(\'project\')
);
$the_query = new WP_Query( $args );
?>
<div class="container">
<div class="row">
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-md-4">
<a href="<?php the_permalink(); ?>" class="project-item">
<?php
$overview_image = get_field(\'small_overview_image\');
?>
<div class="project-item_img">
<img class="img-fluid" src="<?php echo $overview_image[\'url\']; ?>" alt="<?php the_title(); ?>" />
</div>
<div class="project-item_title">
<div class="project-item_title-name"><?php the_title(); ?></div>
<?php
global $post;
$post = get_post($post_id);
$projects_page = get_the_ID();
var_dump($projects_page);
$args = array(
\'post_type\' => \'post\',
\'post_parent\' => get_the_ID(),
\'ignore_sticky_posts\' => true
);
$post = new WP_Query($args);
$child_subtitle = get_the_subtitle($post);
var_dump($child_subtitle);
?>
<div class="project-item_title-description"><?php echo $child_subtitle; ?></div>
</div>
</a>
</div>
<?php endwhile; endif; ?>
</div>
</div>
但这并不是一个好的解决方案,因为我使用了ACF插件转发器部分,并在每个部分中复制了帖子。
只需在一个部分中添加显示帖子的代码,需要使用快捷码,并需要设置帖子限制-3项。如何做到这一点?
最合适的回答,由SO网友:siberian 整理而成
我在函数中创建了短代码。php:
add_shortcode(\'projects\', \'projects_shortcode\');
function projects_shortcode($atts){
extract(shortcode_atts(array(
\'post_type\' => \'project\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 3,
//\'caller_get_posts\' => 1
), $atts));
$args = array(
\'post_type\' => $post_type,
\'post_status\' => $post_status,
\'posts_per_page\' => $posts_per_page
);
global $post;
$posts = new WP_Query($args);
$out = \'\';
if ($posts->have_posts())
while ($posts->have_posts()):
$posts->the_post();
$overview_image = get_field(\'small_overview_image\');
$out .= \'<div class="col-md-4">
<a href="\'.get_permalink().\'" title="\' . get_the_title() . \'" class="project-item">
<div class="project-item_img"><img class="img-fluid" src="\'.$overview_image[\'url\'].\'" /></div>
<div class="project-item_title">
<div class="project-item_title-name">\'.get_the_title().\'</div>
<div class="project-item_title-description"></div></div>\';
$out .=\'</a></div>\';
endwhile;
else
return; // no posts found
wp_reset_query();
return html_entity_decode($out);
}
在wp编辑器中,我使用短代码
[projects]