我的 Wordpress 站点有一个创建帖子的循环,我想针对特定的帖子来更改它们的 css 值。
html - index.php
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content' );
?>
<?php endwhile; ?>
内容.php
<?php
if (has_post_thumbnail()) {
$thumbnail_data = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'my-fun-size' );
$thumbnail_url = $thumbnail_data[0];
}
?>
<article id="post-<?php the_ID(); ?>" style="background-image:url('<?php echo $thumbnail_url ?>')" <?php post_class('container-fluid'); ?> >
<div class="container-fluid">
<div class="col-md-12 text-cell">
<h2><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php the_category(', '); ?>
</div>
</div>
</article><!-- /#post -->
有人建议我使用“get_post_meta ...”,但我不熟悉如何使用它。我只想更改不同帖子的 css 值(填充、字体大小等)
最佳答案
我建议为此使用自定义字段,这样您就可以在帖子本身中定义值,而不是每次添加新帖子时都必须编辑代码。
在帖子中,确保自定义字段可见(可从顶部的屏幕选项切换)
然后创建一个名为“Alignment”或任何您喜欢的字段并为其分配一个值。 (例如“左”)
然后你可以在循环中添加一个条件。
<?php $alignment = get_post_meta(get_the_ID(),'Alignment',true);
if($alignment) == 'left'):?>
<p>Do stuff and things here...</p>
<?php endif;?>
您可以在这里阅读更多相关信息:https://codex.wordpress.org/Custom_Fields
希望这对您有用。如果您想更喜欢它,我建议您查看 Advanced Custom Fields 插件,它提供更多的灵 active 和选项。
根据评论编辑:
第一个选项: 将“ExtraCSS”的字段设置为“color:green;”
<?php $extraCSS = get_post_meta(get_the_ID(),'ExtraCSS',true);?>
<article id="post-<?php the_ID(); ?>" style="background-image:url('<?php echo $thumbnail_url ?>'); <?php echo $extraCSS;?>" <?php post_class('container-fluid'); ?> >
第二个选项:
(在你的样式表中:)
article:nth-child(2n+0)
{
color:green;
}
关于php - 想要循环定位特定帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30083127/