[英]Targeting specific Wordpress posts in a loop
I am trying to target individual posts so I can change the css (title tags, padding, etc) of specific posts. 我正在尝试定位单个帖子,以便可以更改特定帖子的CSS(标题标签,填充等)。 My Wordpress site currently generates the posts in a loop. 我的Wordpress网站当前以循环方式生成帖子。
index.php code (brings in content.php which has 'post' code) index.php代码(在具有“ post”代码的content.php中带来)
<div>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content' );
?>
<?php endwhile; ?>
<div class="clearfix"></div>
<div class="col-md-12">
</div>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
</div>
content.php code (gets post-title, category, and sets post-thumbnail to background-image) content.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="row">
<div class="col-md-12">
<h2><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<?php the_category(', '); ?>
</div>
</div>
</article><!-- /#post -->
functions.php (setting image size) functions.php(设置图像大小)
add_theme_support( 'post-thumbnails' );
add_image_size('my-fun-size', 'thumbnail');
The output is 'rows' 100% width with the title, category and background-image (feature-image). 输出为带有标题,类别和背景图像(特征图像)的“行”宽度为100%。 Stacked on top of each other. 彼此堆叠。 I want to be able to target the text and bg-image of different posts to make them each look different. 我希望能够定位不同帖子的文本和背景图像,以使它们各自看起来不同。
i think the best way to to this is by adding a custom field inside your posts, then, in your templates, you call that custom field this way: 我认为最好的方法是在帖子中添加一个自定义字段 ,然后在模板中以这种方式调用该自定义字段:
get_post_meta($post->ID, 'name_of_your_custom_field', true);
this must be inside the loop. 这必须在循环内。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.