[wp-hackers] Including Sidebar Content via Conditionals Within Same File

Chris Olbekson chris at c3mdigital.com
Fri Aug 16 01:15:25 UTC 2013


What you are trying to do is possible.  Just a suggestion here:

In your call to wp_get_post_tags add a field argument to just get the tag slugs.  Instead of going through the loop just use the in_array() function. I would use the slug for any comparison so you don't have to worry about issues with capital letters and spacing.

global $post;
$tags = wp_get_post_tags( $post->ID, array( 'fields' => 'slug' ) );

	<div class="widget">
		<div class="widget-inner">
				if ( in_array( 'help', $tags ) ) {
					echo do_shortcode( '[ngg_images gallery_ids="1" display_type="photocrati-nextgen_basic_thumbnails"]' );

				} elseif ( in_array( 'some-other-tag', $tags ) ) {
					echo do_shortcode( '[some_other_shortcode]' );

Using your logic the $end variable will be equal to the last tag in the loop.

Good luck and I hope this helps.

On Aug 15, 2013, at 4:12 PM, Gregory Lancaster <greglancaster71 at gmail.com> wrote:

> Ouch, that stings.  You are right I am new to PHP.  I am still completing a
> class and since I enojy it, I thought I would try and move ahead to code
> some things on my own.  The = was a mistake, I know == is the correct
> syntax.  The post tags code is what I used in my sidebar previously and I
> thought it was applicable.  Is what I am trying to do even possible?
> On Thu, Aug 15, 2013 at 1:38 PM, Dobri <dyordan1 at ramapo.edu> wrote:
>> Don't mean to burst your bubble but there are multiple issues with this.
>> Here are some to consider:
>> Whenever you use if, if/else, for, foreach, etc. putting a ; at the end of
>> the line terminates it. So, essentially both your foreach and if statements
>> don't do anything and then the code that follows is executed devoid of any
>> logic. The correct syntax would be foreach($tags as $tag){… and if ($end =
>> 'help') return...
>> When testing a variable, saying for example $end = 'help' will just do an
>> assignment and return the value that was assigned which in 99% of the cases
>> will evaluate to true. So even if your if statement was correct, it would
>> always execute because you are using assignment by = instead of comparison
>> by ==. The correct syntax would be $end == 'help'
>> Even if your foreach was written correctly (as above), you are only using
>> the last value by saying $end=$tag_name. At that point, just remove the
>> loop and say $end = $tags[count($tags)-1] if you just want the last
>> element. If you want to check each element, your whole code block has to be
>> refactored
>> You are using a mixture of return and echo and I'm pretty sure you only
>> want to use one of them. If this is executing inside of a filter function
>> there is a good chance that both return and echo will render in more or
>> less the same place but that doesn't mean to mix them, stick to the better
>> practice of return in case someone wants to further filter your thing down
>> the pipe or something.
>> Overall, I have a feeling you are not very experienced with coding but you
>> made a good attempt so don't be discouraged by my previous comments! I'd
>> say try to work on the issues above and if you can't figure it out I'll
>> take a look at your first email and see if I can help you out.
>> Cheers! Don't hate me.
>> P.S. An hour is *nothing* for coding. Don't be discouraged.
>> ~Dobri
>> On Thu, 15 Aug 2013, at 4:18 PM, BenderisGreat wrote:
>>> I have been working at this for an hour, and hoping someone here can
>> chime in
>>> and solve it quick for me.  Calling left sidebar, and I would like to
>> have
>>> the sidebar determine what content to show based on the post tag.  This
>> is
>>> what I have;
>>> /<?php global $post;
>>> $tags = wp_get_post_tags($post->ID);
>>> foreach ($tags as $tag);{
>>> $tag_name = $tag->name;}
>>> $end =  $tag_name;
>>> ?>
>>> <php if $end = 'help';
>>> return '
>>> <div class="widget">
>>> Help Gallery
>>> <div class="widget-inner">
>>> <?php echo do_shortcode('[ngg_images gallery_ids="1"
>>> display_type="photocrati-nextgen_basic_thumbnails"]'); ?>
>>> </div>
>>> </div> '; ?>
>>> <?php endif; ?>/
>>> I did this with three items, but it shows all of them on every page so I
>>> assume I am using the include statament wrong.  I am still learning php,
>> but
>>> I think maybe I should be using the phpDOM function?  Not entirely sure,
>> but
>>> definitely confused how to make this work.  I just need the sidebar to
>> match
>>> the post tage name ($end) to the correct section of the file and return
>> it.
>>> Any help greatly appreciated!
>>> --
>>> View this message in context:
>> http://wordpress-hackers.1065353.n5.nabble.com/Including-Sidebar-Content-via-Conditionals-Within-Same-File-tp42060.html
>>> Sent from the Wordpress Hackers mailing list archive at Nabble.com.
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers at lists.automattic.com
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list