• hodgy06

    (@hodgy06)


    Hi,

    Ive used this code to add a widget on my static home page and I want it to display a preview of my recent posts.

    My problem is that when I have the widget display my blog page, it doesn’t show the post content as the page has no content on it, it is only displaying posts (hope this makes sense). Is it possible to have the post content displayed on the widget from the blog page?

    https://www.energyforlife.net.au/blog/

    /* add widget area before the blog, after the featured pages, only in home */
    add_filter( 'tc_default_widgets' , 'add_featured_page_widget' );
    function add_featured_page_widget( $defaults ) {
        $defaults['fp_widgets'] = array(
                        'name'                 => __( 'Featured Pages Widget' , 'customizr' ),
                        'description'          => __( 'After the featured pages area on home' , 'customizr' )
        );
        return $defaults;
    }
    
    add_action('__before_main_container' , 'display_my_fp_widget', 15 );
    function display_my_fp_widget() {
        if ( ! tc__f('__is_home') )
            return;
    
        dynamic_sidebar('fp_widgets');
    }
    add_filter('widget_text', 'do_shortcode');
    /* shortcode to display a page/post content + title */
    add_shortcode('tc_post', 'do_tc_post');
    function do_tc_post( $atts ){
        $default = array(
            'id'         => '',
        );
    
        $atts = shortcode_atts($default, $atts);
    
        if ( ! isset($atts['id']) )
            return;
    
        $_post = get_post($atts['id']);
        if ( ! isset($_post) )
            return;
        ob_start();
        printf('
            <article id="%1$s" class="post-%1$ page type-page status-publish hentry row-fluid">
              <header class="entry-header">
                <h2 class="entry-title">%2$s</h2>
              </header>
              <section class="entry-content">
                 %3$s
              </section>
            </article>',
            $_post -> ID,
            $_post -> post_title,
            apply_filters('the_content', $_post -> post_content)
        );
    
        $html = ob_get_contents();
        if ( $html ) ob_end_clean();
    
        return $html;
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi hodgy06, sorry if I don’t get your quesion.. You want to display the content of a page in your blog here https://www.energyforlife.net.au/blog/
    before the list of posts?

    Thread Starter hodgy06

    (@hodgy06)

    Hey d4z_c0nf, yeah I have the widget working to display the blog page on my home page and it works but the problem is, the content you can see on my blog page isn’t actually added on the blog page from the edit page option. If I go to edit page, its blank. I believe its just mirrored there or something as I selected the blog page in settings to display my posts on that page. hope this makes more sense

    Your page “Blog” displays your latest posts, and it’s normal since you selected it as the blog page. It’s normal that it’s blank but it displays posts ??
    So if I’m understanding it right, in your home you would like to display
    latest posts (in full width, with pagination)
    static page (with sidebar)

    but you want to keep the Blog page?

    Thread Starter hodgy06

    (@hodgy06)

    yes, not sure what pagination is but I want to keep the blog page and also have the home page (static page) widget display a preview of my latest post from my blog page.
    The widget isn’t the sidebar one, its the one you posted in someone elses post a few days ago and displays under the feature images

    Yeah I know that is my code ??
    The thing is that that code works with page contents, and you cannot have the page content of your blog page expecting it shows posts..
    I will think to a solution for this…

    Thread Starter hodgy06

    (@hodgy06)

    ok, thanks for your help. I appreciate it a lot

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘can't add blog posts to custom widget’ is closed to new replies.