我创建了一个具有3个自定义字段的CPT(自定义字段在ACF插件中完成)。我有一个在我的主题中显示自定义帖子的简码。到目前为止,一切都很好。
我的问题是,当我有多个CPT时,我想拥有一个shortcode属性,让用户指定他们要显示的自定义帖子。
当前,根据我设置的posts_per_page
来决定显示哪个帖子。它完全忽略了属性。
这是我所拥有的:
// create custom post for jumbotron
function jumbotron_post_type()
{
$labels = array(
'name' => 'Jumbotrons',
'singular_name' => 'jumbotron');
$supports = array(
'title'
);
$args=array(
'labels' => $labels,
'supports' => $supports,
'public' => true
);
register_post_type('Jumbotrons', $args);
}
add_action('init', 'jumbotron_post_type');
// create shortcode for display
function show_jumbotron($atts, $content = null )
{
extract(shortcode_atts(array(
'id' => 'new-jumbo',
'per_page' => -1,
), $atts));
$args = array(
'posts_per_page' => $per_page,
'title' => $id,
'post_type' =>'Jumbotrons'
);
$results = new WP_Query($args);
while ($results->have_posts()):
$results->the_post();
ob_start(); ?>
<section class="jumbotron">
<div class="jumbotron--container">
<h1><?php the_field('jumbotron_title'); ?></h1>
<p class="info"><?php the_field('jumbotron_body'); ?></p>
<h2 class="u--text__right"><?php the_field('jumbotron_end_heading'); ?></h2>
</div>
</section>
<?php
$content = ob_get_clean();
endwhile;
wp_reset_postdata();
return $content;
};
add_shortcode('jumbotron', 'show_jumbotron');
在主题中,我有:
<?php echo do_shortcode('[jumbotron id="Homepage Jumbotron"]');?>
如果我将posts_per_page
设置为-1,它将显示最早的自定义帖子。如果我设置为1,那么它将显示最新的。
2个CPT标题是:
我打算让[jumbotron title="Homepage Jumbotron"]
根据输入的标题显示正确的自定义帖子,默认标题为“ New Jumbo”。
答案 0 :(得分:0)
我发现了错误。该ID需要设置为空字符串
'id' => ''
工作代码为:
// create shortcode for display
function show_jumbotron($atts, $content = null )
{
extract(shortcode_atts(array(
'id' => '',
'per_page' => -1,
), $atts));
$args = array(
'post_type' =>'Jumbotrons',
'posts_per_page' => $per_page,
'title' => $id
);
$results = new WP_Query($args);
while ($results->have_posts()):
$results->the_post();
ob_start(); ?>
<section class="jumbotron">
<div class="jumbotron--container">
<h1><?php the_field('jumbotron_title'); ?></h1>
<p class="info"><?php the_field('jumbotron_body'); ?></p>
<h2 class="u--text__right"><?php the_field('jumbotron_end_heading'); ?></h2>
</div>
</section>
<?php
$content = ob_get_clean();
endwhile;
wp_reset_postdata();
return $content;
};
add_shortcode('jumbotron', 'show_jumbotron');
答案 1 :(得分:0)
Hey megler use my below code. I hope it should work.
function show_jumbotron($atts, $content = null )
{
extract(shortcode_atts(array(
'title' => 'New Jumbo',
'per_page' => 1,
), $atts));
$args = array(
'posts_per_page' => $per_page,
'title' => $title,
'post_type' =>'Jumbotrons'
);
$results = new WP_Query($args);
while ($results->have_posts()):
$results->the_post();
ob_start(); ?>
<section class="jumbotron">
<div class="jumbotron--container">
<h1><?php the_field('jumbotron_title'); ?></h1>
<p class="info"><?php the_field('jumbotron_body'); ?></p>
<h2 class="u--text__right"><?php the_field('jumbotron_end_heading'); ?></h2>
</div>
</section>
<?php
$content = ob_get_clean();
endwhile;
wp_reset_postdata();
return $content;
};
add_shortcode('jumbotron', 'show_jumbotron');
Then add shortcode like this:
<?php echo do_shortcode('[jumbotron title="Homepage Jumbotron"]');?>
<?php echo do_shortcode('[jumbotron title="New Jumbo"]');?>
I hope this will work.
Thank you.