wordpressthemeshookactionstorefront

Limit add_action to default post archive only


Im using the storefront theme of woocommerce. In order to customize my blog page, I removed the 'storefront_post_content' through an action, and added my custom function.

The problem is that the <?php the_title() now also show on the custom post types I created.

How to I limit the remove_action and add_action for the blog page only. I used a static page to display news.

add_action( 'init', 'remove_storefront_actions' );
function remove_storefront_actions() {
 remove_action( 'storefront_loop_post', 'storefront_post_taxonomy', 40 );
 remove_action( 'storefront_post_header_before', 'storefront_post_meta', 10 );
 remove_action( 'storefront_loop_post', 'storefront_post_header', 10 );
 remove_action( 'storefront_loop_post', 'storefront_post_content', 30 );
 add_action( 'storefront_loop_post', 'jk_custom_storefront_post_content', 30 );
}


function jk_custom_storefront_post_content() {
 ?>
<div class="entry-content" itemprop="articleBody">
 <div class="grid grid-21 grid-news">
   <div class="box txt">
     <h3><?php the_title() ?></h3>
     <?php the_excerpt(); ?>
     <?php
    echo '<p><a class="btn-alt" href="'.get_permalink().'">Read More</a></p>'; ?>
   </div>

   <div class="box img">
     <?php
       if ( has_post_thumbnail() ) {
       the_post_thumbnail( 'full', array( 'itemprop' => 'image' ) );
       }
     ?>
   </div>
 </div>
</div>
 <?php
}
?>

I have tried to wrap it in if statements such as:

global $wp_query; if( isset( $wp_query ) && (bool) $wp_query->is_posts_page ) :

and

if (is_page('news')

and

if( get_post_type() == 'post' ) {


Solution

  • I used 'template_redirect' instead of 'init', along with ' if( get_post_type() == 'post' ) {' and it worked!