我不是程序员,所以我对解决方案一无所知。 我一直在用CMB2 对于投资组合/项目自定义帖子类型。
我合并了一个幻灯片,每张幻灯片都使用组字段元数据。
在主页上有 2 个标签为“空项目”和“测试项目 1”的帖子。 如果您单击“空项目”,您将被定向到它的单个帖子页面,在那里您将看到一个带有红色背景的“.flexslider”div。如果组字段为空,这就是我想删除的 div。我的意思是完成删除 div,不留下空的 div,而不是将背景颜色更改为白色。
如果单击“测试项目 1”,将在“flexslider”幻灯片中使用可重复组字段上传图像。这是元字段的结果,元字段与元数据一起保存在其中。
代谢物// 这是我用来注册可重复字段的代码,它允许我为幻灯片插入图像和标题。
add_action( 'cmb2_admin_init', 'gallery_metabox' );
function gallery_metabox() {
$prefix = 'gallery_';
/**
* Repeatable Field Groups
*/
$cmb_group = new_cmb2_box( array(
'id' => $prefix . 'metabox',
'title' => __( 'Gallery', 'cmb2' ),
'object_types' => array( 'portfolio', ),
) );
// $group_field_id is the field id string, so in this case: $prefix . 'demo'
$group_field_id = $cmb_group->add_field( array(
'id' => $prefix . 'demo',
'type' => 'group',
'options' => array(
'group_title' => __( 'Image {#}', 'cmb2' ), // {#} gets replaced by row number
'add_button' => __( 'Add Another Image', 'cmb2' ),
'remove_button' => __( 'Remove Image', 'cmb2' ),
'sortable' => true, // beta
'closed' => true, // true to have the groups closed by default
),
) );
$cmb_group->add_group_field( $group_field_id, array(
'name' => __( 'Image', 'cmb2' ),
'id' => 'image',
'type' => 'file',
) );
$cmb_group->add_group_field( $group_field_id, array(
'name' => __( 'Image Caption', 'cmb2' ),
'id' => 'image_caption',
'type' => 'text',
) );
}
我关注了this显示这些组字段的元数据。 当我使用这段代码时一切正常:
前端//
<div class="flexslider">
<ul class="slides">
<?php $entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
foreach ( (array) $entries as $key => $entry ) {
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
'class' => 'thumb',
) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
} ?>
</ul>
</div>
但我非常想仅在数据存在时显示 .flexslider 容器 + 元数据。如果字段为空,那么我想显示默认文本或者更好的是删除整个 div 本身。
我尽力进行研究,但似乎无法弄清楚问题出在哪里。
我也试过这段代码:
尝试//
<?php $entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
if(empty ($entry)) { echo ''; }
else {
foreach ( (array) $entries as $key => $entry ) {
echo '<div class="flexslider">';
echo '<ul class="slides">';
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick', null, array(
'class' => 'thumb',
) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop( $entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
echo '</ul>';
echo '</div>';
}
}
?>
上面代码唯一的好处是,当元字段为空时,它肯定会删除 div,但如果元数据确实存在,div 仍然不存在。
编辑// 我尝试在下面的答案中使用“@stweb”代码:
$entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
foreach ( (array) $entries as $key => $entry ) {
if(empty($entry)){
continue;
}
echo '<div class="flexslider">';
echo '<ul class="slides">';
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick',
null, array( 'class' => 'thumb', ) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop(
$entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
echo '</ul>';
echo '</div>';
}
但什么也没有发生...红色的 div 只是坐在那里而不是消失。
我基本上想弄清楚如何仅在图像上传到 Group Field 时才显示第一段代码,如果没有,则什么都不显示,甚至连容器 div 也不显示。
谁能解释一下我哪里出错了?
最佳答案
试试这个:
$entries = get_post_meta( get_the_ID(), 'gallery_demo', true );
foreach ( (array) $entries as $key => $entry ) {
if(empty($entry)){
continue;
}
echo '<div class="flexslider">';
echo '<ul class="slides">';
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick',
null, array( 'class' => 'thumb', ) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop(
$entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
echo '</ul>';
echo '</div>';
}
更新:
foreach ( (array) $entries as $key => $entry ) {
if ( !isset( $entry['image_id'] ) || $entry['image_id'] == '' ) {
continue;
}
echo '<div class="flexslider">';
echo '<ul class="slides">';
$img = $img_url = $caption = '';
if ( isset( $entry['image_id'] ) ) {
$img = wp_get_attachment_image( $entry['image_id'], 'share-pick',
null, array( 'class' => 'thumb', ) );
}
if ( isset( $entry['image_id'] ) ) {
$img_url = wp_get_attachment_image_url( $entry['image_id'], null );
}
$caption = isset( $entry['image_caption'] ) ? wpautop(
$entry['image_caption'] ) : '';
echo '<li data-thumb="'. $img_url .'">';
echo $img;
echo $caption;
echo '</li>';
echo '</ul>';
echo '</div>';
}
关于php - 如果存在组字段元数据+容器div,如果字段为空,如何显示默认文本? [CMB2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38689685/
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问
两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat