WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Widget
[resolved] [PATCH] Custom widget frontends (5 posts)

  1. flynsarmy
    Member
    Posted 10 months ago #

    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/

  2. Johan van der Wijk
    Member
    Plugin Author

    Posted 8 months ago #

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

  3. willwillok
    Member
    Posted 6 months ago #

    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.

  4. Johan van der Wijk
    Member
    Plugin Author

    Posted 6 months ago #

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

  5. willwillok
    Member
    Posted 5 months ago #

    Wohoo, finally got this working, although, to me 'relatively easy' is a bit of an understatement :P 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

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.