Inserting variable into header - wordpress

I have setup a custom template and functions.php such that which users can insert an image url. The is for this image to be displayed in the header.
In the custom template, I use an add_action to place the image into the header. My problem is that whilst I can supply a url directly in the custom template with this image showing up, the same url inserted as a variable does not work.
So, the following extract from my custom template works (where $featimage is set to the url of the image)
$page_id = $wp_query->get_queried_object_id();
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
function img(){
$featimage="http://etc.jpg";
return $featimage;
}
function memberland_featured_image() {
$catchkathmandu_featured_image = '<div id="header-featured-image">';
$catchkathmandu_featured_image .= '<img id="main-feat-img" class="wp-post-image" alt="" src="'.img().'" />';
$catchkathmandu_featured_image .= '</div><!-- #header-featured-image -->';
echo $catchkathmandu_featured_image;
}
add_action( 'catchkathmandu_after_hgroup_wrap', 'memberland_featured_image', 10 );
get_header();
but here where $featimgage is set to the variable from the metabox it does not work.
$page_id = $wp_query->get_queried_object_id();
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
function img(){
$featimage=$memoptions['memberland_ftd_hder_image'];
return $featimage;
}
function memberland_featured_image() {
$catchkathmandu_featured_image = '<div id="header-featured-image">';
$catchkathmandu_featured_image .= '<img id="main-feat-img" class="wp-post-image" alt="" src="'.img().'" />';
$catchkathmandu_featured_image .= '</div><!-- #header-featured-image -->';
echo $catchkathmandu_featured_image;
}
add_action( 'catchkathmandu_after_hgroup_wrap', 'memberland_featured_image', 10 );
get_header();
p.s. I am using the function img() as in this post as without it, inserting $featimage directly into the src, even if pre-defined as the url, did not work.
I know that the variable options from them the metaboxes are working as images etc which I want displaying under the header all function.
Is this something to do with the order of wordpress functions? Thanks!

the img() function does not know about $memoptions variable as the variable is defined outside the scope of the img() function.
you can define $memoptions in img() function as below:
function img(){
$memoptions = get_post_meta($post->ID,'_my_meta',TRUE);
$featimage=$memoptions['memberland_ftd_hder_image'];
return $featimage;
}

Related

Wordpress default logo

I am working a new Wordpress theme.
The theme will have the option to set a logo in the customizer as normal.
This is working.
However, I want the theme to automatically set a DEFAULT logo.
If the user doesn't define a logo in the customizer, then the default logo will display automatically.
I have an image called logo.jpg in a folder call img in the theme root.
I am using this code to set the default logo but it is not working:
add_filter('get_custom_logo',function($html){
if(empty($html)) {
$html = '<img src = get_template_uri() . "/img/logo.jpg" >';
}
return $html;
});
Any ideas? Is it my syntax with a mix of " and '?
Thanks!
You had a mistake in the string concatenation.
P.S. Please, don't use anonymous functions in filters. Because it's not possible to remove or change this filter in child-theme for example.
add_filter( 'get_custom_logo', 'apply_default_logo' );
function apply_default_logo( $html ){
if( empty( $html ) ) {
$html = '<img src="' . get_template_directory_uri() . '"/img/logo.jpg">';
}
return $html;
}

Wordpress grab page attributes

Is there a Wordpress function for grabbing the page attributes? I need to be able to check which templates are being used on which pages.
I have tried the get_post and get_pages but neither one outputs the page attributes.
Thanks in advance!
solution:
$ids= get_all_page_ids();
foreach ($ids as $id){
$meta = get_metadata('post', $id);
//var_dump($meta);
$template = $meta['_wp_page_template'][0];
echo $template;
echo "<br>";
}
Try using get_all_metadata. That will fetch all the meta records for a given object.
<?php
$post_id = 123;
$meta = get_metadata('post', $post_id);
echo $meta['my_custom_field_key'];
The docs are a good place to look: Function Reference « WordPress Codex
i.e.: Function Reference/get page template which
Displays the filename of the page template used to render a Page
(printed within an HTML comment, in this example) :
<?php echo '<!-- ' . basename( get_page_template() ) . ' -->'; ?>
And,
global $wp_query;
$template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
will give you the template file name. Use str_replace() to strip the .php from the end.
`

Use postmeta in wordpress shortcode

Trying to get my post meta from posts using shrotcodes and then displaying it on the content.This is the code that's trying to do this:
$string = '';
$custom_content = get_post_custom($post->ID);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
$content = get_the_content();
$bonus = $custom_content["bonus"];
$string .= $content . $bonus . '<br>';
endwhile;
}
return $string;
It's not working as the custom content returns empty. Whats wrong? Thanks in advance
I don't think you got the shortcode idea, you should read some info about add_shortcode() also you can use get_post_meta() to retriev the metadata from the database.
Here is an example on how you can achieve this but this will only work with the main loop (as default):
<?php
//you can put this code in functions.php or you can build a plugin for it
function metadata_in_content($attr) {
//this is the function that will be triggerd when the code finds the proper shortcode in the content; $attr is the parameter passed throw the shortcode (leave null for now)
global $wpdb, $wp_query;
//we need global $wpdb to query the database and to get the curent post info
if (is_object($wp_query->post)) {
$post_id = $wp_query->post->post_id;// here we save the post id
$metadata = get_post_meta( $post_id, $key, $single ); // here we get the needed meta, make sure you place the correct $key here, also if you don't want to get an array as response pass $single as "true"
return $metadata; // this finally replaces the shortcode with it's value
}
}
add_shortcode('insert_metadata', 'metadata_in_content');
//the above code hooks the metadata_in_content function to the [insert_metadata] shortcode
?>
Now all it's left to do is to place [insert_metadata] in the post content and things should work.

Obtaining wp_list_pages in a Wordpress widget

I am creating my own little widget sitemap to put in my footer. Here's my code:
function widget($args, $instance)
{
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if ( $title ) { echo $before_title . $title . $after_title; }
// WIDGET CODE GOES HERE
// ?> THIS IS A TEST! <?php
?><ul><?php
wp_list_pages('title_li=<h2>MAP OF THE SITE</h2>&sort_column=menu_order&depth=0&include=57,55,59,61,63,65,192');
?></ul><?php
echo $after_widget;
}
but looks like wp_list_pages doesn't return anything. Not even the title. Indeed if I uncomment that "this is a test" then it's shown.
The strange thing is the same wp_list_page is implemented also in header.php to obtain menus.
First rule when creating Wordpress custom functions is to check if function with the same name already exists.
<?php if (!function_exists("function_name")) {...} ?>
Second rule is to name your function with really unique name, in order to avoid conflicts. In your case this function already exists, that's why you cannot extract the data as you expect...
One more thing, I assume you will put this whole function code in your theme's functions.php file.

Exclude the_post_thumbnail from gallery shortcode

I am using this code to have a simple gallery on the page:
<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?>
The problem now is that the end-user has to upload an image via the Media page before selecting this image as featured image.
I know this could be solved by adding the featured image's ID to the shortcode's exclude list, but how to get this ID automatically?
function exclude_thumbnail_from_gallery($null, $attr)
{
if (!$thumbnail_ID = get_post_thumbnail_id())
return $null; // no point carrying on if no thumbnail ID
// temporarily remove the filter, otherwise endless loop!
remove_filter('post_gallery', 'exclude_thumbnail_from_gallery');
// pop in our excluded thumbnail
if (!isset($attr['exclude']) || empty($attr['exclude']))
$attr['exclude'] = array($thumbnail_ID);
elseif (is_array($attr['exclude']))
$attr['exclude'][] = $thumbnail_ID;
// now manually invoke the shortcode handler
$gallery = gallery_shortcode($attr);
// add the filter back
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
// return output to the calling instance of gallery_shortcode()
return $gallery;
}
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
<?php $id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID ?>
<?php echo do_shortcode('[gallery exclude='.$id.' link="file" itemtag="div" icontag="span" captiontag="p" size="thumbnail" columns="4" ]'); ?>
How about?
echo do_shortcode('[gallery exclude="' . get_post_thumbnail_id( $post->ID ) . '"]');

Resources