Jump to content

Selector problem - output only tag titles


Peter Knight
 Share

Recommended Posts

How would I refine this selector to list only my tags. Tags are simply an array of pages stored within a field called blog_tags?

The following PHP partially works in that it lists all pages based on blog-post template. The problem is it lists outputs the titles of my tags and the blog posts too.

<?php
   $mytags = $pages->find("template=blog-post");//All pages based on blog_post tamplate
   
   foreach($mytags as $taglet){
   echo "Tags:{$taglet->title}";}		
?>      

I've tried editing the selector to target the field with the template

$mytags = $pages->find("template=blog-post, field=blog_tags ");

but my syntax is clearly wrong. I then tried targetting the echo statement and that's wrong too

echo "<strong>Tags:</strong> {$taglet->blog_tags->title}";}
Link to comment
Share on other sites

Is your template blog-post or blog_post? (underscore or hyphen)

And to display all tags the should you not have, for example:

echo "<strong>Tags:</strong><ul>";

foreach($mytags as $taglet){
echo "<li>{$taglet->title}</li>";
}
echo "</ul>";
Link to comment
Share on other sites

blog_tags is also a PageArray. You would have to loop through it as well. Something like below. However, note, the below will produce duplicates since tags are reusable across multiple posts...If you need unique values just holla and we'll sort that out for ya..

$mytags = $pages->find("template=blog-post");//All pages based on blog-post template
foreach($mytags as $taglet){
   
  //blog_tags return a PageArray so we loop through them as well
  //I don't think this is exactly what you want because this will output all tags used, i.e. there will be duplicates
  foreach ($taglet->blog_tags as $t) {
            echo "Tags:{$t->title}";
  }

}
Edited by kongondo
Link to comment
Share on other sites

My template is blog-post. Have edited above.

@Joss That gives me the same result as my original PHP. IE echoes the titles of the tags AND the titles of the pages containing those tags.

Is there another way to do this so I am only outputting tag titles within pages based on that template.

Link to comment
Share on other sites

Thank you Kongondo. Thats exactly what I was looking for and I don't mind the duplicates right now.

You seem to be nesting foreach statements there? Haven't seen that before but I guess it makes sense.

I drastically simplied the first code example for the sake of outputting *something*. 

What I'd like to do is apply your code to my code example. Let me see if I can figure this out.

Time to put kettle on and switch off the phone :)

<?php
echo"<section id=\"cd-timeline\" class=\"cd-container\">";
           
$updates = $pages->find("template=blog-post, blog_tags=Studio Watch");//Get blog posts tagged Studio Watch
foreach ($updates as $update) {
			
echo"<div class=\"cd-timeline-block\">
         <div class=\"cd-timeline-img\">
         <img src=\"img/cd-icon-picture.svg\" alt=\"Picture\">
         </div>
			
         <div class=\"cd-timeline-content\">
         <h2>Title of section 1</h2>
​         <p>{$update->title}</p>
						
						
         <a href=\"#\" class=\"cd-read-more\">Read more</a>
         <span class=\"cd-date\">Jan 14</span>
         </div>
					
      </div>";}

      echo"</div>";
    echo"</section>";
?>
Link to comment
Share on other sites

Btw, Peter, what is your use case? Do you have a page that will list all tags used across your Blog? If that is the case, then you should be doing this a little bit differently...

@Kongondo - Basically I'm using your blog system with some tweaks.

I have *some* posts tagged as "studio watch" which are not so much full blog posts but mini updates consisting of

  • A headline (which is blog title)
  • Project Tags (which are the normal tags which come boxed with your blog Module)
  • Location Tags (very similar to normal tags but a seperate parent folder for the pages created as tags). (Haven't got round to this bit yet)

I'm then using a CSS Timeline I found and tweaking it to make it pull in the above.

Link to comment
Share on other sites

Hmm, this is interesting. Im not sure again if my syntax is correct or nested badly but I'm only getting a single tag from each post and the page is outputting as follows

Title of Post 1

Tags: Tag 1

Title of Post 2

Tags: Tag 1

Title of Post 2 (again)

Tags: Tag 1

 

Title of Post 3 (again)

Tags: Tag 1

Title of Post 3 (again)

Tags: Tag 1

Title of Post 3 (again)

Tags: Tag 1

            <?php
			$updates = $pages->find("template=blog-post, blog_tags=Studio Watch");//Get blog posts tagged Studio Watch
			
			echo"<section id=\"cd-timeline\" class=\"cd-container\">";
			
				foreach ($updates as $update)
				{
					foreach ($update->blog_tags as $project_tag) 
						{
						echo"<div class=\"cd-timeline-block\">
						
								<div class=\"cd-timeline-img\">
									<img src=\"img/cd-icon-picture.svg\" alt=\"Picture\">
								</div>
						
								<div class=\"cd-timeline-content\">
								
									<p>{$update->title}</p>
									
									Tags:{$project_tag->title}
								
									<span class=\"cd-date\">Jan 14</span>
								</div>
								
							</div>";
						  }
				}	
							
			echo"</section>";
			?>
Link to comment
Share on other sites

Ok, closer to completion.

I'm now not getting any duplicates after adjusting the nesting.

Still only getting 1 tag on the Project Tag and Location Tag even though there should be two or three.

<?php
$updates = $pages->find("template=blog-post, blog_tags=Studio Watch");//Get blog posts tagged Studio Watch
			
echo"<section id=\"cd-timeline\" class=\"cd-container\">";

foreach ($updates as $update)
foreach ($update->client_location as $location_tag) ;
foreach ($update->blog_tags as $project_tag) 

{
echo"<div class=\"cd-timeline-block\">
						
        <div class=\"cd-timeline-img\">
        <img src=\"img/cd-icon-picture.svg\" alt=\"Picture\">
        </div>
	
        <div class=\"cd-timeline-content\">
        <p>{$update->title}</p>
        <strong>Tags:</strong>{$project_tag->title}
        <strong>Location:</strong>{$location_tag->title}

        <span class=\"cd-date\">Jan 14</span>
        </div>
     </div>";
}
											
echo"</section>";
?>
Link to comment
Share on other sites

Ok, finally fixed it.

I realised the second foreach loop needed to be way down the code. Once I figured out my list of brackets, semi-colons, quotation marks etc etc it worked  :rolleyes:

Here's my code

<?php
$updates = $pages->find("template=blog-post, blog_tags=Studio Watch");//Get blog posts tagged Studio Watch
			
echo"<section id=\"cd-timeline\" class=\"cd-container\">";
			
foreach ($updates as $update)
{
				
echo"<div class=\"cd-timeline-block\">
						
<div class=\"cd-timeline-img\">
<img src=\"img/cd-icon-picture.svg\" alt=\"Picture\">
</div>

<div class=\"cd-timeline-content\">
<p>{$update->title}</p>";{
									
echo "<i class=\"fa fa-tags\"></i>";
foreach ($update->blog_tags as $t) 
    {
    echo "{$t->title}";
    }
								
								
echo"<span class=\"cd-date\">Jan 14</span></div></div>"
;}	
}	
					
echo"</section>";
?>
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...