This topic contains 10 replies, has 2 voices, and was last updated by  pwwwpwww 3 years, 2 months ago.

  • Author
    Posts
  • #5577

    pwwwpwww
    Participant

    I’m new PHP / wordpress and I have to admit I’m struggling. The idea is to be able to create a bunch of reusable content aka content blocks. When creating a new post or page I want to be able to select which content blocks to display along side it.

    So far I’ve created the CPT:

    'content_blocks' => array(
    'labels' => piklist('post_type_labels', 'Content Block')
    ,'supports' => array(
    'title'
    )
    ,'public' => false
    ,'show_ui' => true
    ,'publicly_queryable' => false
    ,'exclude_from_search' => true
    ,'has_archive' => false
    ,'hide_screen_options' => true
    )

    And I’ve attached some fields to it like:

    piklist('field', array(
    'type' => 'file'
    ,'field' => 'block_img'
    ));

    And

    piklist('field', array(
    'type' => 'editor'
    ,'scope' => 'post'
    ,'field' => 'post_content'
    ,'template' => 'field'
    ));

    I’ve also created an addmore select field that lists the published content block CPTs. It allows me to choose multiple content blocks and arrange their order. I have attached it to posts and pages:

    piklist('field', array(
    'type' => 'select'
    ,'field' => 'attach_blocks'
    ,'label' => 'Add content blocks'
    ,'add_more' => true
    ,'choices' => piklist(
    get_posts(
    array(
    'post_type' => 'content_blocks'
    ,'orderby' => 'ID'
    ,'order' => 'asc'
    )
    ,'objects'
    )
    ,array(
    'ID'
    ,'post_title'
    )
    )
    ));

    So far so good. Now I’m struggling to figure out how to display the content blocks in my theme’s sidebar (outside the post’s loop). It needs to display the title, main content and associated meta fields. Does anyone have any tips or code examples that might help me get started? Is this even the best way to go about it?

  • #5589

    Steve
    Keymaster

    @pwwwpwww– You can access the Piklist data using standard WordPress functions. Try this:

    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'Your-Custom-Field', true);
    wp_reset_query();
    
  • #5628

    pwwwpwww
    Participant

    Hmm, not sure that’ll work for what I’m trying to achieve. Your code example just pulls in the first entry of my attach_blocks field. I need to somehow loop through the attach_blocks data, i.e the list of content_blocks associated with the page, and then display their contents (title, post_content, block_img). Does this make sense?

  • #5632

    pwwwpwww
    Participant

    Well, after almost a day googling I’m still none the wiser.

    And to add to my failure, I tried replicating the advanced add-more example in the docs without success. Considering I was just copying and pasting the example code, adjusted only for the template file paths, I have no idea why it wouldn’t work – only the days would print and not the workout schedule (reps, sets etc). I won’t even mention what happened when I tried outputting the Page Modules addmore demo in my theme 🙁

    On that note, I think I’ll have to return to ACF. I appreciate it doesn’t store data very well, but the learning curve for non-devs feels less steep. Plus there’s plenty of example code floating around the web for pretty much everything I need to do.

    Either way, thanks for trying to help. Maybe when I get my head around PHP I’ll give your plugin another go.

  • #5633

    Steve
    Keymaster
    This reply has been marked as private.
  • #5635

    pwwwpwww
    Participant
    This reply has been marked as private.
    • #5638

      Steve
      Keymaster
      This reply has been marked as private.
  • #5686

    pwwwpwww
    Participant
    This reply has been marked as private.
  • #5713

    pwwwpwww
    Participant

    Well, I’ve just about managed to get it working. My code needs cleaning up, but it’s a start:

    global $post;
    $post_objects = get_post_meta($post->ID, 'attach_blocks');
    
    if( $post_objects ): ?>
        
        <?php foreach( $post_objects as $post): // variable must be called $post ?>
            <?php 
            setup_postdata($post);
            $blockstyle = get_post_meta($post, 'block_style', true);
            $blockimg = get_post_meta($post, 'content_block_img', true);
            ?>
            
            <section>
                <div class="<?php echo $blockstyle; ?>">
                <h3><?php the_title(); ?></h3>
                <div><?php echo '<img src="' . wp_get_attachment_url($blockimg) . '" alt="" />';?></div>
                <?php the_content(); ?>
                </div>
            </section>
        <?php endforeach; ?>
        
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
  • #5724

    Steve
    Keymaster

    Nice job!

  • #5731

    pwwwpwww
    Participant

    Cheers Steve – just nested arrays to figure out now! You can go ahead and mark this as resolved (I’m sure I’ll fire-up another thread soon enough…)

The topic ‘I need help creating reusable content blocks and showing them on the frontend’ is closed to new replies.