CodeIgniter title won't work - php

I understand an approach to naming declaring a title for a page in CI can be done like this, which has in the past and if anything within an existing project works.
$data['title'] = "myTitle";
$this->load->view("content_home", $data);
But for the life of me, now it just will not work!! :(
Anybody any ideas as to why?

In order to declare a title to a page, you type this in the controller relevant to a page example:
public function home(){
$data["title"] = "my Title";
$this->load->view("content_home", $data);
}
Within the view in order for this to work this must be inserted:
<title><?php echo $title; ?></title>
I had not realised, but this worked for me now after I went back and checked my files properly in my previous project, sorry guys and thanks for the responses.

just fyi you can check if title has been set and if not show something else
<title><?php
// check if $title has been set
if( isset($title) ){ echo $title; }
// else display generic title
else { echo "A Website Title" ; }
?></title>
that way you will always be covered

Related

Simple Shortcode (Wordpress) does not work

This is my "code":
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
Now when I want to use it in a post, all I get is:
[whoop]
As you can see I am very new and unused to it so maybe the answer is really simple, maybe I've just miss a thing in advance.
I both checked defining the function in functions.php and also in content.php
For shortcode function echo is required.
Please check with below code
function whoop_function(){
$responseData = "whoop whoop!";
echo $responseData;
return true;
}
add_shortcode('whoop', 'whoop_function' );
put your code in themes/function.php files and remove from content.php as function is duplicated so function already defined PHP error occurred.
function whoop_function(){
return "whoop whoop!";
}
add_shortcode('whoop', 'whoop_function' );
and add your shortcode [whoop] in any page content section.
if u use do_shortcode('[whoop]'); then echo it like below.
<?php echo do_shortcode('[whoop]'); ?>
Your approach is correct. I think you are using it in a template. Just need to use it as mentioned below :
In files:
<?php echo do_shortcode('[whoop]'); ?>
In admin pages or posts:
[whoop]
Fetch the content like this:
$post_content = get_post(get_id_by_slug('short')); // assuming that you have already defined get_id_by_slug function as you are using it in your code and "short" is a slug for post you are fetching content
$content = $post_content->post_content;
echo do_shortcode( $content );
You are fetching content without considering the short code. Update your code as above.
Hope it will work for you.

Variable auto_prepend_file?

Okay, I suck at titling my questions XD If anyone has a better title, please edit!
Anyway, I have this in my php.ini file:
auto_prepend_file = ./include/startup.php
auto_append_file = ./include/shutdown.php
startup.php looks something like this:
<?php
chdir(__DIR__."/..");
require("include/db.php");
// more setup stuff here
if( $_SERVER['REQUEST_METHOD'] == "AJAX") { // yup, custom HTTP method :p
// parse file_get_contents("php://input")
}
else {
$output_footer = true;
require("include/header.php");
}
shutdown.php goes something like this:
<?php
if( isset($output_footer)) require("include/footer.php");
Now, on to the main problem.
header.php includes the line <title>My shiny new site</title>. I want that title to depend on the page that is currently being viewed. At the moment, I have some ugly hack that involves JavaScript:
document.title = document.getElementsByTagName('h1')[0].textContent;
Obviously, this is not ideal! Any suggestions on how to adjust the title when said title is being output as a result of an auto_prepend_file?
I don't think you should use the append and prepend functionality like that But that's just my opinion.
Why not create header.php to allow for a variable title text. And drop the prepend and append scripts completely
<title><?php echo (isset($title)) ? $title : 'default title'; ?></title>
And whenever you need a title tag.
$title = "some title";
require("include/header.php");

Setting custom page titles for every pages

Currently my site title looks like:
<title>My Site Title</title>
The above code is added on 'header.php' file, so every pages has the same page title.
I need to set different titles for different pages.
for example,
<title>
if 'contact.php' then title= 'Contact Us'
else if 'faq.php' then title= 'FAQ'
else if 'add.php' then title= 'Add'
else title= 'My Site Title'
</title>
somebody please help me!!
I guess contact.php include 'header.php';. Then something like this would work:
contact.php:
<?php
$title = 'Contact Us';
include 'header.php';
// your code
?>
header.php:
<?php
echo '<title>'.$title.'</title>';
Tip: have a look at template engines. I like smarty for example. Maybe someone will comment on this with some other examples ;)
Make a variable in your script called $page and use that variable in the template file.
Business logic for page Home, for example:
<?php
.
.
.
$page = 'Home';
render($page);
View logic page for Home:
.
.
.
<title>
<?php echo $page; ?>
</title>
.
.
.
This is just a concept, it is not a fully functional code.
Split your header in to 2 seperate php files, one before the title, and one after the title (this will work with other page specific data, see note at end of answer)
then the top of your pages should look like:
<?php include_once("inc/begin-head.inc");?>
<title>My Title</title>
<meta name="description" content="description"></meta>
<?php include_once("inc/end-head.inc");?>
There are some other solutions, such as make header a class and define variations to it, and then call a function to output the head completly
Please note, there are a LOT of other paged specific tags. Title, Meta Description, Canonical url link, meta keywords, open graph data .....
You can try like this and use basename($_SERVER['PHP_SELF']) and now lookup for the $title[key]
$title = array();
$title['home.php'] = 'My home page';
$title['Hello.php'] = 'My title';
I'd advice you to use an array with titles instead of a series of ifs (respectively a switch)
<?php
$file = basename($_SERVER['PHP_SELF']);
$titles = array(
'contact.php' => 'Contact Us',
'faq.php' => 'FAQ',
'add.php' => 'Add',
);
if(array_key_exists($file, $titles){
echo '<title>'.$titles[$file].'</title>';
}else{
echo '<title>Ny Site Title</title>';
}
?>
To add title dynamically , first set the following code in header.php file :
<title>
<?php
if(isset($title) && !empty($title)) {
echo $title;
} else {
echo "Default title tag";
}
?>
</title>
and set title in each page before including header as :
$title = "Your Title";
include 'header.php';
How I did this for anyone curious in the future...
I have a "pagetitles.php" page that contains this code:
$page_files=array(
"admin"=>"Admin Panel",
"profile"=>"Your Profile",
"billing"=>"Billing / Subscriptions",
"pricing"=>"Our Pricing",
"settings"=>"Your Settings",
"bugs"=>"Bug/Feature Tracker",
"search"=>"Search Results",
"clients"=>"My Clients");
if(isset($_GET['rq'])){
if(in_array($_GET['rq'],array_keys($page_files))) {
$pagetitle = $page_files[$_GET['rq']];
}
}
Then I include that file at the very top of my index.php page, and echo $pagetitle where I want the title to be. BUT this also requires another file to handle serving the specific pages, using a ?rq request
In my "page_dir.php" file, I have the following that handles ?rq= pages (ex: www.example.com?rq=home will load the home page, with the above page title that's inside of "home" array)
Here's the page_dir.php file:
$page_files=array(
"noaccess"=>"pages/noaccess.php",
"home"=>"pages/dashboard/home.php",
"lists"=>"pages/dashboard/lists.php"
);
if(isset($_GET['rq'])){
if(in_array($_GET['rq'],array_keys($page_files))) {
include $page_files[$_GET['rq']];
}else{
include $page_files['home'];
}}else{
include $page_files['home'];
}
This page_dir.php file, I put on the index page where I want main content to show up at... I then have each individual page with just the content (like home.php file is just home.php content without the navbar and footer)
On my index.php file, where I want the page title, I have this code:
if(isset($code_nav_title)){
echo $code_nav_title;
}elseif(isset($pagetitle)){
echo $pagetitle;
}else{
echo "Default Page Title Here";
}
the $code_nav_title lets me set the page title from form submissions if I want it to say "success" or "failed" :) the "default page title here" lets you set something to automatically show up if everything fails to show (like if you forgot to set the page title)
Hopefully this makes sense! It's saved me sooo many headaches and makes it easy for expansion/changes!

Change title in browser tab

I'm trying to change the title in browser tab for a child theme of twentytwelve. I wan't it to print out the same title on every page. What the heck is happening?
<?php
function im_awesome_title($title){
$title = "Im awesome!";
return $title;
}
add_filter( 'wp_title', 'im_awesome_title', 20 );
or some like this. script better put in the end of all php tags
<?php
//set title
echo "<script>setTimeout(function(){var tts = document.getElementsByTagName(\"title\");if(tts.length > 0)tts[0].innerHTML=\"My title\"; else {var tt0 = document.createElement(\"TITLE\"); tt0.innerHTML = \"My title\"; document.head.appendChild(tt0);}}, 200);</script>";
?>
Your im_awesome_title($title) function will always return the value of $title when invoked. To print this title on every page, you will need to call the function in the title meta tag of your pages. So it will look like this:
<head>
<title><?php echo im_awesome_title($title); ?></title>
</head>
Of course your function expects an argument, so if you want the same title on every page, it's best to leave out the argument and simply set your $title variable to whatever page title that you want to use on all your pages.
Hope this helps.

Get slug and write content with dashes

i'm having a problem with getting content from pages where the slug is seperated with dashes. for example if i go to www.example.com/home it works fine but if i go for example to www.example.com/about-us it gets an error Trying to get property of non-object wich as the error says is regarding this lines;
<h1><?php echo $page->Title; ?></h1>
<?php echo $page->Content; ?>
I have a function that get's the slug from the database that looks like this;
function getSlug($name = null) {
global $db;
if ($name) {
$query = $db->prepare('SELECT * FROM pages WHERE `Title` = ? LIMIT 1');
$query->execute(array($name));
return $query->fetchObject();
}
}
I also have a function that creates the slug that looks like this;
function createSlug($string){
$string = preg_replace( '/[«»""!?,.!#£$%^&*{};:()]+/', '', $string );
$string = strtolower($string);
$slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
return $slug;
}
In my htaccess i have got the rule RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?name=$1 [L].
to get the content i used the following code;
<?php
include 'header.php';
$slug = create_slug($_GET['name']);
$page = getSlug($slug);
?>
<div class="container">
<h1><?php echo $page->Title; ?></h1>
<?php echo $page->Content; ?>
<?php include 'footer.php'; ?>
I really don't have any clue what the problem is. Any help would be much appreciated.
Thanks in advance. Kind regards
Check ModRewrite module enabled and also, in .htaccess check the below line is added in top.
RewriteEngine On
I have solved the problem by adding a row in my databse called Slug, i think this will also be better if i want the slug to be different then the title.
Thanks for the efforts #sk8terboi87

Categories