Wordpress get_template_part based upon custom field - php

I have a WordPress theme with 100s of different template parts - ideally I'd like to be able to specify which template part to load based upon the value of a post's custom field. i.e. if post's field value is ABC then get_template_part ABC-template.php. I'm using Advanced Custom Fields to specify the fields.
To get the field value you normally use:
<?php the_field('field_name'); ?>
I've tried something along the lines of
<?php $var = get_post_meta($post->ID, 'field_name', true);
if ($var == '')
{ }
else { get_template_part( "subdirectory/' . $var . '", "form" );} ?>
but this didn't work. Any ideas on retrieving the custom field value and then loading the relevant template part using get_template_part?
I'm aware I could in theory achieve this using the in_category function - but thought that having to cycle through 100's of "if post in_category ABC load ABC template, if post in_category DEF load DEF template" each time a post loads might slow the website down a bit?
Thank you so much in advance, really appreciate any help you can offer :)
Edit: I've since worked it out (trial and error, I'm rubbish at PHP!)
You need the following:
<?php
$fieldname = get_field('fieldname');
get_template_part('subdirectory/' . $fieldname ); ?>
If you have ABC-DEF stored in the custom field, this will then load the template in subdirectory/ABC-DEF.php
Hope this helps someone :)

Related

How to write a shortcode with variable to display content from specific custom post type post?

I've only written very basic shortcodes without variables and am just not figuring out how to write one that allows someone to enter a variable to pull info from a specific post.
I have a custom post type called "Events". I want to put a shortcode on the site FrontPage that will display the contents of a specific event within that custom post type. I'm assuming a variable is the way to do this, but I don't know what the best way is. Should I be having the user find the post ID number to use as the variable? Note that I am not trying to display an arcive of the post type, only the contents of a specific post, as indicated by the shortcode variable. I can see something like the following, but don't know how to achieve it:
[display-event id="77"]
Really, this is advanced for me, so any direction you can give me would be much appreciated.
~Laura
Have a look at WordPress's documentation for add_shortcode().
You would need to add something like the following in your functions.php file:-
function baztag_func( $atts, $content = "" ) {
// What you would like your short code to do
return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );
add_shortcode

will do_shortcode() allow me to run shortcode in content before a line in the template?

G'day folks, I'm attempting to integrate Ajax Availability Calendar into a WordPress site via PHP.
I have multiple calendars set up, and need to display a different one on each property page.
The problem I'm having is that I need to be able to specify the ID e.g. $_GET["id_item"]=2 of the calendar before the <?php require_once 'pathtofile' ?> statement which is in the template.
The result I need to achieve is...
<?php $_GET["id_item"]=2; require_once 'path\to\required.file'; ?>
If I put this code in the template with the appropriate ID, it works, but I need the ID number to change on each page, otherwise I'll always be displaying the calendar with ID number 2.
I currently have custom shortcode (below) with which I can specify the ID in the page content, but I am at a loss as to how to access that ID before the statement in the template.
So what I currently have is...
In Functions.php
function ajaxcalendar_shortcode($atts) {
$args = shortcode_atts(
array(
'id_item' => 1
),
$atts
);
return '<?php $_GET["id_item"]='.$id_item.'; ?>';
}
add_shortcode('cal_display', 'ajaxcalendar_shortcode');
with the shortcode [cal_display id_item = "2"] in the content.
Is it even possible to do this or should I try another approach?
My research suggests that I may be able to use do_shortcode() to achieve this, but I haven't figured out how to use it in this situation, or I may be misinterpreting it's purpose.
I've tried putting this in the page template file:
<?php $content = the_content(); echo do_shortcode($the_content); ?>
<?php require_once 'C:\xampp\htdocs\MooreRiver\availability\ac-includes\cal.inc.php'; ?>
But it continues to display the calendar with ID 1.
Any tips would be appreciated :)

Show full post (not excerpt) for defined posts in wordpress

I'd like to have the possibility to define some posts in wordpress where the full post is shown and not an excerpt.
There are various solutions like:
Use a hidden html to declare this and code into the theme to either use the_content or the_excerpt
Hardcode into the theme (if postid == xx then the_content else the_excerpt)
Use post meta data and add "if" into theme to check for them
Create a plugin which adds the functionality automatically and also a checkbox "Always show full" into the post-editor.
The first one is easy but ugly, 2nd one should be doable with some googling but the 3rd one seems to be the most appealing to me but I have no idea how to achieve this.
Since usually in the template all i have is somewhere the_excerpt method from wordpress. I therefore should somehow inject some code there and check if the checkbox for the current post is set and then just use the_content instead. Is this possible at all or do I need to modify the theme anyway?
Thanks for your inputs.
I would use custom fields, it's more flexible, so you don't need to hard code the page ids.
<?php
$show_full_content = get_post_meta($post->ID, 'show_full_content', true);
if ($show_full_content == 'yes') {
the_content();
} else {
the_excerpt();
}
?>
You might be interested in ACF, it can make it more user friendly.

how to display custom post type post's title on page

I am trying something out on wp. I want to display custom post types title on page post type.
here is my code.
function get_post_type_title(){
if('movie_ronny' == get_post_type()){ // if post type is movie_ronny, get post title
$movie = get_the_title(); //hold the post title in $movie.
}
if(is_page() ){ // if viewing page, display movie_ronny title
echo $movie;
}
}
add_filter('wp_head','get_post_type_title');
Above code does not display the title of movie post-type when viewing page. Any help is highly appeciated.
Your post type will most likely be shown on its own single-movie_ronny template. In which case of course you wouldn't need a conditional -
if('movie_ronny' == get_post_type()){...}
The other reason why this question is a bit strange is that your loop query must define within it's arguments what type of post, page or custom post type it is querying. So you would also then know which post type you are messing with and not need the first conditional.
And no matter what your situation is:
if(is_page() ){ // will only return true for pages. not posts or CPT's
Also,
get_the_title($id); // needs a post ID outside of the loop.
Well, there are two ways this function will fail (i.e. do nothing):
If the first if block is ignored then the second if block will not do anything (I believe it will actually show a notice, because $movie would not be defined).
If is_page() does not evaluate to true then nothing will be done in any case.
If you know anything about programming then this should be obvious. So, either you have no idea what you are doing, or I am misunderstanding something here.
Try this one:
<?php
$post_type = get_post_type_object( get_post_type($post) );
echo $post_type->label ;
?>

How do I add a custom key to PHP in Wordpress

I am attempting to make a custom query in Wordpress. I want to have it pull up any posts that are tagged relevant to the page that people are on. IE - If they are on the Bay 198 Skatepark page, they also see a list of all Bay 198 Skatepark posts.
I've gotten the custom query to work, but I need this to be dynamic so that if you are on the Bay 198 page, you see those posts, but if you are on the Precision Skateboards page, you see those specific posts.
The easiest way I could think to accomplish this was to create a custom field on each page that would be the same as the tag and that way I would just fill in the tag for each page as a custom field and it would return the posts.
The issue is that I can't figure out how to implement the custom key call into the custom query. Here's my code so far, this one being the one that works:
<?php $second_query = new WP_Query(' tag=bay-198&posts_per_page=500 '); while( $second_query->have_posts() ) : $second_query->the_post(); ?>
I just need to figure out how to replace that "tag=bay-198" and have it dynamically pull in the tag based on the page.
Hopefully that makes sense, I appreciate any help.
You can use a variable that containts the value of your custom field. Then add that variable into the query like so:
<?php
$mykey_values = get_post_custom_values('my_key');
$tag = $mykey_values['your-custom-field'];
$second_query = new WP_Query(' tag=' . $tag . '&posts_per_page=500 '); while( $second_query->have_posts() ) : $second_query->the_post(); ?>

Categories