I have a template which uses a php include file $content to display the main contents of the page. I want to have the page title change depending on the content. I can declare $pagetitle in the included $content php file but the problem is that in the layout the content is loaded after the pagetitle.
I don't want to have to set $pagetitle from my code every time I load a page, I'd rather have it in the relevant content file so it sets automatically every time I include the page. How can I do this?
<div class="container">
<main class="content">
<div id="ctopspace"><h2><?php echo $pagetitle;></h2></div>
<div id="cleftspace"></div>
<?php include $content;?>
</main><!-- .content -->
</div><!-- .container-->
You can include your file above all other code and echo the respective content and title variables after you did a
$content = file_get_contents( your_content_file );
in your included file.
<?php include $contentFile ?>
//...later in the code
<h2><?= $pagetitle; ?></h2>
<div id="cleftspace"></div>
<?= $content; ?>
Note: The <?= is an open short tag for <?php echo. It may be disabled on your server.
Before
file_put_contents('page-title.txt', $pagetitle);
and in the $content file
<h2><?php echo #file_get_contents('page-title.txt'); ?></h2>
ps: # is just to skip file_exists step )
Related
So here is my layout:
randompagenumber.php
|
|
/ | header.php
php include -| content.php
\ | footer.php
The footer has a php script which calls the filename of the randompagenumer.php, removes it's extension and the dashes in it's name and outputs that number of the filename as a pagenumber in a bottom corner.
(left or right, depending if that number is even or odd).
I have a couple of pages (10 for now), I called them --1.php, --2.php, --3.php, --4.php, --5.php, --6.php, --7.php, --8.php, --9.php, -10.php, ...
So the script turns them simply in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...
The code I have for the footer:
<?php
$pagenr = basename($_SERVER['PHP_SELF']);
$pagenr = str_replace( array( '.php', '.htm', '.html', '-', '_' ), '', $pagenr ); // Remove extensions and spaces in filename
if ($pagenr % 2 == 0) {
$footlr = "footerlinks";
$pcontainer = "pcontainerl";
} else{
$footlr = "footerrechts";
$pcontainer = "pcontainerr";
}
?>
<div id="<?=$footlr?>">
<div id="pnummer" class="pcontainer"><?=$pagenr?></div>
So the div with the id "<?=$footlr?>" would change name upon the verification of an even or odd number and by so would read another css style ( #footerlinks and #footerrechts ).
Now what I tried to accomplish has to do with content.php
I know from html that it is possible to link to anchors within it's own file and so I tried to have s somewhat same result with php but I only got to manage it for the php page to load, but it outputs the whole content.php
So basicly I would like to achieve that if the randompagenumber.php reads its filename (through footer.php), it loads the content from a certain section with id that is corresponding to that page number.
This is how I my randompagenumber.php looks like at the moment:
<?php
$PageName = str_replace( array( $PageName ), '#p', $pagenr, $PageName );
?>
<!DOCTYPE html>
<html>
<?php include('templates/pheaderhst.php'); ?>
<?php include("templates/content.php$PageName"); ?>
<?php include('templates/pfooter.php'); ?>
So what I have done here, is making a new string named PageName which is calling the pagenr string that is calculated from the footer.php and let it echo as the php page to load <?php include("templates/content.php$PageName"); ?>
Just to discover it is actually loading the whole page and not only the content of what I named the section id's.
So knowing that the php script changes a filename from 006.php to simply the number 6, then changes the 6 into #p6 (because an id can't start with a number, I named then with a p first).. How come it loads the full page and not just the content of my <section id="p6">content of page 6</section>?
For those interested, here is how content.php looks like;
<?php
?>
<section id="p1">
<div id="contentwrapperpaginas"><?php echo 'Content from page 1' ?></div>
</section>
<section id="p2">
<div id="contentwrapperpaginas"><?php echo 'Content from page 2' ?></div>
</section>
<section id="p3">
<div id="contentwrapperpaginas"><?php echo 'Content from page 3' ?></div>
</section>
<section id="p4">
<div id="contentwrapperpaginas"><?php echo 'Content from page 4' ?></div>
</section>
<section id="p5">
<div id="contentwrapperpaginas"><?php echo 'Content from page 5' ?></div>
</section>
<section id="p6">
<div id="contentwrapperpaginas"><?php echo 'Content from page 6' ?></div>
</section>
UPDATE with solution
So I got it to work!
First my content.php:
<?php
?>
...some div's with page id's here...
<div id="p5">
<div id="contentwrapperpaginas">Content from page 5</div>
</div>
<div id="p6">
<div id="contentwrapperpaginas"><p style="color:yellow;margin-left:-25px;">Content from page 6</p></div>
</div>
...some more div's with page id's here...
So with the following script I got it to manage to load only one single div and it's elements inside. The example used in my copy/paste will be loading the <div id="p6">. The script is inside index.php and loaded with <?php echo $content_div[0]; ?>. :
$url = 'templates/content.php';
$content = file_get_contents($url);
$first_step = explode( '<div id="p6">' , $content );
$content_div = explode("</div>" , $first_step[1] );
The solution was changing the <?php include("templates/content.php$pagenr"); ?> to <?php echo $content_div[0]; ?>
Maybe it's duplicate but I don't know how it calls and cannot find. I need to load a php-file (template of my block) and set content into it.
For example, template file:
<?php include_once 'boot.inc' ?>
<div class="widget">
<div class="widget-title">I need to put title here</div>
<div class="widget-body">I need to put content here</div>
</div>
And php function to insert new block with this template:
function nm_new_block($title, $content) {};
Can I do it in php?
1° Solution: You need to change you code into:
<?php include_once 'boot.inc' ?>
<?php include_once 'template.php' ?>
<div class="widget">
<div class="widget-title"><?php echo $title; ?></div>
<div class="widget-body"><?php echo $content; ?></div>
</div>
In template.php file you must have something like this:
<?php
$title="My Title";
$content="My Content";
?>
And you don't need function for this cause variables are stored in template.php
2° Solution: You need to implement a function to retrieve variables from external source:
<?php include_once 'boot.inc' ?>
<?php include 'functions.php' ?>
<div class="widget">
<div class="widget-title"><?php echoTitle(); ?></div>
<div class="widget-body"><?php echoContent(); ?></div>
</div>
In functions.php
<?php
function echoTitle(){
$title = 'Add code to get title here';
echo $title;
}
function echoContent(){
$content = 'Add code to get content here';
echo $content;
}
Replace Add code to get title here and Add code to get content here with code to get contents ex:
$title = $_POST['title']; supposing you want get title by a submission form
I do not have enough details to tell you more.
I am using this PHP Code on my website to get wordpress page content from the database:
$page = get_page_by_id($records["value"], OBJECT, 'page');
query_posts('p='.$page->ID.'');
echo '<div id="page-title">'.$page->post_title.'</div>';
echo nl2br($page->post_content);
echo '<p> </p><hr /><p> </p>';
in wordpress, i have put the page with HTML and aligned images etc but when it displays on my website it just shows the images and text all under each other with the incorrect format (not as i put it in the text editor)
what have i done wrong here?
Im also wondering why you are doing like this? there is no need to call post title and content that way. simply make a page.php. The TwentyFourteen does have page template but in a folder named "page-templates". You can simply make a page.php by sample code below.
<?php get_header(); ?> //the header template
<?php while ( have_posts() ) : the_post(); ?> //start the loop
<?php the_title(); ?> //Page Title
<?php the_content(); ?> //The content
<?php endwhile; ?> //end loop
<?php get_sidebar(); ?> //sidebar template
<?php get_footer(); ?> // footer template
you can add div / span wherever you want.
I have created a few php files Following are their names:
standard_head.php (which contains basic standard html code)
header.php
navbar.php
sidebar.php
footer.php
standard_footer.php (which contains closing html tags)
Page content will vary in every page and would also include html content.
I am trying to create a lot of php pages which will use all of the above pages directly.
Now I can use them directly using include statement for each of them, but I was wondering if it was possible to include all of them together with one just statement ?
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<!-- my page content -->
</div> <!-- end #content -->
<?php include('includes/sidebar.php'); ?>
</div> <!-- End #wrapper -->
<?php include('includes/footer.php'); ?>
<?php include('../includes/standard_footer.php'); ?>
You could so this (note that this is a very basic example and that I wouldn't use this myself without taking into account meta tags, page titles etc.)
template.php
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<?php echo $content; ?>
<!-- my page content -->
</div> <!-- end #content -->
<?php include('includes/sidebar.php'); ?>
</div> <!-- End #wrapper -->
<?php include('includes/footer.php'); ?>
<?php include('../includes/standard_footer.php'); ?>
Then, on a page (say index.php, for example):
index.php
<?php
$content = '<h1>Welcome</h1>';
include 'template.php';
?>
P.S: If you go down this route, make sure that you check out the Output Control Functions first.
Just create a PHP file that has a list of them i.e.
<?php include("this file");
include("the other file");
?>
And then just add that file.
Not really an answer to your question, but you could use a template engine like Twig.
http://twig.sensiolabs.org/
Try using more complex template system like Smarty or some MVC framework like Zend (this is not required but would allow you to create complex sources more easily) and then build script like this:
<?php include('../includes/standard_head.php'); ?>
<?php include('includes/header.php'); ?>
<div id="wrapper">
<?php include('includes/nav.php'); ?>
<div id="content">
<?php echo $menu->getCustomEntries(); ?>
</div>
Where $menu will be your custom object containing methods for displaying menus and submenus...
There is no straight way to include multiple files, as include / require functions except only one argument. though you can use following logic.
`#include_all_files.php
include('../includes/standard_head.php');
include('includes/header.php');
...
use above file in other files
include('includes/include_all_files.php');
`
I write a custom type for wordpress which is contain a textarea. I can save it and also i can access from single view. But i want to show them line by line if author save it line by line. But it's not showing them line by line, it shows all of them as a single line.
Here is my single.php's custom meta show codes;
<div id="yemek_resim">
<?php if($tz_image_display == 'true') : ?>
<?php if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) : /* if post has post thumbnail */ ?>
<?php the_post_thumbnail('single-large'); ?>
<?php endif; ?>
<?php endif; ?>
</div><!-- yemek_resim -->
<div id="yemek_malzemeleri">
<?php
$malzemeler = get_post_meta($post->ID, 'ofa_yemek_malzemeler', true);
echo esc_attr($malzemeler);
?>
</div><!-- yemek_malzemeleri -->
here yemek_malzemeler's content come from a textarea and author can write multiline content. But at site, they seem as a single line...
You should use the N2BR PHP function in the output file. Just write <?php echo n2br($string); ?> in the single.php file from Wordpress. Good luck ;)
I resolved the problem by using <pre> </pre> tags. thanks for your care.