NoDice Posted August 2, 2013 Author Share Posted August 2, 2013 Ok, as I thought, this code is the problem $thisCategory = wire("page")->name; if($thisCategory !="news") { $category = "news_categories_01.name=" . $thisCategory; } When you click a category in the list, the page is not called "news" anymore, so the $category variable is not set. That's why it's missing. You have to adapt the code so that the categories pages are also included on that if check. -- On your second problem: wire("page") holds the current page being viewed, so when you use it on the homepage, that's the page that will be called by it, and not "news" as you want. To correct this, you will have to change wire("page") by wire("pages")->get(1234), where 1234 would be the id of the "news page". That will work everywhere. I'm not sure why you are using wire() in this code, but in case this is not a conscient decision, just wanted to clarify that wire('page') is the same as $page, and wire('pages') is the same as $pages. Thanks so much again diogo! Yea, as I wrote, I figured out that it is not being defined for the News page, the problem is that I have not been able to define it with an else statement (for when the page is the news page) or such to make the error go away. So the question would be how to do that? As for the wire part - this is all adapted from this wiki: http://wiki.processwire.com/index.php/Simple_News_System So not sure why wire was used (or why the part about not being the news page was defined the way it is), but I will try to change it! Link to comment Share on other sites More sharing options...
kongondo Posted August 2, 2013 Share Posted August 2, 2013 In a function or module use wire. Otherwise use $page or $pages 1 Link to comment Share on other sites More sharing options...
NoDice Posted August 2, 2013 Author Share Posted August 2, 2013 In a function or module use wire. Otherwise use $page or $pages Yea, so that explains why wire was used i guess (it was done as a function in the wiki). I will try to fiddle around and see if I can get it to display on another page. How to solve the error part, I don't get though. ***edit*** Got the display of the "breaking news" working on the home page with the help of your info (once I understood the wire part). Thanks diogo and kongondo! Missing variable error persist though. Link to comment Share on other sites More sharing options...
diogo Posted August 2, 2013 Share Posted August 2, 2013 I looked a bit better at the whole code (i confess I didn't before...). The wire() method is needed because the code is inside a function (variables, like $page, don't work inside functions but methods, like wire() do http://php.net/manual/en/language.variables.scope.php). So keep it as it is. I don't have time to look at the tutorial and understand the problem with $category. What you can do for now to get rid of the error, is define the variable before that if check: $category = ""; 1 Link to comment Share on other sites More sharing options...
NoDice Posted August 2, 2013 Author Share Posted August 2, 2013 You already helped way too much diogo! Also - this last bit worked in all its simplicity, don't know why I didn't think of that with all my attempts with else statements and such. Some people have followed the wiki so I think the problem could perhaps stem from the adaptations I have made to get multiple category tags working (with the help of Ryan). In the original wiki they used a single page category field. Anyway, the error it gone for now and everything works the way I want as far as the markup produced thanks to your help! Link to comment Share on other sites More sharing options...
NoDice Posted August 12, 2013 Author Share Posted August 12, 2013 OK, I have another noob-question (and I will stick to my thread here as I don't want to spam the forum with new topics, if new topics are preferred, please let me know). I figured I'd ask you php-wizards what I am doing wrong here as I'm trying to figure out why this does not work: function categoriesList(){ echo "<div class='categories'>"; echo "<div class='list_heading'><p>" . wire("pages")->get('/news/')->categories_heading_01 . "</p></div>"; $categories = wire("pages")->find("parent=/news-categories/, template=news_index_01, sort=title"); $out =" "; $thisCategory = wire("page")->title; foreach($categories as $category){ $category_active = $category->title; if ($category_active = $thisCategory) { $out .="<li class='active_category'><a href='{$category->url}'>{$category->title}</a></li>"; } else { $out .="<li><a href='{$category->url}'>{$category->title}</a></li>"; } } echo "<ul>$out</ul>"; echo "</div>"; } It is the "if"-statement that is the problem as it returns true for all instances. What I want to do is assign a class to the active category (which is the one that is the same as the page). I figure somehow it looks at all the instances when answering, but since "$category->title" correctly outputs all the different categories in the foreach-part, I am little confused by that. Link to comment Share on other sites More sharing options...
SteveB Posted August 12, 2013 Share Posted August 12, 2013 The "equal" you use to assign a value is "=" the one you use to compare values is "==" so what you meant to do was... if ($category_active == $thisCategory){ 1 Link to comment Share on other sites More sharing options...
NoDice Posted August 12, 2013 Author Share Posted August 12, 2013 Thanks ever so much, works great now! And truly embarrassing of course, as even I have read about people making that mistake many times before in the few weeks I have tried to learn a little about php... Link to comment Share on other sites More sharing options...
kongondo Posted August 12, 2013 Share Posted August 12, 2013 .....and I will stick to my thread here as I don't want to spam the forum with new topics, if new topics are preferred, please let me know). I think if it is a new topic, you start a new topic unless it's closely related to the current topic. Another reason to starting a new topic, this one already had best answer. For those quickly scanning the forum for unanswered topics, the "new question" might be missed. Just my opinion 1 Link to comment Share on other sites More sharing options...
Soma Posted August 12, 2013 Share Posted August 12, 2013 It's a common error in coding and has nothing to do with PHP. I would write the loop and current class like this: foreach($categories as $category) { $class = $category === wire("page") ? ' class="current"' : ''; $out .="<li$class><a href='{$category->url}'>{$category->title}</a></li>"; } === is comparing the objects if they're the same in memory, since $category at some point IS the current $page it works. You could also write: foreach($categories as $category) { $class = $category->id == wire("page")->id ? ' class="current"' : ''; $out .="<li$class><a href='{$category->url}'>{$category->title}</a></li>"; } No need to compare titles as they theoretically could be same for another page. So the id is just as good and better. 2 Link to comment Share on other sites More sharing options...
NoDice Posted August 13, 2013 Author Share Posted August 13, 2013 I think if it is a new topic, you start a new topic unless it's closely related to the current topic. Another reason to starting a new topic, this one already had best answer. For those quickly scanning the forum for unanswered topics, the "new question" might be missed. Just my opinion OK, I just thought this was not really pw related in a way that could help others but more just a basic php-question. I will still start new ones from now on if that is preferred. Thanks for the response on that. Link to comment Share on other sites More sharing options...
NoDice Posted August 13, 2013 Author Share Posted August 13, 2013 Soma, beautiful, and I now integrated the latter version giving the exact same effect on my page as the messier code I had come up with. And yea, I am sure the mistake is common to other languages, but php is the first coding I am dealing with (except a brief stint with static HTML and CSS when I helped a friend build a simple site some 10 years ago). Things are progressing at a better pace now though, so I may share the project if I get it finished in a month or so (my real job to attend to as well I am afraid). Really appreciate the quick help from the forum as usual! Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now