我创建了一个具有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 displayfunction 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标题是:
- 新巨无霸
- 巨无霸首页
#1我发现了错误。 ID需要设置为空字符串
'id' =>
''
【WP以CPT标题为属性的短代码】工作代码是:
// create shortcode for displayfunction 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');
#2
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.
推荐阅读
- 我是否应该在自定义WordPress主题中使用div id =”page” class =”site”()
- 智慧军营政治工作模块介绍
- Office365混合部署实战系列教程二(制定混合部署的迁移方案)
- 大厂面试官(JDK 线程池中如何不超最大线程数快速消费任务())
- 递归问题闲究——汉诺塔问题
- 用vue+eslint+vscode实现代码规范化
- 看动画学算法之:栈stack
- 网络基础之重TCP/UDP三次握手和四次挥手
- 首发Citrix/view虚拟桌面禁用普通用户更改或删除网卡权限