Help with including custom templates using variables

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with including custom templates using variables

BenderisGreat
I have a theme that I would like to include unique left-sidebar for some
posts.  The custom sidebar templates are formatted like
"left-sidebar-$tag", $tag being the taged posts I want this sidebar to show
up on.

How can I append the tag name using wp_get_post_tags to the end of the file
name?  I tried calling it within the string, no success.  I am still
learning php so if this is obvious I apologize - but any help would be
greatly appreciated!

Greg
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

J.D. Grimes
Hi Greg,

wp_get_post_tags() returns an array of tags for the post. By default, each of the tags in the array is represented by the tag object (see https://codex.wordpress.org/Function_Reference/wp_get_post_tags#Examples). But you don't need the whole object for each tag, only the tag name. You can change the behavior of wp_get_post_tags() to return only the tag names using the $args parameter like this:

$args = array( 'fields' => 'names' );
$tag_names = wp_get_post_tags( $post->ID, $args );

$tag_names will now contain an array of the names of the tags for the post. If you just want to load the sidebar based on the first tag, then you could do this:

$tag = $tag_names[0];
$file = "left-sidebar-$tag";

If your posts will have several tags, then you may need to do something more sophisticated instead to determine which tag takes precedence for sidebar loading.

I hope that helps.

-J.D.

On Aug 12, 2013, at 2:05 AM, Gregory Lancaster <[hidden email]> wrote:

> I have a theme that I would like to include unique left-sidebar for some
> posts.  The custom sidebar templates are formatted like
> "left-sidebar-$tag", $tag being the taged posts I want this sidebar to show
> up on.
>
> How can I append the tag name using wp_get_post_tags to the end of the file
> name?  I tried calling it within the string, no success.  I am still
> learning php so if this is obvious I apologize - but any help would be
> greatly appreciated!
>
> Greg
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

BenderisGreat
I got it kind of working, and thank you for your feedback.  Each post only makes use of one tag, so currently choosing which tag shows isnt an issue.  I do have an issue with both sidebars loading though.  I want it to look to see if the custom sidebar exists and if not use the default.  heres the code I came up with;
   
   
<?php global $post;
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag);{
$tag_name = $tag->name;}
$end =  $tag_name;


 <?php get_template_part('/templates/left-sidebar-'.$end.''); ?>  (this checks for the left-sidebar with $tag appended)
<?php else: ?>
    <?php endif; ?>
        <?php get_template_part('templates/left-sidebar'); ?>      
     

Anyone have any idea how I can do that?  
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

Helen Hou-Sandi-2
http://codex.wordpress.org/Function_Reference/locate_template

Also take a look at get_queried_object_id() rather than global $post;
$post->ID and the args for wp_get_post_tags() - you can have it return just
names and then you should be able to array_pop() it.
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

Nikola Nikolov
>>I do have an issue with both sidebars loading though.  I want it to look
to
>>see if the custom sidebar exists and if not use the default.  heres the
code
>>I came up with...

You can simply use the second argument of get_template_part() like so:

<?php get_template_part('/templates/left-sidebar', $end); ?>

This will first look for the file "/templates/left-sidebar-{$end}.php" and
if it doesn't exist, it will look for the file
"/templates/left-sidebar.php". If that doesn't exist either nothing
happens.

You can read about the function in the codex -
http://codex.wordpress.org/Function_Reference/get_template_part

Nikola


On Mon, Aug 12, 2013 at 8:40 PM, Helen Hou-Sandi <[hidden email]> wrote:

> http://codex.wordpress.org/Function_Reference/locate_template
>
> Also take a look at get_queried_object_id() rather than global $post;
> $post->ID and the args for wp_get_post_tags() - you can have it return just
> names and then you should be able to array_pop() it.
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

Helen Hou-Sandi-2
>
> You can simply use the second argument of get_template_part() like so:
>
> <?php get_template_part('/templates/left-sidebar', $end); ?>
>
>
Meant to put that right at the top of my reply, oops :) Definitely start
there.
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

BenderisGreat
In reply to this post by Nikola Nikolov
That did it! Thank you so much!  I spend over 2 hours trying to figure the correct if else statement.  Wish I would have taken the time to read that before.  Much appreciated!
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

BenderisGreat
Is there a way to use only one file for the alternative sidebar, and have conditionals within the extra file?  Rather than have so many extra sidebar files?
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

BenderisGreat
In reply to this post by Helen Hou-Sandi-2
I actually did look at get_queried_object_id but I couldnt get it to call the tag of the current active post outside the loop.
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

J.D. Grimes
In reply to this post by BenderisGreat
> Is there a way to use only one file for the alternative sidebar, and have
> conditionals within the extra file?  Rather than have so many extra sidebar
> files?

Definitely.  If each of the extra sidebar files is going to have mostly the same code, then you would probably be better off combining them and using conditionals. It depends on exactly what you are trying to do; whichever way makes the most since, do it.

- J.D.
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

BenderisGreat
I am still learning php, so I apologize for the questions.  Can you point me in the right direction to learn how to do this?
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

J.D. Grimes
> I am still learning php, so I apologize for the questions.  Can you point me
> in the right direction to learn how to do this?

Just create the sidebar template like you would have for one of the single tags, but instead of hard-coding things, make them dynamic. For example, where you would have put the tag name like "My Cool Tag", instead do something like this:

<?php echo $tag->name; ?>

You will need to get the tag, the same way that you are before you load the template. Or you may not need to get the tag at all until inside of the custom sidebar template (that depends on whether you know you are going to load that template before you check the post's tags).

If you post an example of what you are trying to achieve, we can help you make it dynamic.

- J.D.
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

BenderisGreat
Thank you, I can hardly believe how helpful people are here.  I am using
Roots framework, and I use post tags to associate posts with one another.
 So a group of related posts are all tagged "lightbulbs", which then a
sidebar code I wrote uses to grab all posts tagged with that and show them
in a sidebar style menu.  I opted for this over custom fields because you
can add and remove tags quickly from the quick edit menu, whereas with
custom fields you have to go in and manually edit every post to change
anything.  Although looking at what I am learning in this php class it
might have made more sense to just add the custom fields to the quick edit
screen.

Anyhow, so thats how the sidebar works.  The code I pasted above earlier is
for a left-sidebar used for gallery images and bootstrap scrollspy to show
people where they are on specific pages.  Thats why I need it to call based
on the post tag- so it shows along with the appropriate content.

Im not sure what else to share?  Knowing all that, is it still possible to
call the sidebar from one file with conditionals?


On Tue, Aug 13, 2013 at 5:37 AM, J.D. Grimes <[hidden email]> wrote:

> > I am still learning php, so I apologize for the questions.  Can you
> point me
> > in the right direction to learn how to do this?
>
> Just create the sidebar template like you would have for one of the single
> tags, but instead of hard-coding things, make them dynamic. For example,
> where you would have put the tag name like "My Cool Tag", instead do
> something like this:
>
> <?php echo $tag->name; ?>
>
> You will need to get the tag, the same way that you are before you load
> the template. Or you may not need to get the tag at all until inside of the
> custom sidebar template (that depends on whether you know you are going to
> load that template before you check the post's tags).
>
> If you post an example of what you are trying to achieve, we can help you
> make it dynamic.
>
> - J.D.
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

J.D. Grimes
> Thank you, I can hardly believe how helpful people are here.

You're welcome!

>  The code I pasted above earlier is for a left-sidebar used for gallery images and bootstrap scrollspy to show
> people where they are on specific pages.  Thats why I need it to call based
> on the post tag- so it shows along with the appropriate content.

Sorry, I'm not sure what code you mean. Maybe you could post a link to a Gist (https://gist.github.com/)

> Knowing all that, is it still possible to call the sidebar from one file with conditionals?

Definitely.
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

BenderisGreat
My current code is this:

<?php global $post;
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag);{
$tag_name = $tag->name;}
$end =  $tag_name;
?>

 <?php get_template_part('/templates/left-sidebar', $end); ?>
<?php else: ?>
    <?php endif; ?>
      </aside>


      <aside class="sidebar <?php echo roots_sidebar_class(); ?>"
role="complementary">
      <?php if (roots_display_sidebar()) : ?>
        <?php include roots_sidebar_path(); ?>
      <?php endif; ?>
      </aside><!-- /.sidebar -->


So it looks for left-sidebar-(tagname) first and if that php file doesnt
exist, it reverts to the default left-sidebar.php.   How would I change
this to instead load left-sidebar-tag.php and within that file search for
the correct tag to show, and if it does not exist show the default
left-sidebar.php?

I can easily have it call the left-sidebar-tag.php but once its called I
dont know what to put inside that file to determine what should be loaded.
 Maybe something like

<?php global $post;
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag);{
$tag_name = $tag->name;}
$end =  $tag_name;
?>

if $end == ($posd->ID);
include  //no idea what it would include, since it would be including a
specific portion of the file only...


On Tue, Aug 13, 2013 at 12:09 PM, J.D. Grimes <[hidden email]> wrote:

> > Thank you, I can hardly believe how helpful people are here.
>
> You're welcome!
>
> >  The code I pasted above earlier is for a left-sidebar used for gallery
> images and bootstrap scrollspy to show
> > people where they are on specific pages.  Thats why I need it to call
> based
> > on the post tag- so it shows along with the appropriate content.
>
> Sorry, I'm not sure what code you mean. Maybe you could post a link to a
> Gist (https://gist.github.com/)
>
> > Knowing all that, is it still possible to call the sidebar from one file
> with conditionals?
>
> Definitely.
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

J.D. Grimes
Have you already created the code that displays all the posts for a tag? In other words, have you already written one of the custom sidebars? If so, could you post that code?

_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers
Reply | Threaded
Open this post in threaded view
|

Re: Help with including custom templates using variables

BenderisGreat
Im sorry, this is a wordpress theme so I am not sure how to post it on
github

My theme is Roots, its a bootstrap framework and makes use of a few files
to show the sidebars.
Here is the BASE.PHP which controls which files are loaded for sidebars.

<?php get_template_part('templates/head'); ?>
<body <?php body_class(); ?>>

  <!--[if lt IE 7]><div class="alert"><?php _e('You are using an
<strong>outdated</strong> browser. Please <a
href="http://browsehappy.com/">upgrade
your browser</a> to improve your experience.', 'roots');
?></div><![endif]-->

  <?php
    do_action('get_header');
    // Use Bootstrap's navbar if enabled in config.php
    if (current_theme_supports('bootstrap-top-navbar')) {
      get_template_part('templates/header-top-navbar');
    } else {
      get_template_part('templates/header');
    }
  ?>

  <div class="wrap container" role="document">
         <div class="content row">

         <div class="main <?php echo roots_main_class(); ?> " role="main">
        <?php include roots_template_path(); ?>
      </div><!-- /.main -->

         <aside class="left-sidebar <?php echo roots_left_sidebar_class();
?> " role="complementary">
    <?php if (roots_display_sidebar()) : ?>


    <!-- MY CODE TO PULL CUSTOM .PHP SIDEBAR WITH TAG IN FILENAME -->

<?php global $post;
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag);{
$tag_name = $tag->name;}
$end =  $tag_name;
?>

 <?php get_template_part('/templates/left-sidebar', $end); ?>
<?php else: ?>
    <?php endif; ?>
      </aside>

<!-- END OF MY CODE -->

      <aside class="sidebar <?php echo roots_sidebar_class(); ?>"
role="complementary">
      <?php if (roots_display_sidebar()) : ?>
        <?php include roots_sidebar_path(); ?>
      <?php endif; ?>
      </aside><!-- /.sidebar -->

 </div><!-- /.content -->
  </div><!-- /.wrap -->

  <?php get_template_part('templates/footer'); ?>

</body>
</html>



The actual left sidebar has nothing in it, just some static content I want
to display on specific posts with this tag.   So how would I go about
altering that code?  Should I put this code within left-sidebar.php?



On Wed, Aug 14, 2013 at 3:47 PM, J.D. Grimes <[hidden email]> wrote:

> Have you already created the code that displays all the posts for a tag?
> In other words, have you already written one of the custom sidebars? If so,
> could you post that code?
>
> _______________________________________________
> wp-hackers mailing list
> [hidden email]
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
_______________________________________________
wp-hackers mailing list
[hidden email]
http://lists.automattic.com/mailman/listinfo/wp-hackers