My Wordpress site has a loop that creates posts, and I want to target specific posts to change their css values.
html - index.php
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content' );
?>
<?php endwhile; ?>
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="container-fluid">
<div class="col-md-12 text-cell">
<h2><?php the_title(); ?></h2>
<?php the_category(', '); ?>
</div>
</div>
</article><!-- /#post -->
It was suggested that I use 'get_post_meta ...' but I am not familiar with how to use it. I just want to change css values (padding, font-size, etc) for different posts
I would suggest using Custom Fields for this, so that you can define the values in the post itself, as opposed to having to edit the code every time you add a new post.
In the post, make sure you have Custom Fields visible (Togglable from screen options at the top)
Then make a field called "Alignment" or whatever you prefer and assign a value to it. (for example 'left')
Then you can add a conditional in the loop.
<?php $alignment = get_post_meta(get_the_ID(),'Alignment',true);
if($alignment) == 'left'):?>
<p>Do stuff and things here...</p>
<?php endif;?>
You can read more about it here: https://codex.wordpress.org/Custom_Fields
Hopefully that will work for you. If you want to get fancier with it, I would suggest looking at the Advanced Custom Fields plugin, which allows a lot more flexibility and options.
EDIT from comments:
1st option:
Set a field of "ExtraCSS" to "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'); ?> >
2nd option:
(In your stylesheet:)
article:nth-child(2n+0)
{
color:green;
}
http://www.w3schools.com/cssref/sel_nth-child.asp
Related
I have a code chunk that is inside the_content(); I'm using acf repeater as well. So when I post a blog, I'll either use the_content(); or the acf field. I have h2 tag ( latest articles ) that I only want printed one time, but it's printing everytime I make a post.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="container">
<div class="row">
<div class="col-md-4 sidebar-r">
<?php echo the_content(); ?>
</div><!-- end sidebar-r -->
<?php
$i = $wp_query->post_count;
if($i <=1) {
echo '<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>';
}else{
echo '';
}
?>
<div class="col-md-8 links-wrap">
<?php if(have_rows('daily_links')): ?>
<?php while(have_rows('daily_links')): the_row(); ?>
<a href="<?php the_sub_field('link_url'); ?>" target="_blank">
<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>
<h3 class="link-source">
<?php the_sub_field('link_source'); ?>
</h3>
</a>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- end links wrap -->
</div><!-- end row -->
</div><!-- end container -->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
You'll see I tried using php to count the posts and if more than one post, don't print the tag, but couldn't figure out the exact logic and syntax.
I am honestly struggling a bit to understand exactly what you are trying to do and since I do not even have the posts and other key pieces of information so that I can properly replicate your issue so that I can help you better, this is a little bit challenging. That being said, looking into some ideas I came across another stackoverflow question/answer that might be relevant for you in catching the first post and does something to it. The answer to the referenced question instance was this:
<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>
<?php if($postCount == 2) { ?>
// SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
// SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>
This was suggested by user Bora in this answer from 2013.
Let me know if that helped!
I am trying to have two different queries in my Loop, which is located in index.php. I am working with WP codex, but it isnt working. I want to have every post in its own special DIV later, so this is just start of my work.
The problem is, that the second part of code doesnt work, and I have no idea why. As far as I have read the codex, everything should be fine. Please help me.
<div class="col1">
<?php
$my_query = new WP_Query('category_A tym=featured&posts_per_page=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
?>
<!-- Do stuff... -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<!--Over here everything works fine!-->
<!--This code doesnt show up. It is supossed to show 1 post, only heading and date with author. But it doesnt show nothing at all.-->
<?php
$my_queryOne = new WP_Query('posts_per_page=1');
while ($my_queryOne->have_posts()) : $my_queryOne->the_post();
if ($post->ID == $do_not_duplicate)
continue;
?>
<!-- Do stuff... -->
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<?php
endwhile;
?>
</div>
WP_Query takes an array as a parameter - i.e.
$query = new WP_Query( array( 'category_name' => 'featured','posts_per_page' => 1 ));
Also, when running multiple queries - use wp_reset_postdata() after the first loop.
Great examples here:
https://codex.wordpress.org/Class_Reference/WP_Query
Its probably best to review the coding guidelines that WordPress has laid out. You need to ensure that every open and close tag for inline PHP is on its own line.
Check here for reference:
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#opening-and-closing-php-tags
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><?php the_title(); ?></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.
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.
I'm trying to pull post types into my Bones based theme, and I'd like those posts to live inside of divs. eg.
<div class="fourcol"></div>
The problem is, some of the divs will also need a class of "first" or "last".
Right now, I'm using JQuery to find all of the divs with the class "fourcol" and adding a "first" class to the first div in the row, and a "last" class to the last div in the row. It seems to work, but there's a little bit of shifting when the page loads, and it doesn't seem as safe as I'd like it to be. It also causes issues when trying to filter the post types, as the first and last div are constantly changing, and the jQuery isn't reloading to reflect the posts new position.
Is there better way to pull these posts into divs?
I've tried everything I can to come up with minor fixes, but I'm sure it's come up before so I figured I'd post it here. Let me know if you have any suggestions!
Here's my loop so far:
<?php query_posts( array( 'cat' => 10, 'post_type' => 'people', 'showposts'=>-1 ) ); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="threecol">
<article id="post-<?php the_ID(); ?>" <?php post_class( 'clearfix' ); ?> role="article">
<header class="article-header">
<center><h3><?php the_title(); ?></h3></center>
</header> <?php // end article header ?>
<section class="entry-content clearfix">
<a class="ctp-hover" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<span class="hover-caption">VIEW</span>
<?php the_post_thumbnail('full'); ?>
</a>
</section> <?php // end article section ?>
</article> <?php // end article ?>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
So I'm editing the index.php in my WP theme's folder, essentially what I want it to do is this:
Show 4 Posts on first page of blog.
Style the first (newest) post on the first page differently. Style the other 3 the same.
Show 6 Posts on every other paginated page.
Here's the loop I'm working with, the first page appears fine, but for some reason on page 2 and higher it's executing really weird and showing only one post on each additional page. Also, it will only show the title and not the date or excerpt. Here's my code:
<?php
if( is_home() && !is_paged() ){
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 4;
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
if (++$counter == 1) { ?>
<div class="featured_post">
<p class="date"><?php the_date('M j, Y'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail('featuredblog'); ?>
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="regular_post">
<p class="date"><?php the_date('M j, Y'); ?></p>
<div class="postimage"><?php the_post_thumbnail('regularblog'); ?></div>
<a href="<?php the_permalink(); ?>"><h3><?php
// short_title($after, $length)
echo short_title('...', 7);
?></h3></a>
<?php the_excerpt(); ?>
</div>
<?php } ?>
<?php endwhile;
else :
// Code for no posts found here
endif;
} else {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 6;
query_posts($args); ?>
<div class="regular_post">
<p class="date"><?php the_date('M j, Y'); ?></p>
<div class="postimage"><?php the_post_thumbnail('regularblog'); ?></div>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</div>
<?php } ?>
Maybe there's too many if...else statements in there?
Are you using some kind of framework? that's a pretty bad tempalte engine.
Anyway if yuo need to add style, you should leave the job to CSS.
if you need extremly precision you can just leave a class with a counter for each element you need to style differently.
like <div class="Element<?php echo ++$counter; ?>"></div>
and then you can add your styles in CSS
.Element1,.Element2,.Element3 {styles}
.Element4 {other styles}
in this way you simplify the life with your php code
Keep in mind one day you will not even need to add a class counter.. In the near future you could use the nth-child css selector http://reference.sitepoint.com/css/pseudoclass-nthchild
Edit: damn me I replied someone with 50% AR
my suggestion would follow yes123, but really leave it all to css. basically, all of your posts should be wrapped in a parent div, and each should be classed the same way.
<div id="content">
<div class="post">
<div class="title"></div>
</div>
<div class="post">
<div class="title"></div>
</div>
<div class="post">
<div class="title"></div>
</div>
<div class="post">
<div class="title"></div>
</div>
</div>
that's about what you should resolve to, with more tags in the post, obviously.
from there, the way to style the first post differently would be using the #content > .post:first-child selector.
also, having a different number of posts per page is probably not a good idea. i've actually never really tried it, but i can see where the paging would get screwed up, and you'd probably never see posts 5 or 6. i'm thinking wordpress would probably say that with 6 posts per page, page 2 should always start with post 7. not positive though.
<?php if (is_home() && !is_paged() ) {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 4;
query_posts($args);
} else {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 6;
query_posts($args);
} ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="post<?php echo ++$counter; ?>">
<?php if ( is_home() && !is_paged() && $counter == 1 ) { ?>
do stuff
<?php } else { ?>
do other stuff
<?php } ?>
</div>
<?php endwhile; ?>