Preventing Display of Post Gallery when on Homepage - wordpress

I would like to prevent the post's gallery from displaying when the post is listed on the homepage.
I'm thinking it will utilize add_filter and apply_filter when post in on homepage.
You can add a gallery to posts by clicking the add media button. You can select existing images or upload additional images that will create a gallery within the post. This embeds a shortcode in $post['content'] that looks like [gallery ids="37,38,39,40,41,42].
The issue is that by default it displays when the post is included on homepage as well as the individual post itself.
Update: This is what I am doing right now to achieve the requirement. I suspect there will be a more elegant way.
<div class="entry-content">
<!-- Begin Post Content -->
<?php if ( is_single() ) : ?>
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'faceboard' ) ); ?>
<?php else : // Filter Gallery ShortCode out ?>
<?php
$content = '';
$content = get_the_content();
$content = preg_replace('/\[gallery\sids="[0-9]+(,[0-9]+)*,?"\s?(royalslider="\d")?\]/s',"",$content);
echo wpautop( $content, 1);
?>
<?php endif; // is_single() ?>
<!-- End Post Content -->
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'faceboard' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->

You can add the following to your theme's functions.php file or to a custom plugin (better, as you can disable it without touching the theme).
The filter post_gallery is used to create your own gallery, and the $content parameter comes empty by default. If it returns empty, the original [gallery] shortcode is processed.
Here, we are using a dummy empty value, so the filter is tricked into thinking that we are passing some actual gallery content, but it's just a white space.
add_filter( 'post_gallery', 'disable_home_galleries_so_17635042', 10, 2 );
function disable_home_galleries_so_17635042( $content, $atts )
{
// http://codex.wordpress.org/Conditional_Tags
if( is_home() )
return ' ';
return $content;
}

Related

Show featured image and excerpt of specific page on home page wordpress

I want to display featured image and excerpt of ie. ABOUT page on my home page. Is it possible to do this with pages not posts only?
found the answer in the meantime for the excerpt, now I need only featured image
add new function to functions.php:
//Display page excerpts
add_action( 'init', 'my_add_excerpts_to_pages' );
function my_add_excerpts_to_pages() {
add_post_type_support( 'page', 'excerpt' );
}
and call the function in front-page.php>
<?php echo get_the_excerpt(); ?>
<?php
query_posts("page_id=2");
while ( have_posts() ) : the_post()
?>
<h1>Why US?</h1>
<?php the_excerpt(); ?>
<?php
endwhile;
wp_reset_query();
?>
For the featured image, you need :
1 - to modify your function, to add also thumbnail support
add_action( 'init', 'my_extend-page_functions' );
function my_extend-page_functions() {
add_post_type_support( 'page', array('excerpt', 'thumbnail' );
}
(Have a look on the codex page of all you can add http://codex.wordpress.org/Function_Reference/add_post_type_support )
2- on the front-page, you can use the_post_thumbnail()
http://codex.wordpress.org/Function_Reference/the_post_thumbnail

Edit search results template in Wordpress

I have created a page template with a nice layout using the lovely custom fields plugin so my client can easily update the content.
I created a loop on that page template that displays the relevant information nicely;
Here is the loop I made:
<?php
$args = array( 'post_type' => 'cripps_staff', 'posts_per_page' => 300 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div class="col-md-3 spacetop">';
echo '<a href="'.get_permalink().'">';
echo get_post_meta($post->ID,'image',true);
echo '</a>';
echo '<h2 class="staffname">';
echo get_post_meta($post->ID,'staff_name',true);
echo '</h2>';
echo '<h2 class="staffrole">';
echo get_post_meta($post->ID,'staff_role',true);
echo '</h2>';
echo '<h2 class="staffnumber">';
echo get_post_meta($post->ID,'staff_telephone_number',true);
echo '</h2>';
echo '<h2 class="staffemail">';
echo get_post_meta($post->ID,'staff_email_address',true);
echo '</h2>';
echo '</div>';
endwhile;
?>
I created taxonomies so the staff members are split into categories.
I am then using a plugin called Taxonomies filter to create those dropdown options you will see. When you select an element in the dropdowns, Wordpress goes to/changes the page to a custom search results page I created. I want my search results to be displayed exactly like my loop on the People's template. Currently it just spits it out the title in a h1 tag.
Here is the code I got from the Twenty Fourteen theme:
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
/*
* Include the post format-specific template for the content. If you want to
* use this in a child theme, then include a file called called content-___.php
* (where ___ is the post format) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next post navigation.
CrippsTheme_paging_nav();
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
How can I get the search results to look exactly like my Post loop?
I managed to resolve this completely with the help of Pieter Goosen, who provided me with an awesomely detailed response, see the full answer on Wordpress development forum:
https://wordpress.stackexchange.com/questions/143023/edit-wordpress-loop-taxonomies-filter

How do I tell wordpress that a template is an archive

The use case is that I want to list the most recent posts on a page. Each post containing only the summary and title and a link to the full post.
I have created a template called Latest Posts and it loads ok. However I want get_content() to somehow understand that this template is an archive template and therefor only display the excerpt and not the full post.
I use the following "content" template for all other listings and would like to reuse it when listing my latest posts, but since it isn't either a search or an archive the first selection is skipped, and the_content() will show the whole post.
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'bbl' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'bbl' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
Any ideas? I've been looking around for ideas, and I've seen a lot of different ways to make archives, but I haven't been able to put it all together to fit my use case.
The solution is to use this code:
global $more; // Declare global $more (before the loop).
....
$more = 0;
....
More on this subject can be found in the documentation:
http://codex.wordpress.org/Function_Reference/the_content#Overriding_Archive.2FSingle_Page_Behavior
Now the_content() will not show anything after the more-tag
It's not really telling the engine that this is an archive page, but only mimic the behaviour

get first image on custom post type

I have this custom post type Gallery. I want to call first image from the posts.
<?php
$item_count = 1;
$args = array( 'post_type' => 'gallery', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
$item_count = 1;
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title(); ?>
<div class="count"><?php echo $item_count; ?></div>
<div class="thumbnail">
// THE FIRST GALLERY ATTACHMENT IMAGE
</div>
<?php $item_count++; ?>
<?php endwhile; ?>
Any ideas?
Note: I added Gallery no simple image on the post!
Check this WordPress Codex example.
The code will show the first image associated with the post.
Or check this WordPress forum discussion that also deals with this problem.
UPDATE:
Go to Appearance > Editor and select Theme functions (functions.php).
at the end of the file add this:
// Get URL of first image in a post
function catch_that_image($my_postid) {
global $post;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_post_field('post_content', $my_postid), $matches);
$first_img = $matches [1] [0];
// no image found display default image instead
if(empty($first_img)){
$first_img = "/images/default.jpg";
}
return $first_img;
} ?>
Now in your code you modify this part of the code:
<div class="thumbnail">
<?php echo catch_that_image($loop->ID) ?>
</div>
UPDATE V2
I modified the code to suit your code.
UPDATE V3
I tried the code on my development site and I modified it further until the code worked as it should. You should not have any problems now.

Twenty Twelve display only Title and Meta for tag, category, archive results

I want to only have the post title and meta (cat, tag, date) to be displayed on the results page.
I have used this code posted by Esmi at wp-forums to partially achieve this.
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php elseif( !is_category() ) : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links">' . __( 'Pages:', 'twentytwelve' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
This code in the child theme's content.php will produce Excerpts on the Search page results, but just the Title and Meta for the Category results.
I have tried adding !is_tag || !is_archive to the elseif statement.. then everything returns the full post.
I have replaced the elseif !is_category with elsif !is_tag and I once again get just the title and meta result on the tag page, which is what I want.
It seems I should be able to add this list to the else if statement to get what I want, but I must not the the correct code to do such.
I also tried replacing the top string php if is_search
with is_search || is_tag || is_archive || is_category
and the gave me whole posts as well.
This should be simple.. but I am stuck. Any help will be appreciated.

Resources