Trying to insert a linked featured image into the content - wordpress

I am using filter hook to insert a featured image into the_content, that works, but when I try to wrap the featured image in an anchor tag, the link tag ends up directly after the content(not wrapping the image at all)
Is there something I am missing as far as understanding how to filter the_content()? Here's my code:
add_filter('the_content', 'add_img_to_ps_archive');
function add_img_to_ps_archive($content) {
if (is_post_type_archive('past_symposia') ) {
echo $content . '<a href ="#" "alignleft">' . the_post_thumbnail('symposia-thumb') .
'</a>';
} elseif( is_singular('past_symposia') ) {
echo $content . '<br />';
} else {
return $content;
}
}

Try to use commas ',' and not dots '.' for concatenating - donĀ“t ask me why, but wordpress does that sometimes...

This happened because the_post_thumbnail() outputs the image tag directly to the output buffer. You need to use get_the_post_thumbnail() to return the image tag so you can concatenate it with $content.

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;
}

Closing shortcode in WordPress

I made a simple WordPress plugin which highlights text.
add_shortcode('close-span', 'highlighter_closing_span_shortcode');
function highlighter_closing_span_shortcode($atts) {
return '</span>';
}
It's the closing shortcode part of the plugin. In this case, users must type "[close-span]". I want to change it to "[/span]". How can I modify the code above?
You can use the $content parameter of the shortcode to allow users to put copy between tags:
add_shortcode( 'span', 'my_span_shortcode' );
function my_span_shortcode( $atts, $content = null ){
return '<span class="highlighted">' . $content . '</span>';
}
You would use the shortcode like this:
[span]This will be highlighted[/span]
and that would result in:
<span class="highlighted">This will be highlighted</span>
Hi #Peter (would comment if I could) this may go without saying but have you tried
add_shortcode('/span', 'highlighter_closing_span_shortcode');
function highlighter_closing_span_shortcode($atts) {
return '</span>';
}

Trying to use ACF Options Page in Genesis to output a Image Logo

I have created an options page in ACF Pro and Have added a image field called company logo. Now I want to remove the genesis site title. I have genesis removing the site title but when i add in the ACF call it does not work. If someone can take a look at what I have and give me some advice that would be great.
remove_action( 'genesis_site_title', 'genesis_seo_site_title' );
add_action( 'genesis_site_title', 'child_seo_site_title' );
/* Then add logo to header. */
function child_seo_site_title() {
$logo = get_field('company_logo', 'option');
if($logo) {
$output .= "<img src='". $logo['url']."' alt='". $logo['alt'] ."' />";
}
}
Should be simple but can seem to get it to work.
You're appending the image to an undefined variable, $output, and then doing nothing with it.
Output the image instead:
function child_seo_site_title() {
$logo = get_field( 'company_logo', 'option' );
if ( $logo ) {
echo '<img src="' . $logo['url'] . '" alt="' . $logo['alt'] . '" />';
}
}

Wordpress - Check if the title is used in nav menu

I just added a filter to the_title using the following code:
function my_filter($content) {
$content .= '<div class="meta">';
$content .= 'My filter';
$content .= '</div>';
return $content;
}
if(!is_single()){
add_filter('the_title', 'my_filter', 1);
}
What I noticed is the filter is also added in those titles which are used in the wp_nav_menu() .
I only want to display the filter after the title if the current page is not a single post page.
Try changing your code to remove the filter as well:
if(!is_single()){
add_filter('the_title', 'my_filter', 1);
}
else {
remove_filter('the_title', 'my_filter', 1);
}
My guess is that once the filter is added on a non-single page, it stays applied until removed.
http://codex.wordpress.org/Function_Reference/remove_filter

Drupal html output theme

I have a custom module that generates a page of output, my question is about how to add html elements to the output such as headings and paragraphs etc.
My module is as follows
function mymodule_page() {
$output .= '<h3>'.t('Installs '.$cell).'</h3>';
$output .= theme('flot_graph', $graph1);
return $output;
}
is this the correct way to do this?
should line two be like this instead?
$output .= t('<h3>Installs '.$cell.'</h3>');
I prefer do not mix logic and presentation in module files, so I would use a tpl to print the content. For example, in your module I would put this code:
function mymodule_page() {
return theme_render_template(drupal_get_path('theme', 'your_theme') . 'path/to/your/file.tpl', array('cell' => $cell, 'graph1' => $graph1);
}
And in the tpl file:
<h3><?php echo t('Installs') . ' ' . $cell; ?></h3>
theme('flot_graph', $graph1);

Resources