WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Change the first class if post page is 1 (4 posts)

  1. kanabi
    Member
    Posted 1 year ago #

    Hi, I've found this function:

    function first_paragraph($content)
    {
        if(is_single()) // make paragraph bold for single pages only
        {
            return preg_replace('/<p([^>]+)?>/', '<p$1 class="lead">', $content, 1);
        }else{
            return $content;
        }
    }
    add_filter('the_content', 'first_paragraph');

    How to modify it to change the class of the first <p> only on the first page of the post (divided into pages using the <!--nextpage--> tag (replace the is_single condition)?

    For post pagination/navigation I'm using the following script:
    http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin

  2. justingreerbbi
    Member
    Posted 1 year ago #

    Why not just wrap your content in a class or id?

    This way you can use jQuery to add class:

    Example:

    <div class="content_hook">
    <p>asdasdadadasd</p>
    <p>sdfsdfsdfsfsdf</p>
    </div>

    Using jQuery to add class or change class
    $('content_hook p:first').removeClass('Classname');

    Then you could just change add your class you want
    $('content_hook p:first').addClass('Classname');

  3. kanabi
    Member
    Posted 1 year ago #

    Thank you for your reply, but it's not the issue. I just want to make the above script change the "lead" class (which is already defined) whenever I'm on the first page of a post divided by the <!--nextpage--> tag and leave it unchanged on the other pages of a given post.

    Please help! I'm pretty sure the others would be interested in this solution as well.

  4. kanabi
    Member
    Posted 1 year ago #

    Ok, I've worked out my own solution. Up and running:

    function first_paragraph($content)
    {
    	$page = get_query_var('page');
    	if ($page < 2) {
    	return preg_replace('/<p([^>]+)?>/', '<p$1 class="lead">', $content, 1);
    	}
    	else {
    	return $content;
    	}
    }
    add_filter('the_content', 'first_paragraph');

Topic Closed

This topic has been closed to new replies.

About this Topic