Jump to content

How should I write this code - SOLVED


OllieMackJames
 Share

Recommended Posts

OK, thanks all, I finally got it working, this is how I managed to do it:

/* fill in pageid of page to be used for content homepage*/
if($page->id == 1 && $page->page2use4homepage) {
	$p = $page->page2use4homepage->id; 
    $page = $pages($p); }

and then @Robin S showed me that this could do the trick just as well, with less code:

if($page->id == 1 && $page->page2use4homepage) {
    $page = $page->page2use4homepage;
}

Thanks all.

FYI, here is the original question

I have a question how to turn the pseudocode below into something that works in PW

I have made a new field called page2use4homepage and added it to the homepage template, which shows this:

<?php

include("./basic-page.php"); 

Now to this basic page template I want to add code that does the following:

if pageid = homepage 
-> if exist content of field named page2use4homepage
		$page = $pages(field->page2use4homepageID);
else just carry on and forget about this piece of code}

How would I write the above in PW code?

Thanks

Link to comment
Share on other sites

@louisstephens thanks for that, I tried this:

if( $page->id == 1 ) {
		$page = $pages(1213);}

And that works, but I want to take it one step further, on my homepage template I now have a site settings repeater field.

One of these site variables is called page2use4homepage

My instructions say if I want to access that in a function I need to do this:

echo wire('site')->page2use4homepage;

I now need to combine them that would mean:

if( $page->id == 1 ) {
		$page = $pages(echo wire('site')->page2use4homepage);}

But that gives me a server error. So I must be doing something wrong here.

Thanks!

Link to comment
Share on other sites

@louisstephens thanks but that does not work.

I must not have explained myself well enough, apologies.

if( $page->id == 1 ) { /* this piece of code must only work if the current page is the homepage - CHECKED this works*/

-> if exist content of repeaterfield named page2use4homepage /*if this field is not filled, then forget about the code and just move on with the rest of the page* - this code is not there yet/

		$page = $pages(repeaterfield->page2use4homepageID);/*this will tell PW to pull the contents for $page from $pages(xxxx), where xxxx is stored in the repeaterfield called page2use4homepage, so the question is how to get this value within this function? - my instructions say to use: echo wire('site')->page2use4homepage); but I don't know how to place that in the code so it works. That  is where I got the server error*/

else just carry on and forget about this piece of code}

What I need is the following code to be the result:

if( $page->id == 1 ) {
		$page = $pages(xxxx);}

Where xxxx is the id of the page to be used to pull content to be used for the homepage. What this code does is tell PW that if this is the homepage to pull the contents for the page from page with id xxxx.

On the homepage I have a repeater field called sites, with one repeaterfield called page2use4homepage, the content for this field = xxxx and can be called upon within a template with the following code:

echo $site->page2use4homepage;

and within a function with the code:

echo wire('site')->page2use4homepage;

so the question is how to write code does this:

1 - check if the page is the homepage

2 - check if the repeaterfield page2use4homepage has a value

3 - if that repeaterfield has a value, get it and input that value in the following line in the place of xxxx ==> $page = $pages(xxxx);}

Hope this makes sense.

Thanks

Link to comment
Share on other sites

Is the "page2use4homepage" field a repeater field? can you show us a screenshot of the repeater configuration (and template config)?

Quote

2 - check if the repeaterfield page2use4homepage has a value

That doesn't make sense, because a repeater has "no values", only the fields inside of the repeater ;) 

Link to comment
Share on other sites

@zoeck thanks for thinking along.

What I have done now is forget about the repeater field and just made a pagereference field called page2use4homepage

I can call this with the following code which I checked that works:

<?php echo $page->page2use4homepage->id; ?>

What I now need is to take $page->page2use4homepage->id and use that, but that does not work either

if( $page->id == 1 ) {
		$page = $pages({$page->page2use4homepage->id});
}

thanks!

Link to comment
Share on other sites

I tried this as well and no luck either:

 

if( $page->id == 1 ) {
		$page2use = $page->page2use4homepage->id;
		$page = $pages('$page2use');

 

I also tried:

if( $page->id == 1 ) {
		$page2use = $page->page2use4homepage->id;
		$page = $pages({$page2use});

and also this without luck:

if( $page->id == 1 ) {
		$page2use = $page->page2use4homepage->id;
		$page = $pages({echo $page2use});

 

Link to comment
Share on other sites

i think you are looking for this:

if( $page->id == 1 ) {
 $yourpage = $pages->get("id=".$sanitizer->int($page->pageselect->id));
 echo $yourpage->title;
}

It's a bad idea to replace/overwrite the existing "$page" variable like in your code!

Info:

$sanitizer->int

This is only for security reasons ;)  i'm not sure if we need it here... but it works
https://processwire.com/api/variables/sanitizer/

Link to comment
Share on other sites

Hi @OllieMackJames,

This is a basic example because I don't know what want to do with the contents of page2use4homepage.

$content = '';
// if page2use4homepage is a multi page field
if($page->id == 1 && count($page->page2use4homepage)) {
    foreach ($page->page2use4homepage as $p) {
        $content .= $p->title;// @note: $p is a page; you can access all its properties
    }
}

/* // if page2use4homepage is a single page field
if($page->id == 1 && $page->page2use4homepage) {
    $content .= $page->page2use4homepage;
	// alternatively, you could create a property on the fly and assign to $page, e.g. (a silly example) $page->content = $page->page2use4homepage->id;
}
*/

else $content = $page->body;

// do something with your $content

 

3 hours ago, OllieMackJames said:

$page = $pages({$page->page2use4homepage->id});

You don't want to do this. You are overwriting $page which is a native ProcessWire variable. Same goes for $pages, $template, etc. You want to use different variable names (in general)

4 hours ago, OllieMackJames said:

wow coding is not easy

I highly suggest you go through some basic PHP tutorials. It might seem hard at first but the benefits thereafter will be worth it. Otherwise, copying and pasting code will only get you so far. Please don't take offence; just friendly advice :).

Link to comment
Share on other sites

@kongondo thanks for your thoughts and no offence taken - the opposite I value your and anybodies input and just realize I am not good at explaining what I want to do.

For reasons mentioned in

I DEFINITELY do want to use this code and am using it as below now, but hardcoded, so each time I want to change it I change the code in the template, but I want to do that without touching the template itself:

	/* fill in pageid of page to be used for content homepage*/
if( $page->id == 1 ) {	
		$page = $pages(1213);}

So that is why I want to specify the pageID, in this case 1213 on the homepage admin side via the field page2use4homepage.

And I want the double if statement like mentioned in the first post. Let me try and explain it again:

If this page is the homepage AND if the field page2use4homepage is filled THEN

$page = $pages(get value from homepage-field-page2use4homepage and insert here)

Hope that makes sense. I am aware this might go against what others consider done or not done, but hey, this is what I want to do so if anyone can tell me how to get the content of that field on the homepage into the code, great!

So if I can invite anyone to just focus your coding smarts on how to make this happen, that would be great!

Link to comment
Share on other sites

  • OllieMackJames changed the title to How should I write this code - SOLVED
3 hours ago, zoeck said:

It's a bad idea to replace/overwrite the existing "$page" variable like in your code!

 

2 hours ago, kongondo said:

 

6 hours ago, OllieMackJames said:

$page = $pages({$page->page2use4homepage->id});

You don't want to do this. You are overwriting $page which is a native ProcessWire variable. Same goes for $pages, $template, etc. You want to use different variable names (in general)

 

I think this is not good when you use the $page Variable here...

But why you dont use "$mypage" for example? you only have to use "$mypage" instead of "$page" inside of your Template... 

Link to comment
Share on other sites

1 hour ago, zoeck said:

But why you dont use "$mypage" for example? you only have to use "$mypage" instead of "$page" inside of your Template... 

@zoeck Thanks and how would I get what I want with $mypage code then?

I believe when I use $mypage then I would need to change all templates to use $mypage in stead of $page and this way I can keep everything as it is, and just when I need it, I can swap the homepage, while even keeping the actual content on the page with id=1 unchanged. Maybe I want to check conversions to see which page converts better.

I built my sites to generate action and my designers tell me that my pages are way too long, but hey I tested short and long and the longer pages until now ALWAYS outperformed the shorter pages. I tested design and found that design does not make much difference at all. It is the text and the flow of the text that makes all the difference. I tested video and found that text pages still outperform my videosalespages, but videos strategically placed elsewhere have doubled my conversions on my salespage.

So the code below does just what I need, spot on, what can be wrong about that?

/* fill in pageid of page to be used for content homepage*/
if($page->id == 1 && $page->page2use4homepage) {
	$p = $page->page2use4homepage->id; 
    $page = $pages($p); }

When I don't want to use the contents of the homepage, I just take the contents of another page. And whether that is good or not all depends on what one wants to do and to me this is the exquisite beauty of Processwire. It totally allows me to do what I feel is needed at any moment, even if that goes against the grain of what others consider not good.

Like I said in my other post, there are definite reasons why I want to do this, it allows for using a homepage on a live site, then changing the underlying page that is used to pull content into the homepage.

And the code above works like a charm!

And what I also like about processwire is this wonderful forum, where people and help each other, so @zoeck@kongondo, @louisstephens big thanks for taking out of your time and helping me, much appreciated.

 

Link to comment
Share on other sites

@OllieMackJames, just a note about Page Reference fields:

Where you have a "single" Page Reference field...

2018-02-15_094957.png.61585d69f51ac7b941946abe9c354fd5.png

...when that field has a page selected its value is a Page object. So rather than this...

/* fill in pageid of page to be used for content homepage*/
if($page->id == 1 && $page->page2use4homepage) {
    $p = $page->page2use4homepage->id; 
    $page = $pages($p);
}

...you can just do this...

if($page->id == 1 && $page->page2use4homepage) {
    $page = $page->page2use4homepage;
}

 

  • Thanks 1
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...