javascript - 添加第二个自定义字段

标签 javascript html css wordpress custom-post-type

我有一个自定义字段“ExtraCSS”,它使用以下代码引入自定义帖子 CSS。 (它是从 'have_posts()' 循环中引入的)

enter image description here

html

<?php $extraCSS = get_post_meta(get_the_ID(),'ExtraCSS',true);?><!-- get specific css for post -->

<article>

 <div id="post-<?php the_ID(); ?>" class="img-cell" style="background-image:url('<?php echo $thumbnail_url ?>');" <?php post_class('col-md-12'); ?> >
   <a class="linkage" href="<?php the_permalink(); ?>"</a>
 </div><!-- /#post -->

 <div class="text-cell">
   <div class="<?php echo $extraCSS?>" >
      <h1><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
      <h3><?php the_category(', '); ?></h3>
   </div>
 </div> 

</article>

*编辑 我想再添加 1 个自定义字段(“BG-align”),其值为“BG-align-L”或“BG-align-R”。我想我只是在当前代码下添加另一行类似的代码。

例如

  <?php $extraCSS = get_post_meta(get_the_ID(),'ExtraCSS',true);?>
  <?php $BGalign = get_post_meta(get_the_ID(),'BGalign',true);?>

但是没用

最佳答案

根据*编辑:

'BGalign' 必须在帖子中定义(通过“添加新的自定义字段”),否则它只是空的。

如果未在帖子中设置,您可以设置默认值(编辑“默认值”):

<?php 
$BGalign = get_post_meta(get_the_ID(),'BGalign',true);
$BGalign = ( !empty($BGalign) ? $BGalign : "default-value" );
?>

然后记得回显那个新的 php 变量。例如:

<div class="<?php echo $extraCSS . " " . $BGalign; ?>" >

. 是将变量连接成一个字符串

"" 是空白,确保你的两个类名不会被连接

关于javascript - 添加第二个自定义字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30130757/

相关文章:

javascript - <a> 元素内的链接不起作用

javascript - 为什么这个react/javascript "removeEventListener"不删除监听器?

用于 onclick 函数的 Javascript

html - 边框颜色在焦点上不会改变

css - 有没有办法将 "field_with_errors"类应用于 Rails 中的现有元素?

javascript - typescript 中的类装饰器

javascript - VBA动态网页抓取Excel

javascript - 如何将函数存储到数组中并在 javascript 中循环遍历每个函数

php - 使用 php 和 html 传递数据

javascript - 如何强制下拉菜单在 IE 11 上向下移动?