Why some of Wordpress theme doesn't have Header Image Section - wordpress

I am new in Wordpress. I want to know. Why some of the Wordpress themes don't have header image section in Theme customize? How to add this section to the theme customizer? Is there any way to do it
And one question. How can I modify the upload file image in the theme customizer?
I want to add more field and change the crop size

this depends on the WordPress theme developer how they design and develop themes but you can achieve this by adding your own option like (image, textarea , input) fields in the customizer area.
Here is the example add this code in a function.php file
// Header Logo
function add_logo_customizer($wp_customize) {
$wp_customize->add_section(
'logo_image',
array(
'title' => 'Logo Image',
'priority' => 45,
)
);
$wp_customize->add_setting( 'Logo-upload' );
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
'lp-image_selector',
array(
'label' => 'Logo Image',
'section' => 'logo_image',
'settings' => 'Logo-upload'
)
)
);
}
add_action('customize_register', 'add_logo_customizer');
and you can call it as <?php echo get_theme_mod( 'Logo-upload' ); ?>
The above code is tested and it works

For the first you need to add support to custom-header.
See the codex:
https://developer.wordpress.org/themes/functionality/custom-headers/
Obviously, you need to create a php file with the code and include in the functions.php or add the code directly to your functions.php file.
function themename_custom_header_setup() {
$defaults = array(
// Default Header Image to display
'default-image' => get_template_directory_uri() . '/images/headers/default.jpg',
// Display the header text along with the image
'header-text' => false,
// Header text color default
'default-text-color' => '000',
// Header image width (in pixels)
'width' => 1000,
// Header image height (in pixels)
'height' => 198,
// Header image random rotation default
'random-default' => false,
// Enable upload of image file in admin
'uploads' => false,
// function to be called in theme head section
'wp-head-callback' => 'wphead_cb',
// function to be called in preview page head section
'admin-head-callback' => 'adminhead_cb',
// function to produce preview markup in the admin screen
'admin-preview-callback' => 'adminpreview_cb',
);
}
add_action( 'after_setup_theme', 'themename_custom_header_setup' );
Then on your header.php get the image and put in where you want it.
<?php if ( get_header_image() ) : ?>
<div id="site-header">
<a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
<img src="<?php header_image(); ?>" width="<?php echo absint( get_custom_header()->width ); ?>" height="<?php echo absint( get_custom_header()->height ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>">
</a>
</div>
*functions.php is on the root of your theme folder, if your theme uses a "child-theme" please use this folder to do the changes.
And answering your question, when you upload the image you can crop and resize the image. Wordpress cannot edit photos as an editor, only resize and crop.

Related

Remove images from content in wordpress gallery template

I want to add a gallery template to a child theme and am finding it frustratingly difficult to find good examples. The best example I've found to date is by Andres Hermosilla, but was made way back in 2012.
Steps 1 to 3 will get me half of what I want, which is a gallery inserted above the site title (I don't need fluxslider or similar at this stage.) The following is the code I'm using, placed just before the_title():
<?php
$image_args = array(
'numberposts' => -1, // Using -1 loads all posts
'orderby' => 'menu_order', // This ensures images are in the order set in the page media manager
'order'=> 'ASC',
'post_mime_type' => 'image', // Make sure it doesn't pull other resources, like videos
'post_parent' => $post->ID, // Important part - ensures the associated images are loaded
'post_status' => null,
'post_type' => 'attachment'
);
$images = get_children( $image_args );
if ( $images ) {
foreach ( $images as $image ) { ?>
<img src="<?php echo $image->guid; ?>" alt="<?php echo $image->post_title; ?>" title="<?php echo $image->post_title; ?>" />
<?php }
} ?>
My issue is that the images themselves remain displayed in the content of the post - how can I strip all of the images from the page content at the same time?
Why not add a class to the gallery template page and display none all the images except for gallery ones using css.

I don't know how to place Get Theme Mod

I am developing a wordpress theme and i am trying to add the cutomizer feature.
I want to change to the text only using customizer, otherwise it should be set to default. The section gets added to the customizer but not affecting the text. I wat to change powered by wordpress and other options of footer.
Below is the code i have.
<footer id="ob-footer" class="site-footer" <? echo get_theme_mod('footer');?> >
<div class="site-info" >
<a href="<?php echo esc_url( __( 'https://wordpress.org/', 'openblogger' ) ); ?>">
<?php
/* translators: %s: CMS name, i.e. WordPress. */
printf( esc_html__( 'Proudly powered by %s', 'openblogger' ), 'WordPress' );
?>
</a>
<span class="sep"> | </span>
<?php
/* translators: 1: Theme name, 2: Theme author. */
printf( esc_html__( 'Theme: %1$s by %2$s.', 'openblogger' ), 'openblogger', 'WebsiteGuider' );
?>
</div><!-- .site-info -->
</footer>
I don't know what to do to make it happen. The customizer code looks like:
/**
* Create site footer Setting and customise Control
*/
function your_theme_new_customizer_settings($wp_customize) {
$wp_customize->add_section( 'hellop' , array(
'title' => __( 'Site Footer', 'openblogger' ),
'priority' => 30
) );
// add a setting for the site footer text
$wp_customize->add_setting('footer');
// Add a control to change text
$wp_customize->add_control( new WP_Customize_Control( $wp_customize, 'footer',
array(
'label' => 'Update Text Here',
'section' => 'hellop',
'settings' => 'footer',
) ) );
}
add_action('customize_register', 'your_theme_new_customizer_settings');
Thank You all of you guys, no one answered and now i solved it of my own

How to put HTML code between add_settings_field in custom Wordpress theme options page

I would like to display a horizontal rule between two specific fields.
If I write the following code, the <hr/> appears on the top of the screen. How can I display it in the desired position? I don't want to put the HTML code in the callback function.
Is there any other way to achieve this? Thank you!
<?php
// ...
add_settings_field(
'telephone',
'Telephone',
'my_theme_textbox_callback',
'my_theme_contact_option',
'my_theme_contact',
array(
'context'=>'my_theme_contact_option',
'name'=>'telephone',
)
);
add_settings_field(
'fax',
'Fax',
'my_theme_textbox_callback',
'my_theme_contact_option',
'my_theme_contact',
array(
'context'=>'my_theme_contact_option',
'name'=>'fax',
)
);
echo '<br/>'; // <---- THIS DOESN'T DISPLAY IN THE RIGHT POSITION
add_settings_field(
'address',
'Address',
'my_theme_textbox_callback',
'my_theme_contact_option',
'my_theme_contact',
array(
'context'=>'my_theme_contact_option',
'name'=>'address',
)
);
// ...
?>

How to fetch all images from particular page id in wordpress

I have created a page called "Gallery" and created new gallery with 10 images. My page id is 129. I have 10 images in that page id(129).
Now my question is, i need a code to fetch those 10 images in wordpress. please anyone help me with a code. Thanks in advance.
Get all images from post.
function get_all_images() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
return $first_img;
}
Here you get frist image such like you get all other images
Use get_children
I used this code to extract all the images from a page gallery in the chosen order. you can include this code in the loop or use it stand alone. just choose the appropriate post_parent code (see bellow the code example).
This example show all images associated to the page id 129, have a look:
$images = get_children( array( 'post_parent' => 129, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
$images is now a object that contains all images (related to post id 1) and their information ordered like the gallery interface.
if ( $images ) {
//looping through the images
foreach ( $images as $attachment_id => $attachment ) {
?>
<?php /* Outputs the image like this: <img src="" alt="" title="" width="" height="" /> */ ?>
<?php echo wp_get_attachment_image( $attachment_id, 'full' ); ?>
This is the Caption:<br/>
<?php echo $attachment->post_excerpt; ?>
This is the Description:<br/>
<?php echo $attachment->post_content; ?>
<?php
}
}
Find the post id you wan to extract images from and insert it into this argument: 'post_parent' => 129
you can also use:
'post_parent' => $post->ID
If you want to use get_children in a loop, and get the post id from the returned post id.
If you want to exclude the image selected as a featured image i would have a if statement check if the image URL is equal to the featured image URL.

Wordpress - Featured image on a custom post archive page

i have created a custom post named Products.
register_post_type( 'products',
array(
'labels' => array(
'name' => __( 'Products' ),
'singular_name' => __( 'Product' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor', 'thumbnail' )
);
I have also created a php file named archive-products.php and made it into a template.
In Wordpress I have created a page named Products and selected the products template.
On that static page (that uses the archive template) I have uploaded a image into the Featured Image panel.
In my header I have the code:
echo get_the_post_thumbnail();
But this echos the Featured image of the last custom post in the list (all the products posts have a featured image as well), not the Featured image of the static/archive page, which is what i want. Is this possible to achieve?
Thanks!
I did the very same thing and came across the following answer that solved my problem: https://wordpress.stackexchange.com/a/175228
Save your custom post type archive template as a page.
For example, page-products.php
Backup locally and delete your custom post type archive template from your server.
Show the image with the_post_thumbnail(), put it in a variable with get_the_post_thumbnail(), or make it a background image with your page title over it:
$bg = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'full' );
if( is_page('products') ) : ?>
<div style="background: url('<?php echo $bg[0]; ?>') repeat center center #fbfbfb; background-size:cover;">
<?php the_title( '<h1 class="page-title">', '</h1>' ); ?>
</div>
<?php endif; ?>
Save your permalinks and refresh your page.
This is what worked for me. Hope it helps somebody. :)

Resources