在循环中定位特定的Wordpress帖子(Targeting specific Wordpress posts in a loop)

编程入门 行业动态 更新时间:2024-10-22 12:38:14
在循环中定位特定的Wordpress帖子(Targeting specific Wordpress posts in a loop)

我正在尝试定位各个帖子,以便我可以更改特定帖子的CSS(标题标签,填充等)。 我的Wordpress网站目前在循环中生成帖子。

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代码(获取后标题,类别,并将缩略图设置为背景图像)

<?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(设置图像大小)

add_theme_support( 'post-thumbnails' ); add_image_size('my-fun-size', 'thumbnail');

输出为“行”100%宽度,标题,类别和背景图像(特征图像)。 堆叠在彼此之上。 我希望能够定位不同帖子的文本和bg图像,使它们看起来各不相同。

I am trying to target individual posts so I can change the css (title tags, padding, etc) of specific posts. My Wordpress site currently generates the posts in a loop.

index.php code (brings in content.php which has 'post' code)

<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)

<?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)

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). 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.

最满意答案

我认为最好的方法是在帖子中添加自定义字段 ,然后在模板中以这种方式调用该自定义字段:

get_post_meta($post->ID, 'name_of_your_custom_field', true);

这必须在循环内。

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.

更多推荐

本文发布于:2023-08-07 02:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458833.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帖子   Targeting   Wordpress   loop   posts

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!