• Resolved Guido

    (@guido07111975)


    Hi,

    I’m trying to display custom content from my event plugin on the archive/category page. So I hook into the the_content filter:

    function archive_content( $content ) {
    	$var1 = esc_attr__( 'First var', 'textdomain' );
    	$var2 = esc_attr__( 'Second var', 'textdomain' );
    
    	if (get_post_type() == 'event' && is_archive()) {
    		$content = '<div id="mydiv">' . '<div class="myvar">' . $var1 . $var2 . '</div>' . $content . '</div>';
      	}
    	return $content;
    }
    add_filter( 'the_content', 'archive_content' );

    This works, “First var” and “Second var” (and the default post content) are displayed when viewing the archive/category page.

    BUT, no styling is applied (no id and class in source code either). It’s all stripped. How come?

    Guido

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Guido

    (@guido07111975)

    I think I’ve found it…

    It has something to do with the filter I call (content vs excerpt), because this does work in my case:

    add_filter( 'the_excerpt', 'archive_content' );

    So I guess it depends on the archive file of the installed theme. This file calls for the content or excerpt.

    How can I determine which of the two is being used?

    Or just use a double filter?

    add_filter( 'the_excerpt', 'archive_content' );
    add_filter( 'the_content', 'archive_content' );

    Guido

    Moderator bcworkz

    (@bcworkz)

    Hello Guido!

    The only way to know which function a template uses is by which filter fires, so yes, you can add your callback to both filters to cover either condition.

    I’m unable to replicate the stripped HTML behavior you described with ‘the_content’, so I suspect there’s something about your theme or one of your site’s activated plugins that is doing this. It might even be your own plugin? Could it be some debugging code that is still active? I’ve done something similar before, it’s not hard to do. Try temporarily copying the content filter code to your theme’s functions.php, then deactivating your plugin. If the HTML is not stripped, then it’s your own doing at fault!

    If the HTML is still stripped, restore your plugin and the original functions.php, then do the usual procedure for narrowing down errant code. Revert to default state (where HTML should not be stripped), then reactivate the usual theme and plugins one by one, checking for stripped behavior after each one.

    BC

    Thread Starter Guido

    (@guido07111975)

    Hi BC,

    Thanks for your response.

    Great idea, to try this in a theme instead of the plugin itself.

    I now notice the double filter causes problems. In my own themes both vars are displayed twice(!): 1x correct and 1x stripped. My themes call the_excerpt in file archive.
    In other themes such as twenty twelve it does display correct.

    Will continue testing tomorrow.

    Guido

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Hook into archive / category template’ is closed to new replies.