WordPress.org

Ready to get started?Download WordPress

Forums

WordPress: Display div after Xth paragraph within post (1 post)

  1. miette49
    Member
    Posted 1 year ago #

    I am trying to display a "related posts" div in the Genesis framework of WordPress after the fifth paragraph of my posts, so that it appears "embedded" within the article.

    I've come very close to getting this working, by cobbling together the tutorials here (related posts: http://blogvkp.com/add-related-post-to-genesis-themes-without-plugin/) and here (inserting code within a post: http://rushtips.com/how-to-insert-ads-within-wordpress-post-content). The related posts are generated and the div is inserted in the correct place. Great.

    The problem is: now my entire post content prints TWICE.

    I know it has to do with the printf line (if I remove it, the post prints once, but obviously without the div I need), but I have no idea how else to code that.

    Ideas?

    Here's the code, in my functions.php file.

    /** Display related posts in Genesis based on Category  */
    
    function related_posts_categories() {
    if ( is_single ( ) ) {
        global $post;
        $count = 0;
        $postIDs = array( $post->ID );
        $related = '';
        $cats = wp_get_post_categories( $post->ID );
        $catIDs = array( );{
        foreach ( $cats as $cat ) {
        $catIDs[] = $cat;
        }
        $args = array(
        'category__in'          => $catIDs,
        'post__not_in'          => $postIDs,
        'showposts'             => 5,
        'ignore_sticky_posts'   => 1,
        'orderby'               => 'rand',
        'tax_query'             => array(
        array(
        'taxonomy'  => 'post_format',
        'field'     => 'slug',
        'terms'     => array(
        'post-format-link',
        'post-format-status',
        'post-format-aside',
        'post-format-quote' ),
        'operator' => 'NOT IN'
        )
        )
    );
    
    remove_action('genesis_post_content', 'genesis_do_post_content');
    $content = apply_filters('the_content', get_the_content());
    $content = explode("</p>", $content);
    
    $cat_query = new WP_Query( $args );
        if ( $cat_query->have_posts() ) {
        while ( $cat_query->have_posts() ) {
        $cat_query->the_post();
        $related .= '<li><a href="' . get_permalink() . '" rel="bookmark" title="Permanent Link to' . get_the_title() . '">' . get_the_title() . '</a></li>';
        }
    }
    }
    if ( $related ) {
    $paragraphAfter= 5; // Insert Ad after 5 paragraphs
    
    for ($i = 0; $i <count($content); $i++ ) {
        if ($i == $paragraphAfter)
            printf( '<div class="related-column visible-desktop"><h4>Related Posts</h4><ul>%s</ul><br /><img src="/180x150.jpg"></div>', $related ); //PROBLEM LINE
        echo $content[$i] . "</p>";
    }
    }
    wp_reset_query();
    }
    }
    add_action( 'genesis_post_content', 'related_posts_categories' );

Topic Closed

This topic has been closed to new replies.

About this Topic