• Resolved kanabi

    (@kanabi)


    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:
    https://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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’);

    Thread Starter kanabi

    (@kanabi)

    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.

    Thread Starter kanabi

    (@kanabi)

    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');
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change the first class if post page is 1’ is closed to new replies.