Grab images from wordpress media library category - wordpress

Just picking up wordpress as of late and I am trying to create a homepage slide show. I am creating a custom theme for a client and I want to be able to categorize media in the media library, so I am using this plugin to add categories so I can just pull the images from the "homepageBanner" gallery that I created.
When I pull images from my media library on my template via get_posts(), get_children() or do_shortcodes(), they all don't give me a property on the objects that are returned to determine what category they are in.
I imagine this has been done before (without a plugin), and seems like an incredibly easy task, however where I look and search, all I am finding are plug-ins.

if you are using the media library categories plugin then check out the function get_category_archive located in the media-categories.php. it is used in the taxonomy-media_category.php to pull archived images. you could create a custom function in your themes function.php that does something similar

If you are using the: media-library-categories.1.0.6 plugin, then you can simply do the following to echo out an unordered list containing all your images for a specific media category (replace "11" with the ID of the media category you want to display)
<?php echo do_shortcode('[mediacategories categories="11"]'); ?>

Related

How to get a WordPress template file to insert different images based on the current page?

I have a number of pages (100) which are all build individually in a visual editor (DIVI). They each have a set of 10-15 different images on, inserted into an identical 'wrapper'. Other than the images, the pages are identical - This was my first website and so I knew no better than this approach when I began. As I now understand, I could use a custom page template for these pages to allow scalability/re-design with much greater ease.
I understand how a template.php page could be created that pulls necessary page metadata to populate the title/subheadings etc for each page. I am thinking of writing a for loop to insert each image for the page into the repeating 'wrapper'. However, I am not sure how the template would know which images to pass into this loop based on the page visited?
I had a few thoughts on how this could be done (I am very inexperienced):
a) Re-structure my wp-uploads folder to match my sitemap and get the template to insert all images in the path that matches the slug
b) Upload all images for each page to the page in the back-end (not sure if this is possible) and get the template to insert all images associated with the page in question
c) Get the template to loop through a database table containing all of the image names matched to the page id on which they should be found and only insert those that match the current page id
If this changes anything, all 100 pages are child-pages of pageX and their slug begins with a common word. eg:
mysite.com/pageX/commonword-page1/
mysite.com/pageX/commonword-page2/
Since you have so many pages, I wouldn't use an if statement. The best way to handle this would be to use Advanced Custom Fields and set up an image field to show on the pages post type. Then, in your code, call the custom field and that way every page dynamically show their respective image. You can find ACF in the Plugins repository on your WordPress site or download it directly from their website.
Here is the image field documentation:
https://www.advancedcustomfields.com/resources/image/
The cleanest way is to set the field to use the URL of the image. Here is what would go in your code:
<?php if( get_field('field_slug') ): ?>
<img src="<?php the_field('field_slug'); ?>" />
<?php endif; ?>

Add Thumbnail Support in Wordpress Theme

I am building a theme where I need to add Thumbnails for the Category
I have tried this in the theme function.php
function spencer_cat_support(){
add_theme_support('category-thumbnails');
}
add_action('after_setup_theme', 'spencer_cat_support');
I don't know what I am doing wrong here.
I don't think that there is any support in wordpress to add thumbnail to taxonomies like this it is only available for post types and custom post types. You can either write some code to add image support to category or use some external plugin based on which you prefer.
If you prefer writing your own code check this: https://wordpress.stackexchange.com/questions/8736/add-custom-field-to-category
If you prefer external plugin check this:https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

Add Shortcode to All Post Types

I want to display shortcodes (I'm using fruitful Shortcode) above the post title tag (H1). And I want to display it to all my post (also as a template).
i want to be like this
Thanks.
If you want to adjust the way your single posts are displayed for all the posts, you have to edit the single.php file in your theme folder. You can do this in the backend under "Design" -> "Theme Editor" or you can access the folder and files via FTP.
For your default posts in wordpress you just need to open the single.php file and look for the h1 with your title. It can look something like:
<h1><?php the_title(); ?></h1>
Above that (if you want to display it above), you can put your shortcode with the do_shortcode() function:
<?php echo do_shortcode('[name_of_shortcode]'); ?>
If you are not using normal posts of wordpress but custom post types or something else, please have a look at the wordpress template hierarchy to find out, which file to edit or how to name the new files for creating template files: https://developer.wordpress.org/themes/basics/template-hierarchy/
If you are using a theme that is not made by you, you should make sure you keep your theme updateable. So if you overwrite the single.php, with the next update of your theme, the file will not have your edits. You need a child theme, if you want to keep your parent theme up to date but also customize it in the page templates. Here is a nice tutorial for child themes: https://www.smashingmagazine.com/2016/01/create-customize-wordpress-child-theme/

Using Jetpack Portfolio Project in WordPress child theme does not call archive custom template

I have a child theme that uses the new Jetpack Portfolio Project custom post type and wish to modify archive.php to display custom results.
I'm using: WordPress v3.9.2; Theme: Child of Point, Jetpack is installed with Custom Content Types enabled, and Portfolio Projects selected in the Settings. (No other plugins that implement portfolio functionality are installed.)
According to the Codex:
Template Files
In the same way single posts and their archives can be displayed using
the single.php and archive.php template files, respectively,
single posts of a custom post type will use single-{post_type}.php
and their archives will use archive-{post_type}.php
and if you don't have this post type archive page you can pass BLOG_URL?post_type={post_type}
where {post_type} is the $post_type argument of the
register_post_type() function.
My understanding is that if you create files called single-jetpack-portfolio.php and archive-jetpack-portfolio.php within the child theme, WordPress will automatically use those files in place of single.php and archive.php respectively.
However, my child theme successfully calls single-jetpack-portfolio.php, but completely ignores archive-jetpack-portfolio.php, instead calling archive.php in the child.
I am stuck for a solution.
From the codex above, adding to the URL "?post_type=jetpack-portfolio" does cause the child theme to correctly use archive-jetpack-portfolio.php, but should I need to be manually modifying every single URL to explicitly specify this? Should WordPress not automatically be detecting this, as it does for the single-jetpack-portfolio.php file? How can I solve this?
I have tried:
Resetting the permalinks in case it was related to that (changing the option in Settings and saving and back again)
Adding an archive.php file to the child in addition to archive-jetpack-portfolio.php (I initially didn't have an archive.php in the child, so it used the parent's archive.php)
Publishing a new Jetpack portfolio project and updating an existing page (I read somewhere that publishing something might trigger Wordpress to see the changes)
Thanks in advance for any help.
I had the same problem described by the OP. When I visited mydomain.com/portfolio it would use the custom archive template. When I tried to view a project type it defaulted to the regular archive.php. I'm wondering if OP was viewing a project type page without realizing it.
My solution was to create a taxonomy template file. After playing around with it I figured out that
taxonomy.php
taxonomy-jetpack-portfolio-type.php
taxonomy-jetpack-portfolio-type-{name-of-project-type}.php
all worked correctly, depending on how specific you wanted to get.
There's more info at the wordpress codex: http://codex.wordpress.org/Template_Hierarchy#Custom_Taxonomies_display
Hope this helps someone.
I will be working on this the next days.
You should try this in the child archive.php first lines:
<?php
if( is_post_type_archive('jetpack-portfolio') )
get_template_part('content', 'jetpack-portfolio');
elseif( is_tax('jetpack-portfolio-type') || is_tax('jetpack-portfolio-tag') )
get_template_part('archive', 'jetpack-portfolio');
else continue;
?>

How would you recommend adding an image as a custom field in WordPress?

I need to add an image to each page in WordPress.
I don't wish to insert it using the WYSIWYG editor, I just need the url as a custom field, which I later use in the template.
I tried using the CFI plugin (Custom Field Images), and I hacked my way into getting it to work with the rest of my plugins, but then I moved the site to the production server and CFI just didn't work for some reason.
I can't seem to find any other plugin that just lets you pick an image from the library and add it as a custom field.
I've downgraded to the point where I'm willing to manually enter all the URLs into each and every page. but before I do, I thought I'd check here.
Can anyone tell me what's the easiest, best way to add images as custom fields in WordPress (2.7.1 if it matters)?
In our WordPress template, each post generally only has one image 'attached', which is displayed outside the posts' (textual) content.
I simply upload the file using the edit posts' media uploader, never insert it into the post like JoshJordan above, then retrieve the image using a bit of code in the right place in my template file.
This would also work if you're using more than one image in your post, eg. in your post content. As long as you keep the image used as the 'main' post image as the first image (remember you can re-order images in your posts' image library by dragging them up and down), you're easily able to call it anywhere in your template file by using something like this:
<?php
$img_size = 'thumbnail'; // use thumbnail, medium, large, original
$img_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts where post_parent= $post->ID and (post_mime_type = 'image/jpeg' OR post_mime_type = 'image/gif') and post_type = 'attachment'");
$img_array = wp_get_attachment_image_src($img_id,$img_size,false);
echo '<img src="'.$img_array[0].'"' title="'.get_the_title().'" />';
?>
No need for copying and pasting image urls.
The template I have uses a manually-entered custom field for the splash image of each post. When I'm done writing my article, I upload an image, copy its URL from the upload tool, never insert it into my post, and then paste that URL into the "Image" custom field. Simple as pie and takes only seconds. Insignificant compared to the amount of time it takes me to write an article.
You can use the custom key value fields on posts as well. let's say you always give your images the key 'thumb'. you can then use this code to output them in your post as a thumbnail:
<?php
$values = get_post_custom_values("thumb");
echo “<img src=\”$values[0]\” class=\”thumb\”></a>”; ?>
Consider using Flutter it's a bit tricky to figure out at first, and has many really useful featured, including EIP (edit in place), and image handling.
After installing the plugin create a new "Write Panel", you'll figure it out from there. The plugin provides you with a rather intuitive GUI, which includes an image uploader. The template tags are very easy to use, I believe it's something like
<?php echo get_image('name_of_field'); ?>
I just had to build a site for a client that needed the same feature, I ended up using Flutter.

Resources