Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Display custom post type posts within the_content filter

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #160300
    asiek
    Participant

    I’m using a custom post type labeled “textures” and in the page template that displays all posts within that post type. I decided last minute to add the WP-members plugin in order to deny access to viewers who aren’t registered. When viewing front end, nothing works at all. The login/registration forms aren’t displayed at all. Only the content that is suppose to be blocked. Later on I realized the plugin will only work if

    <?php the_content(); ?> is used. So I replaced this//

    [?php /* Loop the stuff from the videos post type */
                  $args = array( 'post_type' => 'textures', 'posts_per_page' => 50);
                  $loop = new WP_Query( $args );
                  while ( $loop->have_posts() ) : $loop->the_post();?]
    
                    <li>
                        <figure>
                            [?php the_post_thumbnail('thummy'); ?]
                            <figcaption>
                                <h3>[?php the_title(); ?]</h3>
                                [?php if ( has_post_thumbnail()) {
                                $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
                                echo '[a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a]'; }?>
                            </figcaption>
                        </figure>
                    </li>
    
                [?php endwhile;?]
    

    with this//

       <ul class="grid cs-style-3">
        [?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?]
            [?php the_content(); ?]
        [?php endwhile; ?]
        [?php endif; ?]
        </ul>
    

    and tried to use this code within my functions.php file, hoping it will display the desired content within the_content function//

    function new_default_content($content) {
    global $post;
    if ($post->post_type == 'textures') {
    $content .='<li>';
    $content .='<figure>';
    $content .= the_post_thumbnail('thummy');
    $content .= '<figcaption>';
    $content .= '<h3>';
    $content .= the_title();
    $content .= '</h3>';
    $content .= '<span>Cool stuff brah.</span>';
    
     if ( has_post_thumbnail()) {
        $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
        echo '[a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image[/a]'; 
     }
            $content .= '</figcaption>';
        $content .= '</figure>';
    $content .= '</li>';
    }
    return $content;
    }
    add_filter('the_content', 'new_default_content');
    

    After doing that the forms are displayed but when I view the blocked page, after logging in, the content is no where to be found. The blocked page is just blank.

    So my question is, how can I use the WP-members plugin with custom post types? Since CPTs require a query to display all posts…

    Hope that makes sense :/
    I tried my best explaining the issue.
    Please help.

    #160302
    chrisburton
    Participant

    Can you highlight your code and click the ‘block code’ button?

    #160303
    asiek
    Participant

    @chrisburton Yes I realized the code was totally messed up. So I fixed it :) Thank you :D

    #160306
    Alen
    Participant

    the_post_thumbnail and the_title are used to echo data, try using get_the_post_thumbnail and get_the_title inside your function.

    #160311
    asiek
    Participant

    @alen I followed your instructions, but the issue still occurs :( Thanks for your time and help though.

    #160338
    asiek
    Participant

    FOUND A SOLUTION
    I remembered that you can add content inside of shortcodes.
    So I created my own shortcode [textures_content]//

        add_shortcode( 'textures_content', 'textures_shortcode' );
    function textures_shortcode( $atts ) {
        ob_start();
        $query = new WP_Query( array(
            'post_type' => 'textures',
            'posts_per_page' => -1,
            'order' => 'ASC',
            'orderby' => 'date',
        ) );
        if ( $query->have_posts() ) { ?>
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <li>
                        <figure>
                            <?php the_post_thumbnail('thummy'); ?>
                            <figcaption>
                                <h3><?php the_title(); ?></h3>
                                <?php if ( has_post_thumbnail()) {
                                $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
                                echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >View Full Image</a>'; }?>
                            </figcaption>
                        </figure>
                    </li>
                <?php endwhile;
                wp_reset_postdata(); ?>
        <?php $myvariable = ob_get_clean();
        return $myvariable;
        }
    } 
    

    Copied and pasted the shortcode and now it works perfectly!!!!!!

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.