Jump to content

Inject page titles into DIV


Peter Knight
 Share

Recommended Posts

Hi

I'm trying to inject a list of tags (page titles) into a DIV so that this:

<div class ="article-wrapper">

becomes

<div class ="article-wrapper tag01 tag 02 tag 03">

It seems very simple but it's failing to output and not throwing any warnings.

It's the variable on line 4 called audience that should be scooping up my articles_tag_audience page titles

<?php 
$articles = $pages->find("template=articles-post"); //find all posts based on this template
foreach ($articles as $article){ // these are this
$audience = $article->articles_tag_audience; 

echo "
<div class='small-12 medium-6 large-4 columns masonary-brick'>
<div class='article-wrapper {$audience->title} '>

etc...

I've also tried

<div class='article-wrapper . {$audience->title} .'>

which has worked on occasion.

Oddly, the following is used (and works) further into my code block

{
echo $audience->implode(", ", "<a href='{url}'>{title}</a>");
}

so I know the page titles exist.

Link to comment
Share on other sites

If $audience is an array of pages then you cannot get the "title"(sic) from it directly. Use the implode function to get a string with all titles. Also you should rather take the names or use some other function to sanitize titles to valid class names.

  • Like 2
Link to comment
Share on other sites

If $audience is an array of pages then you cannot get the "title"(sic) from it directly. Use the implode function to get a string with all titles. Also you should rather take the names or use some other function to sanitize titles to valid class names.

Thanks - will try.

At the moment I don't need to (don't know how to) sanitize page titles because they'll always be 1 word titles determined by me. But I like the idea.

Link to comment
Share on other sites

Yippeee. I did it :)

See last line of this code.

<?php 
$articles = $pages->find("template=articles-post"); //find all posts based on this template
foreach ($articles as $article){ // these are this
$audience = $article->articles_tag_audience; 
 
echo "
<div class='small-12 medium-6 large-4 columns masonary-brick'>
<div class='article-wrapper {$audience->implode(" ", "title")}'>
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...