Jump to content

Text in place of URLs


NoDice
 Share

Recommended Posts

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

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

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 = "";

  • Like 1
Link to comment
Share on other sites

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.  :wacko:

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

  • 2 weeks later...

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

.....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. :D Just my opinion :)

  • Like 1
Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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. :D 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

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

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...