[wp-hackers] Including Sidebar Content via Conditionals Within Same File
Dobri
dyordan1 at ramapo.edu
Thu Aug 15 20:38:47 UTC 2013
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
More information about the wp-hackers
mailing list