Welcome to PressWork!

Want to ask a question or join the discussion? Great! Join now.

In this Discussion

How to add Adsense Below Post titles?
  • Hi,

    I've been using Presswork for a while now and quite happy with it. Although I've been trying really hard to add adsense after the post titles yet haven't been able to do it.

    I put my adsense code in "loop.php" as shown below but still it shows above the post titles. Any help would be appreciated. Thank you.

    ----------------------------------------------------------------

    <?php<br />/**
    * The loop that displays posts.
    *
    * The loop displays the posts and the post content. See
    * http://codex.wordpress.org/The_Loop to understand it and
    * http://codex.wordpress.org/Template_Tags to understand
    * the tags used in it.
    *
    * This can be overridden in child themes with loop.php or
    * loop-template.php, where 'template' is the loop context
    * requested by a template. For example, loop-index.php would
    * be used if it exists and we ask for the loop with:
    * get_template_part( 'loop', 'index' );
    *
    * Includes all the action blocks for archives, pages and posts.
    *
    * @since PressWork 1.0
    */
    ?>

    <?php <br />if(is_home()) do_action('pw_home_page');

    global $current_class;
    $current_class = 'odd';

    if(is_home()) echo '
    ';
    ?>

    <?php while(have_posts()) : the_post(); ?>
    " <?php post_class(); ?>>

    _______________________
    my-adsense-code
    _______________________

    <?php <br /> if(is_category()) :
    pw_actionBlock('pw_category_post');
    elseif(is_author()) :
    pw_actionBlock('pw_author_post');
    elseif(is_archive()) :
    pw_actionBlock('pw_archive_post');
    elseif(is_search()) :
    pw_actionBlock('pw_search_post');
    elseif(is_page()) :
    pw_actionBlock('pw_page_post');
    elseif(is_single()) :
    pw_actionBlock('pw_single_post');
    else :
    pw_actionBlock('pw_index_post');
    endif;
    ?>



    <?php endwhile; ?>
    <?php if(is_home()) echo '</div>'; ?>
    Post edited by gayan at 2011-11-26 23:46:59
  • This is the bit of code in the actions.php that you have to modify:
    /*
    * Posts
    */
    function pw_posts() {
    echo pw_function_handle(__FUNCTION__);
    ?>
    <header>
    <hgroup>
    <h1 class="posttitle"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', "presswork" ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>
    <h2 class="meta">
    <?php
    _e("by", "presswork"); echo " "; the_author_posts_link();
    echo '&nbsp;&bull;&nbsp;';
    the_time(get_option('date_format'));
    if(!is_home()) {
    echo '&nbsp;&bull;&nbsp;';
    the_category(', ');
    }
    if(comments_open()) { echo '&nbsp;&bull;&nbsp;'; comments_popup_link(__('0 Comments', "presswork"),__('1 Comment', "presswork"),__('% Comments', "presswork")); }
     
    ?>
    </h2>
    </hgroup>
    </header>
    <div class="storycontent">
    <?php
    if(function_exists('has_post_format') &&
    (has_post_format('aside') || has_post_format('link') || has_post_format('video') || has_post_format('image') || has_post_format('audio'))) {
    // new aside || link || audio || video || image post format
    echo '<div class="pformat clear">';
    if(function_exists('has_post_format') && has_post_format('image')) {
    if(function_exists('has_post_thumbnail') && has_post_thumbnail()) {
    echo '<a href="'.get_permalink().'">';
    the_post_thumbnail('medium', array('class'=>'alignleft'));
    echo '</a>';
    } else {
    the_content('');
    }
    } else {
    the_content('');
    }
    echo '</div>';
    } elseif(function_exists('has_post_format') && has_post_format('gallery')) { // new gallery post format
    global $post;
    $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
    if ( $images ) :
    $total_images = count( $images );
    $image = array_shift( $images );
    $image_img_tag = wp_get_attachment_image( $image->ID, 'full' );
    ?>
    <a class="gallery-thumb" href="<?php the_permalink(); ?>"><?php echo $image_img_tag; ?></a>
    <p class="gallery-text clear fl"><em><?php printf( _n( 'This gallery contains <a %1$s>%2$s photo &rarr;</a>', 'This gallery contains <a %1$s>%2$s photos &rarr;</a>', $total_images, "presswork" ),
    'href="' . get_permalink() . '" title="' . sprintf( esc_attr__( 'Permalink to %s', "presswork" ), the_title_attribute( 'echo=0' ) ) . '" rel="bookmark"',
    number_format_i18n( $total_images )
    ); ?></em>
    </p>
    <?php endif; ?>
    <?php
    } else {
    if(function_exists('has_post_thumbnail') && has_post_thumbnail()) {
    echo '<a href="'.get_permalink().'">';
    if(is_home()) $thumb = 'fifty'; else $thumb = 'thumbnail';
    the_post_thumbnail($thumb, array('class'=>'alignleft'));
    echo '</a>';
    }
    the_excerpt();
    }
    if(function_exists('has_post_format') && (has_post_format('aside') || has_post_format('link') || has_post_format('gallery'))) { // new aside || link post format
    // do nothing
    } else {
    echo '<a href="'.get_permalink().'" class="more-link">'.__("Read more &rarr;", "presswork").'</a>';
    }
    ?>
    </div>
    <?php
    }
    add_action('pw_archive_post_middle', 'pw_posts');
    add_action('pw_author_post_middle', 'pw_posts');
    add_action('pw_category_post_middle', 'pw_posts');
    add_action('pw_search_post_middle', 'pw_posts');
    add_action('pw_index_post_middle', 'pw_posts');


    Put your code into a custom actions and modify it as explained here: http://presswork.me/2011/custom-css-and-custom-actions/
    image PressWork Support Moderator
  • @cestbibi,

    First of all, thank you for the answer. Earlier today I did try to adsense in that "actions.php" in my child theme and I had to use a FTP client to access and repair the site which crashed the theme (it has nothing to do with you... all my fault :D).

    I'll give a try at this and let you know. Thanks.
  • Well this is what I tried.

    Since I use a presswork child theme I created a folder called "admin" and then put the "actions.php" file inside it (with my adsense code).

    Now I don't know what went wrong and I did tried few times (each time putting adsense code in a different location in the "actions.php") but other than the site not being able to load (gives me php errors, etc) or other times when it loaded the site yet no adsense ads were seen.

    So if you could please tell me where to put the adsense code (if i want adsense ads to display post titles) and where I have to put the "actions.php" file (concerning that I'm using a child-theme) I'd really appreciate it. Thank you.
  • You have to put your actions.php to the root of your child theme folder. No need to create an admin folder.
    Read carefully this http://presswork.me/2011/custom-css-and-custom-actions/ to understand how custom actions work.
    You could insert your adsense code somewhere before
    <h1 class="posttitle"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', "presswork" ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h1>

    in line 9
    image PressWork Support Moderator
  • @cestbibi,

    I'm sorry to bother you like this but I just can't get this to work :/.

    Here's what I did. Below is the complete copy of my actions.php file (except for the actual adsense code) that I put inside of my child theme and whether I remove the adsense code and just copy and paste the "single posts" php section inside to the actions.php, every time I get errors and the site doesn't load.

    Here's the code...


    Thank you.
    actions.zip
    713B
    Post edited by gayan at 2011-12-15 05:01:11
  • I can't download the file you appended, nor see the code but from what I saw before, the code is giving you errors because you use the same function that the PressWork theme uses.
    That's why I advised you to read here http://presswork.me/2011/custom-css-and-custom-actions/ (specially see the part "Customizing the 404 page:") to understand how custom actions work:

    As you can observe you have to do remove_action before you can put your own code, so in your case first you should do this :
    <?php
    remove_action('pw_index_post_middle', 'pw_posts');
    // Then you create your own function
    function my_custom_posts() {
    // paste hereafter all the rest of the code
    }
    add_action('pw_index_post_middle', 'my_custom_posts'); // here we just registered your own function
    ?>


    If you can get it right, please post the code again so I can see it, I will put the whole code for you.
    image PressWork Support Moderator
  • @cestbibi,

    Thank you for the patience, appreciate it.

    I've re-attached the file and used mediafire this time. Hopefully you can see it now.


    Here's the link

    http://www.mediafire.com/?8fc1r79k55xdawf

    Thanks.
    Post edited by gayan at 2011-12-15 05:47:03
  • EDIT: WRONG CODE
    SEE BELLOW FOR CORRECT ONE
    ***************************************************************************
    Here we go:
    <?php
    /*
    * Add Adsense code to Single Post
    */

    // First let's remove the PW action
    remove_action('pw_index_post_middle', 'pw_posts');
     
    // Then you create your own function
    function my_custom_posts() {
    echo pw_function_handle(__FUNCTION__);
    ?>
    <header>
    <?php if(is_single()) echo '<hgroup>'; ?>
    <h1 class="posttitle"><?php the_title(); ?></h1>
    <?php if(is_single()) { ?>
    <h2 class="meta">
    <?php
    _e("by", "presswork"); echo " "; the_author_posts_link();
    echo '&nbsp;&bull;&nbsp;';
    the_time(get_option('date_format'));
    echo '&nbsp;&bull;&nbsp;';
    the_category(', ');
    if(comments_open()) { echo '&nbsp;&bull;&nbsp;'; comments_popup_link(__('0 Comments', "presswork"),__('1 Comment', "presswork"),__('% Comments', "presswork")); }
    ?>
    </h2>
    ADSENSE CODE
    <?php } ?>
    <?php if(is_single()) echo '</hgroup>'; ?>
    </header>
    <div class="storycontent">
    <?php the_content( __( 'Read more &rarr;', "presswork" ) ); ?>
    </div>
    <footer>
    <?php
    the_tags('<p class="tags"><small>'.__('Tags', "presswork").': ', ', ', '</small></p>');
    wp_link_pages(array('before' => '<p><strong>'.__('Pages', "presswork").':</strong> ', 'after' => '</p>', 'next_or_number' => 'number'));
    edit_post_link(__('(edit)', "presswork"), '<p class="clear">', '</p>');
    ?>
    </footer>
    <?php
    }
    // Now we register your new function
    add_action('pw_single_post_middle', 'my_custom_posts');
    add_action('pw_page_post_middle', 'my_custom_posts');
    ?>

    I've tested it and it works
    Post edited by cestbibi at 2011-12-15 08:28:26
    image PressWork Support Moderator
  • @cestbibi,

    Finally now it's working!.

    However there's still something wrong because although now adsense under titles, etc works just fine but now every post page has an exact replica of itself (post title and the content shown twice).

    I'm sure this is a small issue... can you fix that too? thanks :).
  • cestbibicestbibi
    This answer was Accepted.
    Oops really sorry y pasted the wrong code (I have a zillion code editor windows open at the same time)
    <?php
    /*
    * Add Adsense code to Single Post
    */

    // First let's remove the PW action
    remove_action('pw_single_post_middle', 'pw_single_post');
     
    // Then you create your own function
    function my_custom_posts() {
    echo pw_function_handle(__FUNCTION__);
    ?>
    <header>
    <?php if(is_single()) echo '<hgroup>'; ?>
    <h1 class="posttitle"><?php the_title(); ?></h1>
    <?php if(is_single()) { ?>
    <h2 class="meta">
    <?php
    _e("by", "presswork"); echo " "; the_author_posts_link();
    echo '&nbsp;&bull;&nbsp;';
    the_time(get_option('date_format'));
    echo '&nbsp;&bull;&nbsp;';
    the_category(', ');
    if(comments_open()) { echo '&nbsp;&bull;&nbsp;'; comments_popup_link(__('0 Comments', "presswork"),__('1 Comment', "presswork"),__('% Comments', "presswork")); }
    ?>
    </h2>
    ADSENSE CODE
    <?php } ?>
    <?php if(is_single()) echo '</hgroup>'; ?>
    </header>
    <div class="storycontent">
    <?php the_content( __( 'Read more &rarr;', "presswork" ) ); ?>
    </div>
    <footer>
    <?php
    the_tags('<p class="tags"><small>'.__('Tags', "presswork").': ', ', ', '</small></p>');
    wp_link_pages(array('before' => '<p><strong>'.__('Pages', "presswork").':</strong> ', 'after' => '</p>', 'next_or_number' => 'number'));
    edit_post_link(__('(edit)', "presswork"), '<p class="clear">', '</p>');
    ?>
    </footer>
    <?php
    }
    // Now we register your new function
    add_action('pw_single_post_middle', 'my_custom_posts');
    ?>


    Now it should be ok !
    image PressWork Support Moderator
  • @cestbibi,

    Wow! it worked!!!. Oh man finally, it took me few seconds to believe it :) lol.

    Thank you so much, I mean, I was being a bit hectic and bit of a pain but "cestbibi" again, thank you. You saved me a hell lot of troubles.

    Both Presswork and the community are awesomeeeeeeeeeee :D.
  • @cestbibi,

    After I recently upgraded to the new version (1.0.4.2), everything worked fine, except, now I have two duplicate copies in every post (yikes! :)).

    Now I'm aware that this is because the new version has a redesigned "actions.php" file which is having issues with the custom (and older) "actions.php" file I have. But I just can't figure it out :/.

    So, again, "cestbibi", could you fix it ? Thank you.

    BTW, I downgraded to the older version as a temporary fix.

    Hope that you're doing well (still wrestling with codes I suppose :D).
    Post edited by gayan at 2012-04-19 07:05:35
  • image Senior Developer & Co-Founder of PressWork
  • @cbavota,

    Thank you for the answer!.

    I did manage to make it work after removing some of those codes that you suggested and now everything seems to be working well.

    But does it mean that though I have the latest version of presswork, while "creating posts", presswork still uses the "old" technique ?

    I tried to create a new "actions.php" file (with adsense etc) ... but it only gave me errors though.

    Still, thanks for the answer. Appreciate it :).
  • Okay,

    I tried to customize the new 'actions.php' file from scratch so I'd have adsense below the post title and and above the comments. But every time I put the newly created code, Wordpress refuses to render the site and gives a php error.

    I guess it has something to do with me not closing the last '?>' (according to my limited knowledge. So if anyone can have a look at it and point out what I'm doing wrong, I'll really appreciate it. Thank you.

    Here's the link to the file (sometimes the file attaching in the presswork-support doesn't work, that's why I've also uploaded it to filefactory :D).

    http://www.filefactory.com/file/2txpi4z3h4xn/n/actions.php
    actions.zip
    952B