Replace word in template page with title of page in php - 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".

Related

how link several pages, and adding a plugin to a new custom WordPress theme

I've created my own WordPress custom theme for a website that I'm working on. It's my first time building a theme from scratch! I'm having a difficulty 1st: to link several pages to the fron-page, or home page. For example lets say that I want to link "Who we are", page + "Contact us", page, etc.. How can I make WordPress understands that each page is different, and link to it? here is what I did so far:
in page.php I placed this code:
<?php get_header(); ?>
<div class="container">
<?php
if (have_post()) {
while(have_post()) {
the_post();
get_template_part( 'template-parts/content', 'page');
}}
?>
</div>
<?php get_footer(); ?>
I also created a content-page.php where I've placed my HTML code. (I'm building the whole thing with an HTML and CSS code that's just like building a website from scratch).
I also want to know when using a plugin, how can I connect that into the pages that I'm adding. For example, if I'm installing a form such as WpForms for a page like "Contact us" how can I link that?
Please let me know if my questions need more details
If I understand this correctly...
You want to be able to display (link) a couple of pages to your home page?
Ex:
[home page content here]
[Connect page content here] [Blurb page here]
If so, you need to pull in the page content into the home page.
Something like:
<?php
$postid = 0 /*Need to get page id, that's the key */;
//Pull in the page data
$featured_page = get_post($postid);
$thumbnail = get_the_post_thumbnail( $featured_page->ID );
//Only show the excerpt in this case
$content = apply_filters( 'the_content', $featured_page->post_excerpt );
?>
<article id="post-<?php echo $featured_page->ID ?>">
<header class="entry-header">
<h1><?php echo $featured_page->post_title ?></h1>
</header>
<div class="entry-content">
<div class="thumbnail">
<?php echo $thumbnail; ?>
</div>
<div>
<?php echo $content; ?>
</div>
</div>
Important! You'll need to get the page id from somewhere. You can hard-code it in, but that's risky if something changes. You can set up an Customizer option that lets the user select what page to use.
https://codex.wordpress.org/Theme_Customization_API
As for the contact form etc. I'd suggest putting it on a page as a shortcode, then displaying the full page, not the excerpt.
I use this code for a theme I built myself. If you need anymore help, let me know, I can show you where to find it.

Adding content to wordpress page

So, i finally got my css and js scripts to load on WP
Now there is but one thing i need to get done.
i have my own theme, including header.php, footer.php, page.php
header.php and footer.php is working just fine, loading scripts and showing properly, but now i need to add all the other content.
my page.php is currently:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php the_content(); ?>
<?php get_footer(); ?>
I would need to somehow add html content to pages, i have about 20 ready made .php pages which needs to be transfered to WP.
Soooo how do i go about making new page (pages -> add new) and using page template while getting the html content to show up?
I've tried to just make new page and in text mode add up all the html into page, but it only shows empty page with header and footer, so the problem is most likely the page.php and i have no idea how to get it to work.
You can do look like this:
<?php /* Template Name: CustomPageT1 */ ?>
<?php get_header(); ?>
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile;
?>
<?php get_footer(); ?>
You are on the good way. While developing a custom theme from scratch is a great challenge it's not too hard.
I could recommend to take it easy and follow this tutorial I found really helpful some time ago, I learned a lot there:
Developing a WordPress Theme from Scratch
You must have the official source documentation always in your mind:
Theme Development
Do some reading and you will see that making themes is really fun and gratifying :)
EDIT:
I would recommend picking a good starter theme or framework and work using child themes. You have plenty of them to pick.
To get started adding a new page to your WordPress site, find the Pages menu in the WordPress Dashboard Navigation menu. Click Add new.
The WordPress page editor looks nearly identical to the post editor, except for a few different boxes located on the right side of the screen.
Add the title of the page, like About. Note: If you have pretty permalinks set up, the title of your page will also be the URL slug.
Next, add some content.
The Publish section of the page editor is exactly the same as for writing posts. When you’re ready to publish, you can either publish immediately, save this or a draft, or schedule the page to be published later.
The Page Attributes section applies a parent page and template to your new page. For the Parent section, you can arrange your pages into hierarchies. For example, you could create this new page with additional pages under it. There are no limits to how many levels you can nest pages.
Some WordPress themes have custom page templates, so the next Template section allows you to apply a template to your new page.
The Order box allows you to order your page numerically. Pages are usually ordered alphabetically, but you can choose your own order by entering a number in this field.
Preview the page one last time, then click Publish. You’ve added a new page to your WordPress site.
This is how your
your index.php should look like :
<?php
get_header();?>
<div class="YourContainer">
<div class="Whatever">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="ContentSectionDiv">
<?php the_content();?>
</div>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
</div>
</div>
<?php get_footer();?>
you can make also a custom loop
<?php
$arg = array("post_type" => "services",
"posts_per_page" => 9,
"order_by" => "date",
"order" => "ASC",
);
$services = new WP_Query($arg);
if ($services->have_posts()):;
while ($services->have_posts()):$services->the_post();
the_content();
endwhile;
endif;
?>

wordpress custom theme search has quantity but displays no results

Trying my best to wrap my head around wordpress search process and results.
Been reading and researching online for about 8 hours straight at this stage and have gotten almost nowhere.
I have a completely custom theme, designed by a friend for a client. I have a search box in a nav bar at the top of the page, inside the header. I'd like the user to be able to search the site's content from that search box in the nav bar at any time. I've found plenty of examples of this being accomplished on other pages, though they often seem to be using a ready-made theme.
Eventually I'd like to be able to understand what's going in the search.php file well enough to edit it along with the searchform.php file so that the search box and results appear unified with the rest of the theme.
At the moment the closest I've gotten to having anything functioning at all is the inclusion of a quantity of search results showing the appropriate number of hits expected, but I still can not figure out how to display a result.
I've been combing through others' page sources and every hit I can google about customising or just explaining the search.php file, and yet all I keep finding is "read the Creating a Search Page" and "it must be a theme issue." This is not gainful information.
My understanding of what's being taught in the "Creating a Search Page" info is that it describes the steps for creating a custom search page that one can hyperlink / permalink to, but which is unrelated to the default search function within the wordpress kit. Indeed, I've successfully created a custom search page, but it behaves simply as a stand-alone page, and is not related to a customised search results page. To customise the look of default search function results, I understand the search.php file must be edited. So far the "what to edit" within that file escapes me, but for now I'd just like to understand why I am seeing the correct number of results, but no actual results.
My search.php file looks like this:
<?php get_header(); ?>
THIS IS THE TOP OF SEARCH.PHP
<section id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<h1 class="search-title">
<?php echo $wp_query->found_posts; ?> <?php _e( 'Search Results Found For', 'locale' ); ?>: "<?php the_search_query(); ?>"
</h1>
</header><!-- .page-header -->
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
/*
// Previous/next post navigation.
twentyfourteen_paging_nav();*/
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
</div><!-- #content -->
</section><!-- #primary -->
THIS IS AFTER THE SECTION OF SEARCH.PHP
<?php
// get_sidebar( 'content' );
// get_sidebar();
get_footer();
*Note: The notes in CAPS are to help me understand what content is coming from where as I try to figure out what each phpiece of the puzzle is doing.
Also, the twentyfourteen_paging_nav line is commented out as it kept spitting back an error. The original search.php was from a (default theme?) folder called "twentyfourteen". My current folder is called "ewgi2014" however, replacing twentyfourteen_paging_nav with ewgi2014_paging_nav spat back the same error, and I can't find any more useful information on the topic. Also, when looking at other sample search.php files I didn't see the paging_nav call, so I tried commenting it out, and was able to at least avoid the error, though I still don't understand it.
Sample output on the page after searching for the term, say, "program" is:
8 Search Results Found For: "program"
...with nothing else written beyond that.
Not entirely relevant, I believe, but my searchform.php looks like this:
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>">
<label>
<input type="search" class="search-field" placeholder="search …" value="" name="s" title="Search for:" />
</label>
<input type="submit" class="search-submit" value="Search" />
</form>
I've read and re-read the "Creating a Search Page" page from the Codex so many times I could probably recite it by heart, but haven't found any solutions within.
I've read the stackoverflow hit about "How to display Wordpress search results?" and have tried the code from ThemeShaper, but that throws an error on the shape_content_nav line(s).
I've read this and ... and apparently I can't link to all the other things I've read as this is my first post, but I have been reading a lot, and fiddling, and making very little headway.
Apologies for the long-winded post. Just trying to be thorough and show my research and current point of understanding.
What happens if you put
the_title();
Directly after your while statement?
Each time you loop within the while loop, you are processing each post. Currently, on each loop, you are entering a content-*.php file, where * depends on the post format. Does that file exist?

Wordpress custom page code

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

Adding your own custom page to Wordpress with its own custom script

I wish to add a custom page to wordpress, not in the usual sort of way. This page has my own custom script and functions and to develop a plugin is beyond my means so I was wondering if this is possible:
Suppose I were to build it on the existing page.php script in my there folder, my script looks something like this:
<?php get_header(); ?>
<div id="left-area" class="clearfix">
<?php
//My script would go here
?>
<div class="comments">
<?php comments_template('',true); ?>
</div><!-- end of comments div -->
</div><!-- end of left-area -->
<!-- LEFT AREA ENDS HERE -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Is this even possible? I tried and got a page not found error. And if so, how do I call this script assuming WP is installed in the root directory? Example: www.mysite.com/myscript
Absolutely. Duplicate page.php. Rename it to template_mine.php. Open to edit, and before the get_header tag, add:
<?php
//
// Template Name: My Custom template (choose something more descriptive, like contact page)
//
?>
Go to Wordpress dashboard for pages, create a new page with your title, and in the Page attributes on the right hand side, under templates, select your template name, update, and you are all set.
Reference: http://codex.wordpress.org/Pages#Creating_Your_Own_Page_Templates

Categories