URL for thumbnail in Wordpress - wordpress

Trying to add a JS data attribute to a Wordpress thumbnail. I need the absolute URL of the image, not the post it's attached to.
Got this far:
<?php the_post_thumbnail('thumbnail-full', array('class' => 'addthis_shareable', 'addthis:url' => $src)); ?>
I thouhgt from the docs that $src would hold the URL, but it doesn't. All I get in the code is addthis:url at the end of the image tag, but no URL with it.
Any help? PHP is not my thing.

This should work
<?php
$src = wp_get_attachment_image_src(get_post_thumbnail_id(), 'thumbnail-full');
the_post_thumbnail('thumbnail-full', array(
'class' => 'addthis_shareable',
'addthis:url' => $src
));?>

Related

Placing Wordpress plugin name at the top of the amdin header

I have a Wordpress plugin which I've named 'Solve Maths'. I want this plugin name to be displayed only at the Admin header of the Arthur alone for easy access.
I used the admin_head action hook but once I installed the plugin, the system tells me my plugin has generated these number of characters at the header.
<?php function Solve_Maths{ echo "<a href='Solve_Maths.php'>Solve Maths</a>";} add_action('admin_head','Solve_Maths');?>
The Solve_Maths.php is the name of the main plugin file with the header information. I want this file name in the tag to be shown at the admin header of the user and should execute the file when the link is linked. Thank you all for your help.
This is not the correct way to do it. Please check the WordPress Codex and refer to the function add_node: https://codex.wordpress.org/Function_Reference/add_node
add_action( 'admin_bar_menu', 'toolbar_link_to_mypage', 999 );
function toolbar_link_to_mypage( $wp_admin_bar ) {
$args = array(
'id' => 'my_page',
'title' => 'My Page',
'href' => 'http://example.com/my-page/',
'meta' => array( 'class' => 'my-toolbar-page' )
);
$wp_admin_bar->add_node( $args );
}

Wordpress Display Child page data: Title, featured image and custom field

I'm displaying the child pages of the current page. With the featured image, title pulling through. I also want to pull through a custom field I've added. I'm outputting this at html so it looks like each child page has its own box, with the featured image filling the box, the title on top of the image and the the custom field value sitting under the title.
But I can't get the custom field to display properly, I just get the value 1, not sure if I've converted the array properly?
Could you help me out to output the custom field 'PageTag'
https://i.stack.imgur.com/vzCBU.png
https://i.stack.imgur.com/BwfuV.png
Wordpress template code
<div class="childPages">
<?php
$subs = new WP_Query(
array(
'post_parent' => $post->ID,
'post_type' => 'page',
'meta_key' => '_thumbnail_id'
)
);
if( $subs->have_posts() ) :
while( $subs->have_posts() ) :
$subs->the_post();
$pageTag = get_post_meta($post->ID, '_PageTag' , true);
echo '<div class="childPageBlock"> '.get_the_post_thumbnail().'<div class="childPageBlockText"><h3>'.get_the_title().'</h3><span>'.print_r( $pageTag ).'</span></div></div>';
endwhile;
endif;
wp_reset_postdata(); ?>
</div>
Removing the print_r() and leaving its as
'.$pageTag.'
Worked!

wordpress get_children doesn't reflect edits to image galleries

I have a very simple theme that retrieves all image gallery links for each post using the get_children() function.
Unfortunately, there's some strange bugs - first, if images are not uploaded when creating the post and are instead selected from the media library, they don't show up after publishing. Also, if I do any edit after a post has been created, be it reordering the images, adding new ones, deleting images, even deleting the whole gallery and creating a new one, they don't show up either. Refreshing browser cache doesn't do a thing.
If i change to one of the base themes, the images show up, and the right links are there when querying the db directly.
I reproduced the basic problematic code in a one file micro-theme, but the issue still happens:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php echo $post->id;
$args = array(
'numberposts' => -1,
'order_by' => 'menu_order',
'order' => 'ASC',
'post_mime_type' => 'image',
'post_parent' => $post->ID,
'post_status' => inherit,
'post_type' => 'attachment'
);
$images = get_children($args);
if ( empty($images)){
echo "nothing";
}
foreach ( $images as $id => $image ) :
echo $image->post_title;
endforeach;
?>
<?php endwhile; ?>
<?php endif; ?>
I'm stumped, I've tried using different functions to retrieve the posts with the same result, deactivated all plugins, nothing.
I'm testing both on OSX mavericks and an Ubuntu vps, could it be some database cache thing i'm supposed to flush somewhere?
When you set 'post_parent' => $post->ID, you will only get attachments to that page. That's part of the reason not all of the images show up.
But the bigger problem is that galleries are handled differently than posts/pages/attachments. Galleries only exist in the shortcode- they're not stored in the db- so you can't query them with get_posts or get_children.
To customize the way galleries are rendered you'll have to either hook into the wordpress code that creates the gallery, or deregister that function and write your own.
This question answers how to manipulate shortcode if you were writing a plug-in- but the process will be the same from a theme.
:-)

WordPress trigger standard post list rendering

Is there a way to programmatically trigger the standard displaying post list from a custom WP_Query, just like a "category" menu item does?
To clarify:
i'm not looking for plugins like List category posts that do the work but with a custom built-in template..
I need the wp's(theme's) standard post list rendering loop to be triggered !
Thanks!
Put this code where ever you need to render list of post from category "example_category_slug", (or any other taxonomy).
$wpq = array ('taxonomy'=>'category','term'=>'example_category_slug');
$myquery = new WP_Query ($wpq);
$article_count = $myquery->post_count;
echo "<ul>";
if ($article_count){
while ($myquery->have_posts()) : $myquery->the_post();
echo "<li>".$post->post_title."</li>";
endwhile;
}
echo "</ul>";
or use simpler query, like $myquery = new WP_Query ('cat= 11, 9'); for cat ids, or any other wp_query
But while choosing where to put it, consider Template Hierarchy as a set of rules which template file is right to use.
If you need a list of categoires insted post use wp_list_categories
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list'
);
wp_list_categories($args);
?>
ful ref. in wp codex
Here is a way to do it..
inside a shortcode's function, or in a function that act ajax style, put a code like that:
global $wp_query;
$cat = $_GET['categoria'];
$wp_query->init();
$wp_query->query(array(
'posts_per_page' => 4,
'category_name' => $cat
));
get_template_part( 'blog', 'columns' );
wp_reset_query();
die();
the get_template_part arguments are theme dependent and can be desumed from php template names from theme's root directory.
in this case wp will call [theme's_dir]/blog-columns.php..
i reccomend the lecture #Michal reccomended me

Default Image appears blank - Wordpress Theme Customization API

I want to make it possible for users of my theme to replace the banner through Theme Customization API. I have got it working, however, the default image appears blank and when I click "view page source" I get the following:
<img src=" " alt="banner" />
The default image shows up in the preview inside the API window, but not in the browser. The moment I upload a banner to replace the default banner with, it works perfectly. But I just can't get the default image to appear. Am I doing something wrong?
This is in my functions.php:
// Start New Section for Images
$wp_customize->add_section('customtheme_images', array(
'title' => _('Images'),
'description' => 'Change Images'
));
$wp_customize->add_setting('banner_image', array(
'default' => 'http://mywebsite.com/banner.jpg',
));
$wp_customize->add_control( new WP_Customize_Image_Control ($wp_customize, 'banner_image', array(
'label' => _('Change Banner'),
'section' => 'customtheme_images',
'settings' => 'banner_image'
) ));
And, this is inside my header.php:
<img src="<?php echo get_theme_mod('banner_image'); ?>" alt="banner">
And yes I have tripple checked the path of the default image, and it is corrent. Please help!
When using $wp_customize->add_setting('banner_image', array(
'default' => 'http://mywebsite.com/banner.jpg',
)); the default value is not saved into the database (until you save).
So you will have to use: <img src="<?php echo get_theme_mod('banner_image','http://mywebsite.com/banner.jpg'); ?>" alt="banner">
The problem you describe is some kind related to: https://wordpress.stackexchange.com/questions/129479/alternatives-to-handle-customizer-settings
I have the same situation here. The problem with the solution below:
<?php echo get_theme_mod('banner_image','http://mywebsite.com/banner.jpg'); ?>
I need the user to be able to delete the default image. This default image will only be used for the initial display of the theme. So I need a solution that when activating the theme, is already saved in the database.

Resources