Wordpress Custom Post Template - php

I'm trying to get Wordpress to include a different template file, within single.php, if it sees a file matching the slug.
The file does exits, path is correct, safe_mode set to OFF...am I missing something?
$dir=get_bloginfo('stylesheet_directory').'/post_tmpl/';
$categories=get_categories();
foreach($categories as $cat){
if(is_file($dir.$cat->slug.".php")){
require($dir.$cat->slug.".php");
}else{
require($dir."default.php");
}
}

can you try with this code?
$dir=get_bloginfo('stylesheet_directory').'/post_tmpl/';
$categories=get_categories();
foreach($categories as $cat){
$temp = $dir.$cat->slug;
if(is_file($temp.".php")){
require($temp.".php");
}else{
require($dir."default.php");
}
}
Thanks

Related

Wordpress 4.5.3: get content with applied template file?

Is there a way to get the content of an entire wordpress page? My problem is, I want to include page content on another page (to create a one-page layout). What I tried was this:
$post = get_post($the_page_id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
But the page has its own template and I would like to display the entire page, including whatever is done in the template.php file. Is that possible?
Well, I came up with a solution for this. It might not be the best way but this worked for me. I created a page "home" to represent the one-page page. All subpages of this page will have a certain template which does not include header or footer. My home template looks like this:
//fetch header as usual
get_header();
//fetch all subpages of this page
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'ASC'));
$children = get_page_children( get_the_ID(), $all_wp_pages );
//browse these subpages and get the content for each one
foreach($children as $child){
$post = get_post($child->ID);
getOnePageContent($post);
}
//footer
get_footer();
Now in functions.php I defined the getOnePageContent:
function getOnePageContent($page){
global $post;
$post = $page;
$slug = get_page_template_slug( $page->ID );
if($slug){
$pl = get_the_permalink($page);
$content = file_get_contents($pl);
echo $content;
} else {
$content = apply_filters('the_content', $page->post_content);
echo $content;
}
wp_reset_postdata();
}
As long as the subpages do not contain headers or footers this works fine. Of course, I do not think it's an elegant or good solution to fetch the page with file_get_contents but at least it works.

if file_exists in PHP issue wordpress

I am attempting to create code that checks if an image exists on my site and if not shows a default image. In one case I know the file exists and can get it to link to the file using the variable that I want file_exist to use!
$menuCategories = get_categories( array(
'child_of' => $whichGrade,));
foreach ( $menuCategories as $menuCategory ) { ?>
<?php
$linktoicon = get_bloginfo('template_directory') ."/images/menuicon_".$menuCategory->slug.".png";
if (file_exists($linktoicon)) {
$iconref = $menuCategory->slug;
}else {
$iconref = "default";
} ?>
<a href='<?php echo $linktoicon; ?>'> <?phpvar_dump($iconref); ?></a>
<?php }?>
$linktoicon` is "http://mybritishhelper.com/wp-content/themes/wpex-magtastico/images/menuicon_colours.png"
Thank you.
PHP's file_exists isn't used with URL's, it's used for paths to files on the server itself. Not a problem. First, we need to get the path to your theme's template directory:
$templateDirectory = get_template_directory();
Assuming that the link you provided is on your own server, the full path to your image is
$pathToImage = $templateDirectory . '/images/menuicon_colours.png';
We can now check if your image exists with the following
if (file_exists($pathToImage)) {
// Do stuff
}
Hope this helps. For reference, see the docs for file_exists and get_template_directory()

Best way to have a variable template in php

I'm coding a menu bar for a website in php. Because I don't want to have to edit it multiple times on the half a dozen or so pages I'll have I've decided to put it in it's own separate header.php file and just include_once(header.php) in the various pages.
My problem is that the menu is going to be slightly different depending on which page it's included in. Right now I'm dealing with it by having the following in my header.php file with $PageTitle being defined in the individual pages:
if ($PageTitle == "Home"){
echo '<li class="active">Home</li>';
}
else{
echo '<li>Home</li>';
}
if ($PageTitle == "About"){
echo '<li class="active">About</li>';
}
else{
echo '<li>About</li>';
}
...
The active class simply highlights the menu of the current page (Like the menu bar on the top of StackOverflow). It works fine but I'm curious if there is a better perhaps more efficient way to doing this. Thanks guys.
Try this:
//list of menu headers
$headers = new array();
//populate the array with your headers here ...
foreach($headers as $val)
{
if( $PageTitle == $val )
echo '<li class="active">'.$val.'</li>';
else
echo '<li>'.$val.'</li>';
}
for current class you can also use jquery if you want to:
$(function(){
var path = location.href;
if ( path )
$('.side_menu a[href="' + path + '"]').attr('class', 'current');
});

How to set a link based off a post_parent being set - WordPress

I'm learning as I go here and wanted to reach out for a better understanding of how to handle an if statement within WordPress regarding the Parent being set or not.
What I'm trying to do:
I'm attempting to set the URL for an element based off the Parent being set for a page within the "Page Attributes" section. As it currently stands, if a Parent is set for the page, it will update the href value based off the homepage of the parent. However, if no parent is set, it is populating the page URL as the parent.
What I want it to do:
If no parent is set, echo home_url(). This will have it default to the homepage URL if no Parent is set.
Original version:
<?php $permalink = get_permalink($post->post_parent); ?>
Newer version (that needs TLC to work):
PHP:
<?php
if ($post->post_parent) {
$permalink = echo get_permalink($post->post_parent);
} else {
$permalink = home_url();
}
?>
HTML:
Example
Currently, it's not working for the else statement. If I attempt to echo any type of text for the else statement, it appends it to the vainty URL set for the page I'm actively viewing.
What I need the function to do:
<?php
if (a page/post has a parent set within the page attribute) {
$permalink = echo get_permalink($post->post_parent);
} else (if a page/post does not have a parent set within the page attribute) {
$permalink = echo home_url();
}
?>
Any help would be greatly appreciated! Thanks!
on your PHP:
<?php
the_post();
if(count(get_pages('child_of='.$post->ID))!=0){
if($post->post_parent!=0) {
$permalink = get_permalink( end( get_ancestors( get_the_ID(), 'page' )));
} else if ($post->ID==0) {
$permalink = home_url();
} else {
$permalink = get_permalink();
}
} else {
$permalink = home_url();
}
?>
you should remove echo after equals = if you don't want the page to mess up.
then you will be able to get the value you want on your variable $permalink.
P.S. there might be a cleaner way to do this but for now, here it is.
UPDATE: pls check discussion logs to how we arrived with the answer.

PHP + Wordpress: Auto list image name into dropdown slection

I am creating wordpress theme option panel and want to use some icons. I have one directory dedicated for icon into my theme folder. What I want to do is if user add any new image into that folder it will automatic appear into dropdown selection list into theme option panel.
Is there any way to do this in PHP with Wordpress? I believe that is possible as I saw one theme has the same option but it was so complex so couldn't figured out that and don't remember theme name too now.
I have to use it with below type of code
$video_tax = array(-1 => 'Choose a category');
$video_terms = get_terms('video_category');
if ($video_terms) {
foreach ($video_terms as $video_term) {
$video_tax[$video_term->term_id] = $video_term->name;
}
}
You might want to start by having a look at scandir. This will list all the contents of a folder on your system. From there it would just be a matter of putting the correct path, url or whatever you want in the the value of your options.
EDIT: Here's some sample code from one of my plugins:
function icons_meta(){
global $post;
$custom = get_post_custom($post->ID);
$link = $custom["icon"][0];
$files = scandir(PATH."/icons");
$selected = '';
echo "<select name='icon'>";
foreach($files as $file){
if($file == $link){
$selected = 'selected="selected"';
} else {
$selected = '';
}
echo "<option value='$file' $selected>".$file."</option>";
}
echo "</select>";
}

Categories