January 12, 2016 at 12:02 pm #5577
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' ));
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?
January 13, 2016 at 11:12 pm #5589
@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();
January 15, 2016 at 11:25 am #5628
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?
January 15, 2016 at 4:05 pm #5632
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.
January 15, 2016 at 4:53 pm #5633This reply has been marked as private.
January 15, 2016 at 5:43 pm #5635This reply has been marked as private.
January 15, 2016 at 10:28 pm #5638This reply has been marked as private.
January 21, 2016 at 2:45 pm #5686This reply has been marked as private.
January 25, 2016 at 11:32 am #5713
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; ?>
January 25, 2016 at 12:36 pm #5724
January 25, 2016 at 1:05 pm #5731
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.