Wordpress custom page code - php

I'm very new to wordpress and was just wondering if anyone
could point me in the direction of a tutorial which will explain
how to add my own code to wordpress pages.
So far all that I've been able to find is that I'm supposed
to copy the page.php from my template and use that as the
template for a page added in wordpress. This however only
gives me control over the body and not the header/nav menu/sidebar.
Am I correct is assuming I must replace the method calls such as
get_header(), etc with the code it would generate and manipulate
it from there? Also is it correct if I change the arguments to
get_template_part( 'content', 'page' ); and add a php page with
the appropriate name for the body?

Way 1 You can do this:
1) add new page
2) add these code to index.php or page.php
<?php
if (is_page('About 3C')) {?>
<div>Display this particular div</div>
<?php }
else { ?>
<div>Display that particular div</div>
<?php }?>
or
<?php
if (is_page('About 3C')) {
<div>Display this particular div</div>
}
else {
<div>Display that particular div</div>
}
?>
assume your added page name is: About 3C
and you can call the database from above code
OR way 2: please refer:
http://line25.com/tutorials/how-to-create-a-wordpress-custom-page-template

Related

Get_Header not working on Home Page Wordpress - Tried Almost Everything

At my website: www.TattiniBoots.com, I would like to add a larger header than what exists on all other pages. Below is the code that I am using
<?php
if (is_front_page() ) {
get_header( 'home' );
} else {
wp_head();
}
?>
When removing the else function, all other pages lose the header except the home page; and therefore I am sure that the call script is working appropriately
My new header file that I am calling is: "header-home.php" which includes the col-md-12 instead of col-md-3. However, it does not display
Even when removing all code inside "header-home.php" the original old header will still display on the home page
What am I doing wrong?
I simply need one line of code to be altered on this index page (col-md-12 instead of col-md-3)
You can do this programmatically without having to create a different header-home.php. Following is the code.
if (is_front_page()){
$header_home_class = "col-md-12";
} else {
$header_home_class = "col-md-3";
}
//whereever you want to add the class name
<div class="<?php echo esc_attr($header_home_class);?>">

How to include php script into header for specific page in wordpress?

I want my script to load only in one certain post page in my blog.
Example page: www.name.com/this-is-cool-post/
How can i call my script if for example my script is in another .php file?
<?php if(is_page('this-is-cool-post')){ ?>
<?php include("myscript.php"); ?>
<?php } ?>
this is not working for me and i don't know where to problem is :S
This may not be the most programmatic way of doing it, but how about creating a custom blog post template, call it blog-specific.php or whatever you want, add the following to the top of it:
<?php
/*
Single Post Template: [Temp Name]
Description: Description of template
*/
?>
Then add your script (<?php include("myscript.php"); ?>) only to this template:
You should then be able to change the template of a particular post in the Single Post Template dropdown option when editing a post:
I think the problem is the location of your script.
If you work on a theme, try to include your script like this
include get_template_directory() . "/path_to_ur_script/script.php";
I hope that help u.

Dynamic title depending on page (DRUPAL 7)

Before I made the switch to Drupal (previously just static HTML), I had a div title in my header that changed depending on what page the user was on using the following code:
ABOUT PAGE
<?php
$title = "ABOUT US</span>";
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/includes/header.php";
include_once($path); ?>
The reason this worked, however, was because I was using static HTML... which means I had every page set to use its own Index.PHP, located at (for the About page, for example) public_HTML/about/index.php. Therefore, I could edit each page individually to my liking by editing its respective Index file...
Now, with Drupal.. instead of having so many Index.PHP files like I did before (which was messy IMO...), I want to have every page use the same page.tpl.php (with the exception of the front page, which uses page--front.tpl.php).
Can I somehow modify the above PHP code to display a different title using IF statements depending on what page the user is on?
For example... (I have no idea how to PHP code so this is just to give you experts an idea of what I would want..)
Ifpage=about, $title=About Us;
Ifpage=contact, $title=Contact Us;
Could this code all be in the same page.tpl.php?
Thank you so much!
Drupal 7 has a title field for each content type. Why don't you use that?
It shows by default and you can change it for every node.
It is this code in your template file.
<?php print render($title_prefix); ?>
<?php if ($title): ?>
<h1 class="title" id="page-title">
<?php print $title; ?>
</h1>
<?php endif; ?>
<?php print render($title_suffix); ?>
$title_prefix (array): An array containing additional output populated by modules, intended to be displayed in front of the main title tag that appears in the template.
$title: The page title, for use in the actual HTML content.
$title_suffix (array): An array containing additional output populated by modules, intended to be displayed after the main title tag that appears in the template.
See https://api.drupal.org/api/drupal/modules!system!page.tpl.php/7 for all the variables available in your page.tpl template file.
If you want to style one part different from the other you could use an extra field for it.
Or if you need to do something with the value (I wouldn't suggest this!!!).
switch ($title) {
case "About Us":
// Do something with the title
break;
case "Contact Us":
// Do something with the title
break;
case 2:
// Do something with the title
break;
}
Here's a simple approach:
Add a new field for the content type
Call it title2
Render the two fields
Use css to inline align them and add styling
Here was my solution since I am unable to position a Drupal-based title block right next to a non-Drupal based logo DIV..
I created a div called .headertitle and positioned/styled it how I want.
My header.php is an PHP include file which is called by each page and the headertitle has a PHP condition..
<?php if(empty($headertitle)) $headertitle = "ASK REAL QUESTIONS, <br><span class='white'>GET FREE ANSWERS.</span>";
echo $headertitle; ?>
That way, if no text is declared for the .headertitle, it takes the default form of "ASK REAL QUESTIONS, GET FREE ANSWERS."
...Now, I had to figure out a way to actually declare text for the .headertitle depending on what page I was on...
I did this by using a custom template for each page (node) by using this file.. "page--node--nodeid.tpl.php".
So, for my ABOUT US page, I created a tpl.php file called "Page--node--7.tpl.php".. for this page, the 7 is the nodeid which is easy to find.
Then, I added the following PHP code to declare text for the .headertitle specifically on the ABOUT US page which looks like this...
<?php $headertitle = "ABOUT<span class='white'>.US</span>"; include('includes/header.php'); ?>
DONE AND DONE.
I'll do that for each page. :)

Customizing a WordPress Template to Remove *One* Page Title

I'm using WordPress to build a simple personal website. I know (basic) HTML and CSS but no PHP, so I'm having a little trouble with a particular customization.
My theme inserts the title of the page at the top of the page. I would like to stop it from doing this on one page only (the home page).
Thanks for any help. The site is here: http://www.melvingauci.com/
Try going into your page.php template and remove <?php the_title(); ?>. Then add this:
<?php if ( ! is_front_page() ) { ?>
<?php the_title(); ?>
<?php } ?>
Note: This assumes your home page uses the page.php template. If another template is used, then the same approach will work.

Replace word in template page with title of page in php

I am now starting to realize the infinite ways a person can utilize php.
I have created a template page called new-member.php that calls in the content from another page so that when I add a new member, it is fast and easy.
The path I use now is:
I add a new page with the company name as the title.
I choose the new member template.
I Click publish. This creates a "new member coming soon" page that is up in seconds. Perfect!
But I would like to take it to a higher level and have a php function replace "New Member" with the page title (The company name) so that when I add a new page, not only does it have the new members url, but instead of saying "New Member coming soon" it says "XYZ Company Coming Soon!" which is a temporary page until the official one is built.
Could someone please show me the correct code and where to place that code? I am using wordpress. I have no clue how to write this code. Does the code go into the new member.php template? The header? I have no idea but want to learn!
Please help!
What you need to do there (if I were in your position I would have put together a plugin or do some functions.php hacking) is capture the output of the included page and replace your specific tag "New Member" with what you need. In the below example i'm going to use %NEW_MEMBER_NAME% instead of "New Member", which makes your replacement string more unique, so that in your placeholder wp page you will write
"%NEW_MEMBER_NAME% comig soon".
<?php /* Template Name: New Member */ ?>
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php
the_post();
// i don't think you need this anymore
// get_template_part( 'content', 'page' );
?>
<div class="entry-content">
<?php if(function_exists('iinclude_page')) {
ob_start();
iinclude_page(112);
$content = ob_get_clean();
echo str_replace('%NEW_MEMBER_NAME%',the_title('','',false),$content);
} ?>
</div>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Remember that it's never a good ideea to hardcode things in your templates, as you are doing here with "112", the id of your placeholder wp page, and presumably the replacement tag %NEW_MEMBER_NAME%.
You should fork the theme, and create an admin options page for these two.
Hope this hepls you in some way.
Edit your newmember.php template file. Find 'The Loop'. This is where your post content is shown. Echo the title out.
http://codex.wordpress.org/Function_Reference/the_title
//syntax
<?php the_title( $before, $after, $echo ); ?>
//add this to your newmember.php template
<?php the_title('<h3>', '</h3>', true); ?>
If you post what is in your newmember.php template file then a more specific answer can be provided. But this is simple enough, I think you can probably figure it out on your own.
Not sure If I understood your question correct.
But here is what I think you may have to do.
Just replace
New Member coming soon
with
<?php the_title();?> coming soon
Now you will get the page title instead of "New Member".

Categories