WordPress.org

Support

Support » Themes and Templates » [Resolved] How can I connect a page to an attachment post of an img from a gallery in CSS

[Resolved] How can I connect a page to an attachment post of an img from a gallery in CSS

  • 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.

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

    @esmi

    Forum Moderator

    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.

    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');

    esmi

    @esmi

    Forum Moderator

    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.

    Yep that’s the one – many thanks es!!!

    D

    Resolved

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Resolved] How can I connect a page to an attachment post of an img from a gallery in CSS’ is closed to new replies.
Skip to toolbar