Different Widget Every Single Post
-
Hi,
I am after a solution to display a different widget on every single post. The site will have hundreds of posts as it’s being run as a diary/blog format, so I am after a solution bearing this in mind.I’ve tried to implement Custom Fields alongside PHP Code Widget.
In the PHP Code Widget Sidebar propety i’ve the following code:<?php if ( is_single() ) : global $post; if ( get_post_meta( $post->ID, 'per_post_content', true ) ) : echo get_post_meta( $post->ID, 'per_post_content', true ); endif; wp_reset_postdata(); endifOn every Post, I’ve got a Custom Field named ‘per_post_content’ and inside their I’ve been inserting my WP Widget Shortcode for example;
[wp_progress_bar text="Progress" text_color="#fff" background="#eee" progress_color="#bbb" pc="90" width="100%" bottom="15px"]The widget is just blank and not displaying anything, and I am unsure if the code is wrong, or what’s the best solution to get this working.
-
First; let’s make sure the code is working properly.
if ( is_single() ) : global $post; if ( get_post_meta( $post->ID, 'per_post_content', true ) ) : echo 'TESTING'; endif; wp_reset_postdata(); endif;Does that display the word “TESTING” in the widget area?
Hi Josh,
Yes inserting this code into PHP Code widget outputs the word TESTING in Widget AreaOkay.. now let’s wrap the shortcode in a do_shortcode() function.
if ( is_single() ) : global $post; if ( get_post_meta( $post->ID, 'per_post_content', true ) ) : $shortcode = get_post_meta( $post->ID, 'per_post_content', true ); echo do_shortcode( $shortcode ); endif; wp_reset_postdata(); endif;Sorry to sound daft, the code you attached above, am i right in saying that goes into the PHP Code Sidebar Widget
And do i then in my “per_post_content” insert the following value;
do_shortcode([wp_progress_bar text="Progress" text_color="#fff" background="#eee" progress_color="#bbb" pc="90" width="100%" bottom="15px"])Or am i missing something here?
Scrap that last reply, I’ve just inserted the Code you sent above into my PHP Code widget however it needed the opening:
<?phpOnce i’d inserted this and then in my per_post_content I added:
[wp_progress_bar text="Progress" text_color="#fff" background="#eee" progress_color="#bbb" pc="90" width="100%" bottom="15px"]It’s now working just perfect!
Thanks Josh you’re a star!
My pleasure. Glad you got it working.
Yes, sorry… you need the opening
<?php.
You’ve got it, good job.You are very welcome 🙂
The topic ‘Different Widget Every Single Post’ is closed to new replies.