Wordpress Custom Pages or Something Else?

2019-08-30 19:05发布

问题:

I am creating a website which is not a blog, and I am using wordpress for my CMS - in short I am not even using the wordpress loop.

The website will display graphs and data from someone else's API and such but I am stuck on what seems to be the right way to do this, and all the documentation I find is for blogs or very very vague.

I want to be able to create pages, using just an import statement not a custom template - since those seem to be for text only, and not the huge php, ajax, mysql backend I plan to slap behind these pages.

For example:

// index.php

<?php get_header(); ?>

// Some file, like index_content.php imported into here
<?php get_custom_page_content(); ?>

<?php get_footer(); ?>


// about.php

<?php get_header(); ?>

// Some file, like about_content.php imported into here
<?php get_custom_page_content(); ?>

<?php get_footer(); ?>

It seems very conflicting because if I just make a new .php file like I explain below, it detracts from the CMS all together. I just edit them myself without even logging into the admin panel, since I see nothing to manage those files for me without directly linking from my menu items or whatever. Why wouldn't I then just @include everything and get rid of Wordpress completely. I must be missing something here.

The long and short of it is, I have no idea how to structure this and from my attempts to research, no idea what I am looking for to answer this.

How do I go about creating the above scenario, or is it as simple as just creating a new .php file for each named page and @including what I want?

I don't want to get into bad practices.

回答1:

1) You can use WordPress default Page Templates. You'll have the files page-index.php and page-about.php and they will appear as templates in the attributes meta box.

2) Or you can rely in custom meta fields and boxes, making your own personal meta box just like the default Attributes. This is done with add_meta_box, get_post_meta and update_post_meta. Check the following results in WordPress Answers.
But, long story short, even knowing how to use those, I prefer to use Advanced Custom Fields to handle that, as it's a well maintained and feature rich plugin.

With a custom meta box in place, simply choose from a pre-configured dropdown list (index, about) and in your page.php theme file use:

global $post;
$include = get_post_meta( $post->ID, 'include_file', true );
get_template_part( 'content', $include );

include_file is the custom field that contains the dropdown selection. And you handle it yourself with update_post_meta or in a field created with ACF.

get_template_path() is a bundled WP function and in this example will look for the file example.com/wp-content/themes/YOUR-THEME/content-index.php or example.com/wp-content/themes/YOUR-THEME/content-about.php.


Method 1 is straight forward. Method 2 enables much more details to be fine-tuned. A combination of both can be used.