How to fetch Page ID in wordpress - wordpress

I am using this code to bring the content out of the page.
<?php
$page = get_page_by_title( 'page-title' );
$title = $page->post_title;
echo "<h3>" . $title . "</h3>";
$content = apply_filters('the_content', $page->post_content);
echo $content;
?>
Now, i want to get the content and title by using page ID.
If any one has the answer?

You can use the get_post() function
$myPost = get_post(17);
echo $myPost->post_title;
Per your comment:
Is there any way i can avoid putting id in the direct code. eg. just
making a page and content from back-end it reflects in front-end. i am
making a onepage website. can it will be done by any kind of loop or
something like that.
I bolded that word because if this is the case there's a very simple solution, but there are many ways to reach it.
Option 1:
If you're familiar with the WordPress Template Hierarchy you'll know you can create a PHP paged called front-page.php. Then you can create 1 single page under Pages, once that's done you can go to Settings -> Reading and click the radio button which allows you to
Set a static page (select below)
You can then select the page you created. This will automatically use the front-page.php template versus page.php.
Option 2:
You can copy your page.php template and rename it to use a pages slug. For instance, if I create a page called "Contact Us" WordPress will automatically make a slug that looks like contact-us which is part of the web URL to view the page. I can then copy page.php and rename it page-contact-us.php so when you view the "Contact Us" page, it will automatically use this page template.
Option 3:
Finally you can directly create a Page Template where you select it from a drop down in the Admin Panel when you create a new page. All you have to do is copy your page.php template file and call it whatever you want, something standard like page_template-contact or something of the sort. Then you can add this direct above your get_header() call:
/**
Template Name: My Custom Page
**/

Related

How to Create Single.php for Wordpress

I designed a template for WordPress, I've only encountered a problem in one section.
How can I create a post to display single.php for my site?
Thanks!
Your issue is a trivial thing. In order to show your blog/website posts, you need to call the_post() function inside of a loop.
As long there are posts in your blog, the_post() should render a response. Take a look at Twenty_Fourteen single.php default template in order to get a better understanding of how things should be.
Make a back up copy of single.php, call this single-original.php.
Open single.php file. Delete the contents of single.php and place the below code in it.
<?php
$post = $wp_query->post;
if ( in_category('2') ) {
include(TEMPLATEPATH . '/single_new.php');
} else {
include(TEMPLATEPATH . '/single_new1.php');
}
?>
Open single-original.php and choose File > Save As and call this file single_new.php. Choose File > Save As again and call this file single_new1.php.
Upload single.php, single_new.php and single_new1.php as well as style.css, if you made changes in this.
Refresh your web page and click on one of the post titles in the category for which you are using single_new1.php.

How to make a post page in wordpress?

I have been searching on google but unable to find any proper tutorial. Can anyone tell me how do i make my own post page in wordpress?
For Example:
In wp-admin, There is already a Post option in menu..
Post>Add New
Post>All
Post>Categories
Post>Tags
In Add New, There is a blog form, where we can add post but I want another option in Menu.. called Licenses, Then option of Add New, When i click on it, There will be a form, What I have to do is to add Featured image in it and add title of it and publish and then it should show on license page. Demo License
Similarly I want another Trades, in which I should add trades and it should show on trades pages. Note, the trade page and license page will have different Style for showing, different Css. In Trade page there will be a boostrap accordion which will show trades, Demo Trades
Similarly I want another page for this affiliation page.. Demo Aff IN this page, there are some images with title, and sometimes client send more images, so i want a form in my wp-admin where i can just upload image and show on page like im showing. It will show differently for all pages.
How will i achieve this? Any suggestions? Any other method? im totally new to wordpress
You can create your own page template. Here is the reference
In your template directory create a new file like page-anyhing.php
Add this to your start of the file:
<?php
/**
* Template Name: Anything
*
* #package WordPress
*/
Then, when you create a new post, at the right side of the dashboard, you can select the Anything from Page Template menu.
You need to build your page template by PHP and HTML codes.
UPDATE
Based on OP comment:
You need to create a new template, eg. affiliates as I mentioned above.
When you get a new affiliate, then you can create a post, and add Category: Affiliates.
In your page template you need to get posts by categories:
$query = new WP_Query( array( 'category_name' => 'affiliates' ) );
Here is the reference from WP_Query.
if ($query->have_posts()) {
echo '<ul>';
while ($query->have_posts()) {
$query->the_post();
echo '<li><h2>' . get_the_title() . '</h2><div>Show image here</div></li>';
}
echo '</ul>';
} else {
echo "Sorry, no affiliates...";
}
/* Restore original Post Data */
wp_reset_postdata();

Two pages with same content

How to create two pages with same content?
For example, the first page should be with a header menu and the second page should be without a header menu.
The page of my theme has an options without header
Or I can use two different templates of page, but the content of pages should be the same.
You can read the documentation here.
Create a file, called page-mytemplate.php under your theme directory.
In that file, start with this:
<?php
/*
Template Name: My Custom Page
*/
Create a page, and set the template to My Custom Page.
At here you can do whatever you want.
For the other page, repeat the steps, but just call the other template something different name.
EDIT
When you want to show the same content with different templates, just simple get the content of the page:
$post = get_post($postId);
echo $post->post_title ."<br />";
echo $post->post_content;
You can wrap this with your two template page. See the documentation here

Wordpress get permalink of a first page of a specific page template

I wonder if it is possible in Wordpress to get the permalink
of a first page of a specific page template.
For example, I have a couple of custom page templates.
In my index page I have to anchors that one should load the first page of default (text) page template and the second should load the services template for example.
Is it possible to achieve this in Wordpress?
If yes, what is the solution?
Thank You in advance!
There aren't any WordPress functions to do this (that I know of) but you can write your own query with $wpdb. If I had a template with a filename of "template-faq.php" and wanted to get the permalink of the first page that used this template it would look like this:
<?php
global $wpdb;
/* Get post IDs of all pages using "template-faq.php" */
$faq_pages = $wpdb->get_results("SELECT `post_id` FROM $wpdb->postmeta WHERE `meta_key` ='_wp_page_template' AND `meta_value` = 'template-faq.php' ", ARRAY_A);
/* Get permalink using post ID of first page in the results */
$faq_permalink = get_permalink($faq_pages[0]['post_id']);
?>

Make another content list as content file of wordpress

I have page I want to display the page in different style (not as the content.php file ) so now I made copy the content.php file and just modified it. I want to display only for one page and other pages should display like content.php . So how can I change the index page to display the particular as new content-list page. My page name is News and it should be display like content-list.php.
For display indiviual page you have to make a Template for that page and from
Dashboard-->Pages open the page where you want to display that template, on right hand side you have seen TAMPLATE-->DEFAULT TEMPLATE, select template you have created...
Syntax for new template
<?php
/*Template Name:abc
*/
?>
<?php get_header();?>
<!--------------------------------------->
CONTENT FOR PAGE
<!--------------------------------------->
<?php get_Footer();?>
I don't know about which file you're referring to as content.php
I have page i want display the page in different style
So, if you want to display a page with a different style, you should have a look at WordPress Template Hierarchy.
You can create another page template, use custom files like header-{name}.php and footer-{name}.php and use get_header() and get_footer() with the appropriate arguments(the {name} you mentionned to call these files.

Resources