我试图将另一个类添加到<div class="postthumbnail">
。我想把一个帖子打造成不同的风格。因此,经过研究,我发现最好的方法是使用自定义字段将类添加到帖子中。
本教程How to style WP posts different,特别是底部的这一部分,解释了如何使用自定义字段向帖子添加类。我通过给自定义字段命名&#34; en_proceso_class&#34;和价值,&#34; en_proceso&#34;这是一个css类。但我对我需要添加的最后两个代码感到困惑。该教程并不清楚我需要添加它们的位置。
我的原始代码是:
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=9&posts_per_page=9&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<div class="proyectpost">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="innerpost">
<div class="postthumbnail">
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
} ?>
</div>
<div class="posttitle">
<h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<span><?php echo get_post_meta($post->ID, 'location', true); ?></span>
</div><!-- .entry-header -->
<div class="postsubtitle">
<div class="datepanel">
</div>
</div>
</div>
</article><!-- #post-## -->
</div>
<?php endwhile; ?>
<div class="paginationbar">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages,
'show_all' => True,
'prev_next' => False
) );
?>
</div>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
这是我尝试将新代码添加到的代码部分:
<div class="innerpost">
<?php $custom_values = get_post_meta($post->ID, 'en_proceso_class'); ?>
<div class="postthumbnail <?php en_proceso_class('class-1 class-2 ' . $custom_variable); ?>">
<?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
} ?>
</div>
我需要做些什么才能让它发挥作用?
答案 0 :(得分:1)
The last part isn't 100% clear but I'll try to answer as best I can.
This piece of code:
$custom_values = get_post_meta($post->ID, 'en_proceso_class');
Gets the value of a post meta field name: 'en_proceso_class'. You actually need to set that first in order for this to work. And you need to add 'true' as another parameter to that function. See here for more info: https://developer.wordpress.org/reference/functions/get_post_meta/
Then there's this:
div class="postthumbnail <?php en_proceso_class('class-1 class-2 ' . $custom_variable); ?>">
Which calls a function named 'en_proceso_class' -- I don't think this is what you want to do. Unless you declared that function beforehand you'll to do something like:
div class="postthumbnail <?php echo 'class-1 class-2 ' . $custom_variable; ?>">
So the whole code put together would look like this:
<div class="innerpost">
<?php
// Get post meta that is already set
$custom_values = get_post_meta($post->ID, 'en_proceso_class', true);
?>
<div class="postthumbnail <?php echo 'class-1 class-2 ' . $custom_values; ?>">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
echo '<img src="' . $image_src[0] . '" width="100%" />';
}
?>
</div>
Cheers