• Resolved flynsarmy

    (@flynsarmy)


    Love your plugin, works a treat.

    We wanted to use a slightly differen widget front end HTML structure than what your plugin provides, so I added a small (backwards compatable) patch to support this.

    In /custom-post-widget/post-widget.php custom_post_widget::widget() change

    $content = $content_post->post_content;
    if ( !$apply_content_filters ) { // Don't apply the content filter if checkbox selected

    to

    $content = $content_post->post_content;
    
    // Display custom widget frontend
    if ( $located = locate_template( 'custom-post-widget.php' ) ) {
    	require $located;
    	return;
    }
    
    if ( !$apply_content_filters ) { // Don't apply the content filter if checkbox selected

    Example usage:

    Add custom-post-widget.php to your theme folder. Copy the rest of the function into the file with any modifications you want to make:

    <?php
    if ( !$apply_content_filters ) { // Don't apply the content filter if checkbox selected
    	$content = apply_filters( 'the_content', $content);
    }
    echo $before_widget;
    if ( $show_custom_post_title ) {
    	echo $before_title;
    	echo apply_filters( 'widget_title',$content_post->post_title);
    	if ( $show_featured_image ) {
    		echo get_the_post_thumbnail( $content_post -> ID );
    	}
    	echo $after_title; // This is the line that displays the title (only if show title is set)
    }
    echo do_shortcode( $content ); // This is where the actual content of the custom post is being displayed
    echo $after_widget;

    In the example above I moved the post thumbnail to before the closing HTML tag. It’s a small example but it shows how easily a post structure could be modified using this method.

    http://wordpress.org/plugins/custom-post-widget/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Johan van der Wijk

    (@vanderwijk)

    Thanks for providing this code, I have added it to version 2.3.5 which I have just released!

    Hey guys,

    Nice work on the patch and also the plugin!

    A question that both flyns’ and Johan may be able to answer:

    Is there any way to adjust your patch to pull a template created for a specific content block using the unique ID assigned to each block…

    eg. custom-post-widget-1.php <— would be applied to custom post widget id-1 and so-on

    Thanks alot,

    and keep up the great work.

    Plugin Author Johan van der Wijk

    (@vanderwijk)

    Hi willwillok,

    This should be relatively easy to acomplish. All you have to do is create a template in your theme folder as described above and in that template you can add the mechanism to vary the layout based on the ID of the content block that is called (if post ID = bla then do this, else do that).

    Wohoo, finally got this working, although, to me ‘relatively easy’ is a bit of an understatement 😛 I know, I know… two lines of code, but i’m a hack/slasher and this took me a while to get working!

    My template code (will take the post ID, check if there is a template named ‘custom-post-widget-###.php and will include. Otherwise it will just return the $content as per above )

    NB: this code pulls the wordpress post ID not the number assigned to the custom posts
    I.e NOT custom_post_widget-1!!!
    templates must be name: ‘custom_post_widget-[wpPostID].php

    <?php
    if ( !$apply_content_filters ) { // Don't apply the content filter if checkbox selected
    	$content = apply_filters( 'the_content', $content);
    }
    echo $before_widget;
    if ( $show_custom_post_title ) {
    	echo $before_title;
    	echo apply_filters( 'widget_title',$content_post->post_title);
    	if ( $show_featured_image ) {
    		echo get_the_post_thumbnail( $content_post -> ID );
    	}
    	echo $after_title; // This is the line that displays the title (only if show title is set)
    }
    
    if (file_exists( get_stylesheet_directory(). "/custom-post-widget-{$content_post -> ID}.php")):
    include( get_stylesheet_directory() ."/custom-post-widget-{$content_post -> ID}.php");
    
    else:
    
    echo do_shortcode( $content );
    endif; 
    
    echo $after_widget;

    Thanks for your initial work/help on this guys

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[PATCH] Custom widget frontends’ is closed to new replies.