Creating custom page with data from database in Wordpress - php

I'm trying to create a wordpress website with some information on the page with displaying data from the database. So, I have the home page which has some information to display. I want to display the data right below this information on the home page. So, I performed the following steps:
1) Created a block on the new page and enter the information for the home page
2) Inserted data on the database
3) Created a publications.php page and saved it on the themes folder as Publication template
4) Installed InsertPages plugin on wordpress
5) Created a new page with title and template set as Publication
6) Then, added a new block below the block from 1) in the Home page
The code looks like below:
<?php
/**
* Template Name: Publications
* The template used for displaying publications
*/
?>
<div class="pubications">
<ul class="collection">
<?php
global $wpdb;
$publications = $wpdb->get_results("SELECT * FROM `cidhi_publications` ORDER by Year DESC LIMIT 12;");
<div class="content">
foreach($publications as $publication){
echo "<li class='collection-item avatar'><a href='https://doi.org/".$publication->doi."'>";
echo "<img src='/cidhi/wp-content/uploads/2019/11/".$publication->image_name."' alt='' class='circle'>";
echo "<span class='title'>".$publication->Title."</span>";
echo "<p>".$publication->Authors."</p>";
echo "<a href='#!' class='secondary-content'>".$publication->Year."</a>";
echo "</a></li>";
}
</div>
?>
</ul>
</div>
If I only open the page as follows: http://localhost://wp/publications, it shows the data but without the theme formatting but only as plain HTML.
But, if I open the main home page, where this page is inserted to the home page, I only see the title not the content. I also checked the settings in "InsertPages" Plugin and made sure that "Display" section has "All" selected
This is the first time I'm working on wordpress so any direction can be very helpful. I"m using the most recent version of wordpress

Your Publications template needs to call the WordPress header and footer files.
So, above your HTML, add:
<?php get_header(); ?>
And below it, add:
<?php get_footer(); ?>

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.

Can't see php-content from custom template in WordPress

I'm trying to create a custom template for a small wordpress site, the template should display some data from the database.
But I can't get PHP to output anything on the pages, I've written a few echo's just to get something but can't see anything.
I can, however, add the page through "Pages" in WordPress, that works just fine and displays an empty page (or with text if I write something in the text box)
To set up the template I created a child template of my original template, the CSS file in the child template works just fine.
I added the custom page called "petitionlist.php" in the root of my child-theme, the code I added for testing is as following
<?php
/*
Template Name: petitionlist
*/
get_header();
echo "test test test";
$test = "testtetsttest";
echo "<h1> this is a test</h1>".$test; ?>
<h1>Hello wordl!</h1>
<?php get_footer(); ?>
Attached is a screenshot of my folder-structure, the petitionlist.php file in wechange-child is the file I'm working on.

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

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.

Manually insert a WordPress plugin inside custom theme's header.php file

All right, so I have an issue I'm trying to work through involving WordPress plugins. I have found a gallery plugin that I like--has pretty much everything I need--but the problem is that placing the shortcode in the content editor causes it to be placed below a banner image that I have (this banner image is stored in my custom theme's header.php file and it has to go there because of the layout of the site I'm working on).
What I want to do is call the plugin inside my header.php before this banner image...but I am having trouble figuring out what I need to do in order to accomplish this. I read about the need to potentially remove the register_activation_hook or activate_pluginname actions when I manually call the plugin in my header.php file...do I need to somehow incorporate these or create functionality similar in my header.php file?
This is what I'm aiming for in my header.php file (I am only looking to load this gallery plugin for the home page, hence the check to see if the page id is the home page id):
<div id="Container_Body" class="clearfix">
<div id="topbox">
<?php
$id = get_the_ID();
$hpid = get_option('page_on_front');
if($id == $hpid)
{
code to display plugin
}
?>
<div class="GalleryPlugin">
<?php code to display gallery plugin ?>
</div>
<div class="giantBanner" style="background: rgb(0, 141, 175);">
<img title="Main-Header-Banner" src="<?php bloginfo('template_directory'); ?>/images/Market-Crowd.jpg" alt="Market Crowd">
</div>
</div>
Check this
<?php
if(is_home() || is_front_page()) // this will check for homepage its a wp check
{
// echo the plugin shortcode here like
echo do_shortcode('[gallery]'); // this will be any shortcode
}
?>

Categories