WordPress.org

Ready to get started?Download WordPress

Forums

Add html code to a page via theme based on body class (2 posts)

  1. iamaaronryan
    Member
    Posted 1 year ago #

    I would like to add some code to a theme that adds some html code to a page or post based on the body class of a page/post. The user would add a custom body class in the page/post editor when creating the post. Different custom classes would have different images displayed. Something like below:

    if body class="class name" {
    			echo '<img src="http://www.website.com/image.jpg">';
    		}

    Disclaimer, I have no php programming knowledge. My experience has been limited to being able to copy some code and use it other places.
    I know I have the first line completely wrong just trying to convey what I want to do.

    Regards

  2. alchymyth
    Forum Moderator
    Posted 1 year ago #

    possibly use get_body_class() and check the returned array for "class name"

    http://codex.wordpress.org/Function_Reference/get_body_class

    example:

    if( in_array('class name', get_body_class()) ) {
    			echo '<img src="http://www.website.com/image.jpg">';
    		}

    http://php.net/manual/en/function.in-array.php

Topic Closed

This topic has been closed to new replies.

About this Topic