• Dot22

    (@dot22)


    Hello everyone! First of all, sorry me if my English isn’t very clear but it’s not my native language, so I’ll try to do my best with these questions.

    I’ve a site running the last version of WordPress and I need to introduce two modifications regarding posts and style sheet. To begin, I want to give a different style to the first post on index.php page but I don’t know how to do that. ?It’s necessary to modify that on content.php page or can I define the style for that post on index loop?

    Also I’ve three categories on the site (let’s suppose they’re CAT A, CAT B and CAT C). All the posts under a certain category must have a different style on the title and button read more. CAT A style must be “btn-more-A”, CAT B must be “btn-more-B” and CAT C (or any other future category) must be “btn-more-A”. I’m using this code on functions.php to add the read more link…

    function new_excerpt_more( $more ) {
    	if ( in_category('cat_a') || is_category('cat_a') )
        return '...</p> <a href="'. get_permalink( get_the_ID() ) . '" class="btn-more-A">READ MORE...</a>';
    
      	elseif ( in_category('cat_b') || is_category('cat_b') )
        return '...</p> <a href="'. get_permalink( get_the_ID() ) . '" class="btn-more-B">READ MORE...</a>';
    
     	else {
    	return '...</p> <a href="'. get_permalink( get_the_ID() ) . '" class="btn-more-A">READ MORE...</a>';
    }
    }
    add_filter( 'excerpt_more', 'new_excerpt_more' );

    Thanks in advance, the forum is really helpfull! =)

Viewing 5 replies - 1 through 5 (of 5 total)
  • megafreechips

    (@megafreechips)

    Regarding the detection of the first post, I would modify index.php
    For example, you can do something like this:

    $postIndex = $wp_query->current_post;
    $isFirst = $post_index == 0;
    
    <article class="blogpost <?php echo $isFirst ? 'first-post' : '' ?>">
    ...
    </article>

    Hope it helps.

    Thread Starter Dot22

    (@dot22)

    Thanks for your reply megafreechips. The code must be included inside the loop, right?

    megafreechips

    (@megafreechips)

    Yes it should be used inside the loop, so it’s aware of the post index.

    Thread Starter Dot22

    (@dot22)

    I’ve tried to get it work but it didn’t. The code of the loop in my index.php page is:

    <?php /* The loop */ ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    				<?php get_template_part( 'content', get_post_format() ); ?>
    			<?php endwhile; ?>
    
    		<?php else : ?>
    megafreechips

    (@megafreechips)

    So you should put it inside the relevant post_format template file that you are displaying.
    Please note that the use of ‘article’ tag I showed and the ‘blogpost’ class are just for an example, you should adapt it to your blog-post tag & class of course.

    Can you provide an example of where & how you used my snippet?
    And how did you check if it worked or not?
    As basically the class might have been added, but if you haven’t added any style to it you wouldn’t see any change.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Questions about functions.php’ is closed to new replies.