Adding content via wordpress admin - php

I'm developing a wordpress theme where I have a set of custom pages and each page has it's own content! For an example, say there is a custom page where the content is already done using HTML but I need to be able to change the content of a single paragraph using the wordpress admin panel.
How do i do this using the wordpress framework? is there any special way of adding custom editable fields to specific content locations in a page?

Just add meta box and get the values from post meta to show in your html
you can generate metabox from this site without any programing skills
https://jeremyhixon.com/tool/wordpress-meta-box-generator/
and get meta field value with the below code
get_post_meta(get_the_ID(), 'meta_key', true);

Use Wordpress loop to do this:
<div class="content">
<p>Your static content goes here</p>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<p>Or here</p>
</div>
This part between if(have_posts()) and endif; will be generated from Wordpress admin panel so you can easily change it with page WYSIWYG editor without any additional plugins.
Don't forget to create your custom pages as page templates and assign to page in admin panel otherwise it won't work as expected.

Use the following code:
<?php
$id=2;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
Here, $id=2 is your page id which is you get from WordPress Admin panel.

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.

How can I set up a taxonomy category as my page on wordpress?

I have this theme on my ecommerce website and its called Soma. He is great but sadly, the team doesn't support anymore.
The question I have is how can I set up a category as a page? Cause on this particularly theme, the sidebar menu doesn't show up on normal pages, just on shop pages and categories pages.
I need to create a page that shows only one type of product, that's ease with woocommerce code. But the problem is that on that page, it doesn't show the sidebar menu.
I will put some prints here, maybe it can help.
Print of the problem:
<?php
/*
Default Template Page
*/
get_header();
if (have_posts()) : while (have_posts()) : the_post();
$soma_vc = preg_match("/\[vc_row.*?\]/i", get_the_content());
$soma_page_class = "container";
if ($soma_vc && function_exists('vc_map')) {
$soma_page_class = "n-container";
}
?>
<div class="<?php echo esc_attr($soma_page_class) ?>">
<?php
the_content();
// WP Link Pages
$args = array(
'before' => '<div class="navigation">',
'after' => '</div>'
);
wp_link_pages($args);
?>
</div>
<?php
endwhile; endif;
// Comments
comments_template();
get_footer();
And this is what I need to put there:
<?php if($soma_shop_sidebar != '2' && is_active_sidebar('shop-sidebar')) : ?>
<div class="soma-overlay" id="woo-sidebar_overlay"></div>
<div class="woo-sidebar fixed-lateral left item-delay_off">
<div class="hide-scrollbar">
<div class="inner-hide_scrollbar">
<div class="woo-sidebar_padding">
<?php echo esc_html__('Close', 'soma') ?>
<?php get_sidebar('shop'); ?>
</div>
</div>
</div>
</div>
This is one is still tough to assist on without seeing the full source of Soma, which isn't open-source.
However, if you simply need to customize the template used to display a single category, and there isn't a category.php template in the theme yet, this is the procedure:
1. Create a custom template for single categories
Referring to the WordPress Template Hierarchy, you'll want to create a custom template called category.php
If you want the layout for this page to be similar to another existing template, simply copy that template's code into category.php to begin.
2. Copy relevant sidebar code into your new category.php template
Add the sidebar code you've posted into your new category.php template, wherever it belongs in the layout.
If you're not sure where to put it, or you'd like to include a sidebar template in the same way that the Shop and Categories pages do:
look at code of those templates where the sidebar does currently appear
find the place where they include the sidebar template
do the same thing in your new category.php file

How to display the content of a blog post using a custom post template?

I am using my own custom WordPress theme and I am running into trouble displaying the content of blog posts. I can display the title and the date it was published using php but I can't get any of the paragraphs, images, headings, etc. to display on the page. I am using Gutenberg blocks (default) for the content of the blog posts.
I have tried using php functions to grab the content but they don't seem to be working.
<div class="col-md-6 col-md-offset-3">
<p class="date"><span class="glyphicon glyphicon-time">
</span> <?php echo get_the_date();?></p><br />
<p><?php $content = apply_filters('the_content', $post-
>post_content);?></p>
</div>
I am expecting the content of the post to display within the div container but the function is not grabbing the content. Any help would be appreciated!
It sounds like you may be trying to retrieve the post content from outside the loop.
If you look at the post template for a theme e.g. 2017, it is this bit that does the magic. It’s not even necessary to pass a post ID:
<?php
while ( have_posts() ) : the_post();
get_template_part( 'components/page/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
E.g. you should just be able to do:
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile; // End of the loop.
?>
Might be a good idea to start with the code on the link above, or copy the single.php file for the theme you’re using and use that as the basis for your custom post page?

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 display page contents and a loop?

I have a custom loop in a page template to display posts by category by give category and tag. It works, but above the loop I need to show the content of the page itself, i.e. the content that's been entered into the Wordpress Dashboard normally.
What do I add to my template to display this content?
I have tried:
$id = $post->ID;
get_page($id);
// then my custom loop
Which does get the current page ID, but no content.
In WordPress, calling
<?php the_content(); ?>
will pull in the content from the WYSIWYG editor on the page that is using that template as long as it is inside the loop.
The most basic example of this might look like this:
<?php while (have_posts()) : the_post();/* Start loop */ ?>
<?php the_content(); ?>
<?php endwhile; /* End loop */ ?>
<!-- Enter your custom loop for displaying posts by category down here -->
More info here: http://codex.wordpress.org/Function_Reference/the_content
In my case, with a modern theme and using Gutenberg blocks, I had to apply filters to the content before the posts loop, as mentioned in this thread here: Proper way to get page content
Following one of the examples, a simple working solution would be:
<?php
$id = 669; // page ID that has been assigned for posts
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
<!-- Enter your custom loop for displaying posts by category down here -->
To display the content of the page, using the the_content() function.
Added your custom loop after this function call.

Categories