Is it possible to echo a value from the active post in wordpress?
Example: If I make a row called: post_numberofreads and I want to echo this value for each post, by inserting a line of code in the single post template page (single.php) that calls any given posts 'post_numberofreads'. How would I go about doing this?
Please notice that this is an example. The important part is that I want to echo something from the active row - not just get the number of reads.
Thanks for your time.
While the ideal solution is to create a wordpress plugin with your custom functionalities but it require good understanding of wordpress and plugin development.
The other solution more can be to inject your custom code in the wordpress core manually. I is comparatbiliy easy and often unsecure and get broke frequently.
For second option you can:
A) Create required custom columns in wp-posts in wordpress database using phpmyadmin or your favourite tool.
B) Inject your code into the relevant files. Generally these files are:
wp-content/themes/your_theme/single.php #For blog post
wp-content/themes/your_theme/page.php #For pages
C) Then you can using built in wordpress functions like get_the_ID to get current post ID. You can also other wordpress functions to communicate with wordpress database or write your own functions.
Related
I'm new to web building and programming and what not, but I'm trying to create a website with a lot of pages. Note that this is pages not posts. The pages will use the same layout, but the content will be different. It's very inefficient to create the pages in Wordpress. I was wondering where does Wordpress store the database that is used to generate the pages. I figured I'll just add information to the data base - which would be much for efficient.
Any ideas on how to do this? Or other ways to create many pages efficiently. I would love to be able to work with Excel or some other text software, rather than the slow Wordpress platform.
Thanks!
All content are saved to the wp_posts table in your WordPress database. whether its for pages or as posts. Databases are provided by your webhost. Edit: Excel is not a web authoring program, nor should it be used as one.
You should put your header in header.php and footer in footer.php, and main content if it's not post type in page.php or you can make you custom page template, also if you are use page and not post, don't forget change it from settings
You can't use Excel for your data. You can only use the Wordpress Database or external files.
As stated above, pages are stored in the posts table.
Since your data isn't actually dynamic, you should probably go ahead and create individual pages and create a template to display the pages the way that you want them to look.
https://developer.wordpress.org/themes/template-files-section/page-template-files/
For the record, you can use a single page to output different content. Most shopping carts, for example, are displayed using a page called 'Cart' (or something similar), but the output is different. The Events Manager plugin outputs events to a page called 'Events'. This, however, is for truly dynamic data and not what you are looking for.
I have 1 custom table (not wp core table) where are specific products imported. I've set up custom wp page where these products are listed, custom filter created etc. In this step so far everything is OK - products are listed and filtering works, pagination works...
But what I want to extend further now - open each product page (like /product-listing-page/product-name-1) when clicking on product names in product list page... How can be this achieved "out of wp_posts" table?
Why I'm not using wp_posts table: web site have static pages and dynamic posts as news and product table is truncated and re-imported once in a day every night! So, can not really use wp_posts page when product list is dynamic and totally truncated before each import...
Any ideas? Maybe anybody already did such stuff?;-)
I don't know if you developed a plugin to handle all the custom table and code you created, but if you didn't, I strongly encourage you to do so.
Because by making your code as a plugin, you have the oportunity to use all the composants of wordpress, like the rewrite rules system, which, I think, could help you to achieve your goal.
I created a plugin some times ago for a website I have the charge and I needed a custom page to be displayed within the website (worpdress) url system, which is what you want to do if I correctly understand your question.
Unfortunately the plugin has evolved since and I didn't keep a backup of that code, but if I remember correctly how I did it back when, here is the rough approach I followed:
I create a plugin which handle :
custom db table(s)
custom php objects
...
AND (this is the intersting part) add rewrite rule to wordpress rewrite rules system as follow:
set rewrite tag (init action) (if needed)
add rewrite rule (init action)
intercept url and parse it to get the params i need in my custom
page (parse_request action)
The following functions may help you with that :
add_rewrite_tag
add_rewrite_rule
And you need to hook in :
init
parse_request
Hope it helps you start.
the solution was quite quick and easy! For my needs it was enough just to register global query var:
function register_new_query_var($vars) {
$vars[] = 'product_id';
return $vars;
add_filter('query_vars', 'register_new_query_var');
and then I changed theme template for page product-listing-page to work for product listing and individual products with using
if(!get_query_var('product_id')). So, if query empty - do listing stuff, if not empty - do individual product stuff!
For me this is quite enough and I do not need even seo friendly urls (because product list changes every day, so - every day will be new products, links, and there will be to much 404 errors)...
and I can use http://example.com/product-listing-page?product_id=1 and page displays with date which is from custom table! ;-)
After providing an event calender with PHP/MySQL/HTML/CSS I want to use Wordpress for that project from now on. Account and data management will remain outside of wordpress scope.
My best idea so far is to populate the posts (or pages) entirely from the already existing db. The URLs would be created by apache's rewrite engine.
To use one post and populate just the content would lead to really bad SEO.
So I want to create (virtually) unique posts populated entirely from the db. Extensive research on that topic brought up nothing usable so far. Maybe someone could point me in the right direction?
Preferences:
using mod rewrite to create the pages urls from event /ID/title rather than using the timestamps to create posts
keeping the data editable via external admin panel (no copying into WP-DB)
head data of post / page must be populated from db, e.g. title, author, timestamp etc.
Suggestions would be much appreciated, thanks in advance.
Have you tried the plugin WP All import. It creates unique post such are car listing etc based on the external DB. I also allows you to style and format your post.
https://wordpress.org/plugins/wp-all-import/
I wrote a plugin for WordPress which has a few user-configurable settings that are stored using WordPress's suggested method. I know they are saved in the wp_options table, but that is abstracted by the WordPress options API.
Now I'm trying to add a "global override" of the settings that can be configured in the Network Admin section of a multi-site installation. I found the appropriate hooks to design my settings page, however I can't find any info about where to save the data.
If I save it using the normal options API, then the settings get saved individually for each site. I'm looking for a place to save them globally for all sites, so the plugin can first look to see if the settings have been globally overridden by the server admin.
I can just write some code to write directly to the wp_options table of one of the sites (for example site #1) or even create my own table. I know how to do all of these things, but I don't want to do that if there's a preferred way to write mult-site plugins.
Thanks for any advice.
As you know the main deference between developing a single WordPress plugin vs. a multisite plugin is where data is written. According to this article since plugins normally have complete reign over one database, and in some cases the plugins local pages files you must be sure to only call the $wpdb global object instead of a hard-coded reference to a table.
For example, if you need to execute an SQL command on the posts table, then do not use wp_posts as the table name $post_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM wp_posts"));
use this
$post_count = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts"));
Please see the article for further info.
Ok so i have this custom blog that i now need to move over to wordpress. I tried using the import features in wordpress but that didnt work as planned. The custom blog is written in php and has a userscomments table, authors table, and posts table. Is there an easy way to move all these posts over to wordpress and preserve all the comments per post.
If the custom blog doesn't have an export function that will dump its data into a format WordPress can read, then it may be that you'll have to roll your own. There are 2 approaches:
Do a database-only import, using code or SQL tools along with information from the WordPress Codex entry on the WP database. This will probably be fairly simple from a coding standpoint, but you have to make sure you get it right.
Use the WordPress API to create the posts based on the output from the old system. This will involve a bit more learning, but it's the Right Way™ to do it, and means that you learn the WordPress API rather than the database structure.
If I were doing it myself, I'd choose option #2.