Customize static wordpress pages correctly - php

I m building a word press site with a unique theme that i created, now i got like 10 classes of different styles for the static pages in my site.
The question is, when i want to display the content of the page, with all the forms images and text, how should i do that? With the text editor where the user usually insert his content for the posts in regular wp website? Or should i insert the content images and forms hard coded in the php custom page template of those static pages?
In the text editor if i insert some html in edit as html it's becoming a mess it doesn't seems right to create sections and div where the user regularly just put his raw content, and anyway adding more content to a static page in the future will require my intervention to wrap it with css sections and div's definitions and classes...
By the other hand to write raw data with images and forms in the php custom page template doesn't seems to me the right choice... Is it normal to do so for static pages?
Thanks everyone!

Wordpress static pages, by default contains text and images that you can put into it using the Wordpress text editor. This content is known as "The Content" in WP context.
If you want to put into your static page some elements that cannot be setted by the text editor (like your own forms, per example) you'll need to create your own page template with hard-coded elements.
Example:
<?php /* Template Name: My custom page template */ ?>
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div id="article">
<div class="date"></div>
<!-- Your own custom content here -->
</div>
<div class="clear">
<?php if ( comments_open() ) : ?><div id="comments"><?php comments_template('', true); ?> </div>
</div><?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>

I normally will put those styles into styles.css of your theme. Even if it's images that will be standard images for the theme or shortcodes available to the user you want it to load from within the styles.css page. You don't want to do anything static like that with WordPress. Updates can mess it up as well as having issues with inline css messing up your SEO for the site.
If the user needs to do something in the editor to have an image show or something like that you can do it as a shortcode. That's normally going to be your best approach. Have you used shortcodes and loaded the images that before?

Related

WordPress custom template not displaying custom HTML, Custom fields, etc

I am developing a custom theme in WordPress, and am trying to put custom HTML and PHP in my home page template. Whenever I am posting code for divs or custom fields in my home page template, they are simply not rendering and displaying on the front end. In the example below, the <div class="col-full"> is not being shown on the front end.
<?php
/*
Template Name: home
*/
?>
<!DOCTYPE html>
<?php get_header(); ?>
<center>
<div class="col-full">
<?php get_template_part( 'content', get_post_format() ); ?>
</center>
<?php get_footer(); ?>
Looking at your code, you've set it up as a template file. These need to be manually selected from the page as the template for it to work.
I'd recommend consulting the wordpress template hierarchy https://wphierarchy.com/. This shows you which files will try to be loaded based on what page you're visiting.
If you're still having trouble, i'd suggest installing the plugin https://en-au.wordpress.org/plugins/query-monitor/, which will tell you the template being loaded as well as other handy information.

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;
?>

Different Single.php content depending on page

Backstory: I've been dealing with this website for the past 5 months. I didn't have previous experience with Wordpress, but I had some with HTML, CSS and PHP.
I have two different ways to display posts on my custom Wordpress template.
On the "Services" page, a post is loaded onto a dynamic content div by clicking on different buttons. Each button loads a different post on the same div by calling specific post ID's:
<a
href="#contentBox1"
class="dem_buttons"
onclick="loadPage1('<?php the_permalink(361) ?>')"
target="#contentBox1">
</a>
<div class="text-center clearfix" id="contentBox1"></div>
On the "Search" page there is no dynamic content div. Each search result consists on an image, the title and the excerpt of the post. The posts are acessed by clicking on their respective titles after the search results come up.
Here's the problem: If I don't include the header and the footer on single.php, the post will be loaded without styles, navigation bar and footer after clicking the title of the search result. I need to load the header and the footer on single.php because of this search page, but this means there will be a second header and footer on the dynamic content div of the "Services" page, as well.
I've tried starting single.php with:
<?php
if ( is_page([page id]) ) { ?>
[single.php's content]
<?php
} else {
get_header(); ?>
[single.php's content]
<?php get_footer();
} ?>
to no success. I've also tried out !is_page, but no luck on that either.
Please don't recommend plugins. I really need to code everything by hand. The written reason for that would be longer than this post.
The final solution was to rearrange the parts of single.php so that it used include (or require) for showing the given post's content (named here: postcontent.php).
// Pseudo code
get_header()
// postcontent.php is the body of the post
include_once( postcontent.php )
get_footer()
This way the single.php had its header and footer, but the "Services" template could include the post's content directly:
// Pseudo code
// Template: Services
get_header()
// the content of the "Services" template
services_content()
// postcontent.php is the body of the post referenced
include_once( postcontent.php )
get_footer()
This way headers and footers are there on both pages, and there are only one header and footer on each of them.
I'm not 100% sure on the Wordpress side of all this, but could you perhaps wrap the second/first header in a div with a specific id, then give that id a style of display none? Like below:
HTML
<header class='header'> ..... </header>
<div id='hidden-header'>
<header class='header'> ..... </header>
</div>
CSS
#hidden-header {
display: none;
}
Let me know if that helps or not and maybe I can see if I can find something else.

Convert html to wordpress & provide link to menu inside panel

I have an HTML template which I am trying to convert into PHP for WordPress.
The homepage has been converted and is shown properly. Next, the menu in navbar is about, the page that needs to be converted in PHP.
I have just added get_header() at the top and get_footer() below. In between these two I have added the HTML content for my entire page.
Then I tried to provide the link for the menu that I created, but the content of about page is not visible.
Do I need to add any other line for that page apart from get_header and get_footer? Or is something wrong with the link?
Clearly I can't understand what your are trying to say. I think you want to create a page that will show your about menu content.
So in your theme directory create a page name page.php and use get_header(); in first and below get_footer and where you want to show page content write the_content();. like this
<?php
get_header(); ?>
<div><?php the_content(); ?></div>
<?php get_footer(); ?>
after that follow the url of # Sajid Anwar. because for dynamic data you have to create pages(home,about etc) from wordpress dashboard and in menu section add these pages as navigation menu to menu.

Styling separate elements on a page in Wordpress

Using Wordpress I am designing a theme for a client's website.
I have set the landing/home page to a static page, and I'm using front-page.php as the default template for this.
My question is how can I style individual elements on the page when all of the page content is called in at once using <?php get_template_part( 'template-parts/content', get_post_format() ); ?>
Currently I multiple <div>'s in the WP page content editor like so:
However, I'm concerned that my client or something else, may delete this content by accident and completely spoil the design. How can I separate content and style it accordingly in my template?
My question is how can I style individual elements on the page?
There are always body classes you can use for different pages/posts.
How can I separate content and style it accordingly in my template?
There are few options depend on what you need:
Turn off "Visual" editor mode for each user account. It sort of helps.
Move the custom HTML into the template files. If that's OK.
Use custom fields, preferably with Advanced custom fields plugin.
For every page,if you need the alone class then function :body_class() : is sufficient.Syntax :- < body < ?php body_class(); ?> > and you can also implement the another function : post_class : for the loop class.Syntax :- < body < ?php post_class(); ?> >

Categories