I've recently started using Wordpress PODS plugin and I'm having a little trouble displaying some basic content. I can display custom fields content just fine using the following:
<?php
$field_name = get_post_meta( get_the_ID(), ‘my_field_name’, true );
echo $field_name; ?>
However I can't get basic stuff such as the following:
title (which in regular posts it’s just the_title();)
content (which in regular posts it’s just the_content();)
featured image
Can someone please help me figure out how to pull the title, content and featured image from a POD?
pod-type-slug should be replaced by the slug of your pod type. The pod-slug is the slug of the specific pod content you created:
//Pods->display has been deprecated since Pods version 2.0 with no alternative available according to a warning when trying to use this function:
$pod = pods('pod-type-slug', "pod-slug");
$pod->display('title')
$pod->display('post_content')
etc.
You can also use the default WordPress functionality:
$pod = pods('pod-type-slug', "pod-slug");
$row = $pod->row();
//print the pod content:
echo $row->post_content;
get_the_post_thumbnail($row['ID']);
After a frustrating hour of Googling and looking through their examples, there didn't seem to be a single source of how to get a Pod's content or custom field. The answer above helped me a bit, but wasn't well explained.
In your wp-content/plugins folder, create a new folder called podutils and create a php file called PodUtils.php.
Copy/paste this class into that file:
<?php
//Class to get WordPress Pod data:
class PodUtils {
//get WordPress Pod Object:
public static function GetPodObject($podType, $podSlug) {
//for use with Pods WP Plugin (https://pods.io/):
if(function_exists("pods")) {
$pod = pods($podType, $podSlug)->row();
return $pod;
} else {
return false;
}
}
//get the content from a WordPress Pod:
public static function GetPodContent($podType, $podSlug) {
$pod = PodUtils::GetPodObject($podType, $podSlug);
if($pod !== false) {
return $pod["post_content"];
} else {
return false;
}
}
//get a custom field from a WordPress Pod:
public static function GetPodMeta($podType, $podSlug, $metaName, $isSingle = true) {
$pod = PodUtils::GetPodObject($podType, $podSlug);
if($pod !== false) {
return get_post_meta($pod["ID"], $metaName, $isSingle);
} else {
return false;
}
}
}
To use the class with static methods, do so as follows:
Include the PodUtils.php file in the php file you want to use it:
require_once ABSPATH . 'wp-content/plugins/podutils/PodUtils.php';
Get content:
$strPodContent = PodUtils::GetPodContent('pod-type', 'pod-item-slug');
Get meta (custom field you added to a pod type):
$strPodMeta = PodUtils::GetPodMeta('pod-type', 'pod-item-slug', 'custom-meta-name');
This can easily be changed to an object that you instantiate with public functions, or a plugin of its own.
Related
I'm coding a WordPress website in which I want all the external links to be "no-follow" by default.
It is quite long to go and edit each single link to add the "no-follow", is there a way to code it once for all?
Thank you
IMHO, the best way to do so is using a custom filter. Being generated dinamically by PHP, you can edit links on post and pages server-side even before a user visit them: WordPress used to add rel="nofollow" to each link by default in the past, but I see it doesn’t happen anymore. Then, I found a solution by Debjit Saha you may try on functions.php.
add_filter('the_content', 'my_nofollow');
add_filter('the_excerpt', 'my_nofollow');
function my_nofollow($content) {
return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
}
function my_nofollow_callback($matches) {
$link = $matches[0];
$site_link = get_bloginfo('url');
if (strpos($link, 'rel') === false) {
$link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
} elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
$link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
}
return $link;
}
Please, notice that some plugins could break this code and I’ve never tried this on newer WordPress versions.
One way you can do it is using JavaScript:
var hrefToCheck = "mysite.com" // change this string
noFollowExternalLinks(hrefToCheck); // call to run the function below
function noFollowExternalLinks(siteHref) {
document.querySelectorAll('a').forEach(function(link) {
if (!link.href.includes(siteHref)) {
link.setAttribute("rel", "nofollow");
}
})
}
So, you can change hrefToCheck variable to a piece of your site URL that will be checked inside each link inside the page.
The function will loop to check every link in the page and apply rel="nofollow" to all external links (that don't match the text in the variable).
I use wp_set_object_terms to set terms for user custom taxonomy, this function only works from the plugin main php file, but not from included php file.
for example this works wp-content/plugins/my_lugin/my_plugin.php:
function set_user_term_a()
{
$user_professions = Array('aaa','bbb');
wp_set_object_terms(46, $user_professions, 'user_profession', false);
clean_object_term_cache(46, 'user_profession');
}
add_action('init', 'set_user_term_a');
but if function included from another php it not work
wp-content/plugins/my_lugin/included_file.php:
echo "the file was successfully included"; // ok
function set_user_term_b()
{
$user_professions = Array('aaa','bbb');
wp_set_object_terms(46, $user_professions, 'user_profession', false);
clean_object_term_cache(46, 'user_profession');
}
add_action('init', 'set_user_term_b');
wp-content/plugins/my_lugin/my_plugin.php:
include_once("included_file.php");
Any ideas what wrong?
I have correctly created a custom widget, evreything is translating well with a correct .po file, except the title.
Here is my code :
$concert_widget_name = __('Tour Dates', 'concerts');
wp_register_sidebar_widget (
'tourdates', // your unique widget id
$concert_widget_name, // widget name
'tourdates_widget_display', // callback function to display widget
array( // options
'description' => 'Displaying upcoming tour dates'
)
);
Is there an error ? An other way to translate the widget name ?
I usually register my widgets using the register_widget function. In the constructor of the widget class I place the following code:
class TourDates extends WP_Widget
{
public function __construct()
{
$options = array('classname' => 'tour-dates', 'description' => __('Display upcoming tour dates'));
parent::__construct('tour_dates', __('Tour Dates'), $options);
}
}
You can also check out the Widgets API on the WordPress Codex site. Hopefully this helps you in creating your custom widget.
Also what I usually do is merge my translations with the default ones loaded from WordPress, like so:
function loadTextDomain() {
$locale = get_locale();
$languageDir = dirname(__FILE__) . '/languages';
$domain = 'default';
$mofile = $languageDir . '/theme.' . $locale . '.mo';
global $l10n;
$mo = new MO();
if (!$mo->import_from_file($mofile)) {
return false;
}
if (isset($l10n[$domain]) && !empty($l10n[$domain]->entries)) {
$l10n[$domain]->merge_with($mo);
} else {
$l10n[$domain] = $mo;
}
}
add_action('init', 'loadTextDomain');
This code looks similar to the load_textdomain function from WordPress but it avoids all the filters that do exist in the original function, which helps in avoiding any WordPress hook from altering your $domain and $mofile variables.
But I will leave that up to you. Maybe the load_textdomain() function from WordPress will work just as fine, but in case it doesn't this function should do the trick.
Now if your using the loadTextDomain() function I pasted above you can just place a languages folder in the same folder as your functions.php resides, in that new folder you could place theme.nl_NL.mo or theme.de_DE.mo files depending on the language your using. This should allow translation of your website and also the admin area.
In the template.php file I inserted the code below:
I found a tutorial online that gives the code, but I'm confused on how to get it to work.
I copied the code below and inserted it into the template.php from the theme HTML5_base.
I duplicated the page.tpl.php file and created custom pages -- page-gallery.tpl.php and page-articles.tpl.php. I inserted some text to the files just see that I've navigated to the pages w/ the changes. It looks like Drupal is not recognizing gallery.tpl.php and page-articles.tpl.php.
In the template.php there are the following functions:
html5_base_preprocess_page()
html5_base_preprocess_node()
html5_base_preprocess_block()
In the tutorial it uses these functions:
phptemplate_preprocess_page()
phptemplate_preprocess_block()
phptemplate_preprocess_node()
function phptemplate_preprocess_page(&$vars)
{
//code block from the Drupal handbook
//the path module is required and must be activated
if(module_exists('path'))
{
//gets the "clean" URL of the current page
$alias = drupal_get_path_alias($_GET['q']);
$suggestions = array();
$template_filename = 'page';
foreach(explode('/', $alias) as $path_part)
{
$template_filename = $template_filename.'-'.$path_part;
$suggestions[] = $template_filename;
}
$vars['template_files'] = $suggestions;
}
}
function phptemplate_preprocess_node(&$vars)
{
//default template suggestions for all nodes
$vars['template_files'] = array();
$vars['template_files'][] = 'node';
//individual node being displayed
if($vars['page'])
{
$vars['template_files'][] = 'node-page';
$vars['template_files'][] = 'node-'.$vars['node']->type.'-page';
$vars['template_files'][] = 'node-'.$vars['node']->nid.'-page';
}
//multiple nodes being displayed on one page in either teaser
//or full view
else
{
//template suggestions for nodes in general
$vars['template_files'][] = 'node-'.$vars['node']->type;
$vars['template_files'][] = 'node-'.$vars['node']->nid;
//template suggestions for nodes in teaser view
//more granular control
if($vars['teaser'])
{
$vars['template_files'][] = 'node-'.$vars['node']->type.'-teaser';
$vars['template_files'][] = 'node-'.$vars['node']->nid.'-teaser';
}
}
}
function phptemplate_preprocess_block(&$vars)
{
//the "cleaned-up" block title to be used for suggestion file name
$subject = str_replace(" ", "-", strtolower($vars['block']->subject));
$vars['template_files'] = array('block', 'block-'.$vars['block']->delta, 'block-'.$subject);
}
Ah! That's a lot of pain to do this.
Why don't you try the Ctools and Panels modules.
Ctools has a lot of useful modules, the one you'd make good use of is called "Page Manager". It will allow you to create custom pages of your choice, and apply custom layouts using the Panels module.
Hope this simplifys things.
If I have a custom post type named 'my_type', how can I get Wordpress to make date-based archives, for example:
mysite.com/my_type/2010/
mysite.com/my_type/2010/07/
mysite.com/my_type/2010/07/28/
I'm looking for tips both on creating the rewrite rules and on linking the urls to templates.
Thanks!
Update:
I've tried the following in my functions.php (EVENT_TYPE_SLUG is a constant defined elsewhere):
function event_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
EVENT_TYPE_SLUG."/([0-9]{4})/([0-9]{1,2})/$" => 'index.php?post_type=event&year='.$wp_rewrite->preg_index(1).'&monthnum='.$wp_rewrite->preg_index(2),
EVENT_TYPE_SLUG."/([0-9]{4})/$" => 'index.php?post_type=event&year='.$wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'event_rewrite_rules');
the rewrite rules show up in $wp_rewrite-> rules, but when I navigate to those pages I get a 404 error. If I manually navigate to mysite.com/index.php?post_type=event&year=2010, I get redirected to mysite.com/2010?post_type=event
Benj I think WordPress automatically creates archives for custom post type
OK ... took some time but I figured this out (there may be more elegant solutions out there).
1) Create a standard wordpress page to serve as the archive page (and to give me access to the template). I defined the page's slug as a constant (EVENT_ARCHIVE_BASE) so that it's just hard-coded in one place and referenced everywhere else.
2) Make custom rewrite rules that catch that page's urls and redirect them to the page:
function event_date_queryvars($qvars) {
array_push($qvars, 'eyear', 'emonth');
return $qvars;
}
add_filter('query_vars', 'event_date_queryvars');
function event_date_rewrite_rules() {
// Adds rewrite rules for date-based event archives
global $wp_rewrite;
$new_rules = array(
EVENT_ARCHIVE_BASE."/([0-9]{4})/([0-9]{1,2})/?$" =>
"index.php?pagename=".EVENT_ARCHIVE_BASE."&eyear=".$wp_rewrite->preg_index(1)."&emonth=".$wp_rewrite->preg_index(2),
EVENT_ARCHIVE_BASE."/([0-9]{4})/?$" => "index.php?pagename=".EVENT_ARCHIVE_BASE."&eyear=".$wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'event_date_rewrite_rules');
3) At the top of my page template, I check for the custom query vars and build the page accordingly. (I should have been able to use the built-in year and monthnum, but couldn't get them to work properly. Future troubleshooting):
// Was the page called with a specific year, month, or just plain?
$year = get_query_var('eyear');
$monthnum = sprintf('%02d', get_query_var('emonth'));
if ($monthnum) {
$list_type = 'Month';
$monthname = $month[$monthnum];
$heading = "Events from $monthname, $year";
} elseif ($year) {
$list_type = 'Year';
$heading = "Events from $year";
} else {
$list_type = 'AllPast';
$heading = get_the_title();
}
Thanks for the help, and hope this is useful for someone else! Let me know if you have a simpler/built-in way to do this.
I managed to find a more elegant/built-in solution.
The trick is in the "post_type=article" parameter.
If you create a rewrite like so:
'(articles)/([0-9]{4})' => 'index.php?post_type=article&eyear=' . $wp_rewrite->preg_index(2)
This will then keep your URL structure, but go through the default 'archive' template.
Then you steal the template away from the default to your own archive template. (for instance 'archive-articles.php')
This function accomplishes this (works for me):
add_action("template_redirect", 'my_template_redirect');
// Template selection
function my_template_redirect()
{
global $wp;
global $wp_query;
if ($wp->query_vars["post_type"] == "article")
{
if (have_posts())
{
include(TEMPLATEPATH . '/archive-articles.php');
die();
}
else
{
$wp_query->is_404 = true;
}
}
}
You'll still need to create your own methods to handle the get_query_var()'s for the year/month/day, but at least you don't need to create a custom page template.