For example in Wordpress you can call to home with:
<?php echo home_url(); ?>
But in OpenCart I can't find a similar function. In my header this works:
<?php echo $base; ?>
But not on other templates in my theme. Someone who got a global function for this in OpenCart? Or a library to share, would be perfect!
I work with OpenCart 2.0
Try this,
<?php echo HTTP_SERVER; ?>
In the controller of the page, in my case footer.php I pasted this:
if ($this->request->server['HTTPS']) {
$server = $this->config->get('config_ssl');
} else {
$server = $this->config->get('config_url');
}
$data['base'] = $server;
And then in my template, footer.tpl I could use:
<?php echo $base; ?>
The correct way to do this in OpenCart is to use
<?php echo $this->url->link('common/home'); ?>
Note that this adds the full URL and route, not just / which is not possible using the SEO URL class without modification
<?php echo $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'); ?>
Copy from controller/common/home/header.php
$this->load->language('common/header');
$data['text_home'] = $this->language->get('text_home');
and
$data['home'] = $this->url->link('common/home');
Add these to whichever controller needs them
Copy from header.tpl
href="<?php echo $home; ?>"
add to whichever template needs it.
Would that not cover SEO and link needs?
Related
I am new to PHP and want to create a link with parameters.
So I used this :
<li>
<!-- $GLOBALS["ROOT_PATH"] is where my index.php file located -->
<a href="<?php echo $GLOBALS["ROOT_PATH"]."/Views/pages/profile.php"; ?>">
Profile
</a>
</li>
but when I click on the page it doesn't send me to the page or do anything. When I look at the URL it's something like file:///C:/bla/bla/bla/Views/pages/profile.php
Edit:
Basically, I want to use this in my header.php but my files are :
index.php
Views/pages/profile.php
and so on.
When I use the relative path for the header the path changes for these pages. How can I solve this?
So I solved it using some tricky way but it works :
function createLink($url,$text,$class){
echo "<li>";
// Gets the current php file name.
$basename = substr(strtolower(basename($_SERVER['PHP_SELF'])),0,strlen(basename($_SERVER['PHP_SELF']))-4);
if($basename !== "index"){
$url = "../../".$url;
}
echo '<a href="'. $url . '">';
echo '<span class="'.$class.'"></span>';
echo ' '.$text.'';
echo "</a>";
echo "</li>";
}
So it just add the relative path if the file name is not index.php.
I have to make some changes on my old website where I'm not using any templating system. I'm loading the content for some pages from a database based on ?page parameter. So I have something like this:
<title>Page title</title>
...
...
...
$page_id = $_GET['page'];
include 'page.php'; //escaping is done in this file
Inside the page.php file I'm actually loading the information about the page. Based on this information I have to change the title of the main page.
I know that this design is not good at all and I wouldn't do this way these days, but to change everything on this website would be too complicated.
Thank you for your ideas.
Try to add php code before title
<html>
<?php
$page_id=$_GET["page"];
include('page.php');
echo "<title>".$page_title."</title>";
?>
<body></body></html>
Inside page.php:
echo '<script>
document.title = "This is the new page title.";
</script>';
How to dynamically change a web page's title?
Enjoy !
<?php
$page_id = $_GET['page'];
if ($page_id == 'first value') {
$title = 'first title';
} else {
$title = 'second title';
}
?>
<title><?php echo $title?></title>
...<?php
include 'page.php'; //escaping is done in this file
I had set and get the current page URL in a variable using session.
$currentURL = Mage::helper('core/url')->getCurrentUrl();
$currentPageSplit = explode('?___',$currentURL);
$currentPageURL = $currentPageSplit[0];
Mage::getSingleton('core/session')->setCurrentPage($currentPageURL);
session_start();
$curr = Mage::getSingleton('core/session')->getCurrentPage();
Now that I want this variable $curr to be appended to another URL used for switching from one page to another.
<a class="desktop" href="<?php echo $curr.'/?switch-view=desktop';?>">View desktop version</a>
I am unable to do this. Can someone tell if this syntax is right or what else could be done.
Thanks in adv
yes possible.
<?php
$sample=$_SESSION['alpha'];
echo 'click';
?> `
Try this.
To use the url in same page where you declare your session variable
<?php
$view = 'switch-view=desktop';
$_SESSION['view'] = $view;
$curr = $_SESSION['view'];
?>
<a class="desktop" href="<?php echo $curr;?>">View desktop version</a>
If you need to use the session in another page
<?php
session_start();
$curr = $_SESSION['view'];
?>
<a class="desktop" href="<?php echo $curr;?>">View desktop version</a>
Ok so I am making a website on each page where I want to include a file sidebar.php .
In sidebar.php I would like to echo the name of file that included sidebar.php .
Below are contents of sidebar.php, they return 'sidebar'.
<?php
$file = basename(__FILE__, '.php');
echo $file;
;?>
I found a similar question but the whole point is that I don't have to make no variables in on each page.
Excuse me for vague use of word 'include', I am using the it as the statement in php.
You could pass it to the sidebar page through a session variable:
<?php
session_start();
$_SESSION['filename'] = "thisFilesName.php";
include('../sidebar.php');
?>
sidebar.php:
<?php
session_start();
echo $_SESSION['filename'];
?>
I want to use the following code in my wordpress. How can I do that?
<?php
$theme_name = get_template();
$directory = "wp-content/themes/" . $theme_name . "/pdf/ECE340/";
$pdfs= glob($directory . "*.pdf");
foreach($pdfs as $pdf)
{
$link= substr($pdf,48,90);
?>
<?php echo $link; ?>
<?php
echo "\n\n";
echo "<br />";
}
?>
Below is the answer (taken from here):
add_filter('widget_text','execute_php',100);
function execute_php($html){
if(strpos($html,"<"."?php")!==false){
ob_start();
eval("?".">".$html);
$html=ob_get_contents();
ob_end_clean();
}
return $html;
}
Copy the above function at the end of your functions.php file.
But the best soluttion will be to create a custom template instead of using PHP in pages/widgets/posts. You will find more about wordpress custom templates here.
Login to your admin panel. Click on Appearance -> Editor from left side menu. Then find the Template where you want to add the PHP code from right side menu. Then you can add your PHP code in the editor.
NOTE: Make sure do backup before you do any editing. I'm not responsible for your mistakes.