Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 5 years ago.
Improve this questionOriginal title - Noob here. Wondering how to make use of this Astra theme function / filter to have different site identity logos on each page
The function is at line 1471 here (astra_replace_header_logo).
function astra_replace_header_logo( $image, $attachment_id, $size, $icon ) {
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( ! is_customize_preview() && $custom_logo_id == $attachment_id && 'full' == $size ) {
$data = wp_get_attachment_image_src( $attachment_id, 'ast-logo-size' );
if ( false != $data ) {
$image = $data;
}
}
return apply_filters( 'astra_replace_header_logo', $image );
}
I know I should be writing functions with conditional statements in the functions.php file of my child theme, but beyond that I need a bit of guidance. I have used astra_has_custom_logo
before to disable the logo for certain pages, and I made use of is_page()
in that case (for if elses), which worked.
Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.
Closed 5 years ago.
Improve this questionOriginal title - Noob here. Wondering how to make use of this Astra theme function / filter to have different site identity logos on each page
The function is at line 1471 here (astra_replace_header_logo).
function astra_replace_header_logo( $image, $attachment_id, $size, $icon ) {
$custom_logo_id = get_theme_mod( 'custom_logo' );
if ( ! is_customize_preview() && $custom_logo_id == $attachment_id && 'full' == $size ) {
$data = wp_get_attachment_image_src( $attachment_id, 'ast-logo-size' );
if ( false != $data ) {
$image = $data;
}
}
return apply_filters( 'astra_replace_header_logo', $image );
}
I know I should be writing functions with conditional statements in the functions.php file of my child theme, but beyond that I need a bit of guidance. I have used astra_has_custom_logo
before to disable the logo for certain pages, and I made use of is_page()
in that case (for if elses), which worked.
1 Answer
Reset to default 1Here's couple examples on how to determine which page you're on within a filter and how to return different data based on it.
On these examples I'm using the apply_filter()
part of the code you linked to to hook the custom code to the theme code. The custom function recieves the variabels from apply_filter()
as parameters.
The custom function should return a value with a matching type and format as the first parameter.
In my example the option 1 uses a hard-coded array of pages and logos (with a image id and image source array examples). Option 2 assumes the page specific logo is saved as an integer in the page post_meta. The custom function returns custom image data, if there's a page match and the data is found. The function returns the first parameter as a fallback / default, meaning nothing was changed.
function my_prefix_filter_astra_replace_header_logo( $image ) {
// option 1 - logo from hard coded value
global $post;
$logos_for_pages = array(
'page-a' => 123,
'page-b' => array( // using wp_get_attachment_image_src() return format
'/wp-content/themes/my-theme/logos/logo-b.png', // url
100, // width
30, // height
false // is_intermediate
),
);
if ( isset( $logos_for_pages[$post->post_name] ) ) {
if ( is_int( $logos_for_pages[$post->post_name] ) ) {
$replace_logo = wp_get_attachment_image_src( $logos_for_pages[$post->post_name], 'ast-logo-size' );
if ( false !== $replace_logo ) {
return $replace_logo;
}
} else {
return $logos_for_pages[$post->post_name];
}
}
// option 2 - logo from post meta
global $post;
$logos_for_pages = array(
'page-a',
'page-b'
);
foreach ( $logos_for_pages as $page_name ) {
if ( is_page( $page_name ) ) {
$logo_id = get_post_meta( $post->ID, 'meta_key_for_page_specific_logo', true );
if ( $logo_id ) {
$replace_logo = wp_get_attachment_image_src( $logo_id, 'ast-logo-size' );
if ( false !== $replace_logo ) {
return $replace_logo;
}
}
}
}
return $image;
}
add_filter( 'astra_replace_header_logo', 'my_prefix_filter_astra_replace_header_logo' );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745273297a4619883.html
评论列表(0条)