WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How can I connect a page to an attachment post of an img from a gallery in CSS (6 posts)

  1. dennisbrewer
    Member
    Posted 1 year ago #

    Hi guys,

    Have a different text colour on three of my pages and a different default colour. When I use a gallery and click the image it goes through to an attachment post page with no relation to it's parent page and uses the default style.

    How can I get the gallery or the attachment post to show somekind of link to its parent [class or id] that will let me style the correct text colour in css?

    Thanks.

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    Not tested but something like:

    function my_attachment_body_class($classes) {
    	global $post;
    	if( $is_attachment() ) $classes[] = 'parent-' . $post->post_parent;
    	return $classes;
    }
    add_filter('body_class', 'my_attachment_body_class' );

    added to the theme's functions.php file should add an extra class to the body element in the form post-xxx where xxx is the parent's id.

  3. dennisbrewer
    Member
    Posted 1 year ago #

    Ah thanks esmi.

    I pop it at the bottom of functions.php and nothing seems to change until I take the if statement out though. Then it works fine but obviously is on all the pages...

    As it's a image post from a wordpress gallery does that mean I need to call it something else than if( $is_attachment )?

    Just to be clear the following does what I required but justs adds the parentis on all the pages not just the image gallery pages.

    function my_attachment_body_class($classes) {
    	global $post;
    
    	$classes[] = 'parentis-' . $post->post_parent;
    
    	return $classes;
    }
    add_filter('body_class', 'my_attachment_body_class');
  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Try:

    function my_attachment_body_class($classes) {
    	global $post;
    	if( $post->post_type == 'attachment' ) $classes[] = 'parent-' . $post->post_parent;
    	return $classes;
    }
    add_filter('body_class', 'my_attachment_body_class' );

    That works for me.

  5. dennisbrewer
    Member
    Posted 1 year ago #

    Yep that's the one - many thanks es!!!

    D

  6. dennisbrewer
    Member
    Posted 1 year ago #

    Resolved

Topic Closed

This topic has been closed to new replies.

About this Topic