Site logo

Archived topic

Custom Post Type

21 replies · Started by Patrick on August 19, 2019

Viewing posts 1–15 of 22

Hi Tom,

I've created a new custom page template, which should be usable like a blog page. For this I have build the page with the following code and named the page template page-advert.php

<?php
/* Template Name: Adverts-Listing-Page */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

get_header(); ?>

	<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
		<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
			<?php
			/**
			 * generate_before_main_content hook.
			 *
			 * @since 0.1
			 */
			do_action( 'generate_before_main_content' );

			$args = array('post_type' => 'advert');
			$myQuery = new WP_Query($args);
			
			if ( $myQuery->have_posts() ) :

				while ( $myQuery->have_posts() ) : $myQuery->the_post();

					/* Include the Post-Format-specific template for the content.
					 * If you want to override this in a child theme, then include a file
					 * called content-___.php (where ___ is the Post Format name) and that will be used instead.
					 */
					get_template_part( 'content', get_post_format() );

				endwhile;

				/**
				 * generate_after_loop hook.
				 *
				 * @since 2.3
				 */
				do_action( 'generate_after_loop' );

				generate_content_nav( 'nav-below' );

			else :

				get_template_part( 'no-results', 'index' );

			endif;

			/**
			 * generate_after_main_content hook.
			 *
			 * @since 0.1
			 */
			do_action( 'generate_after_main_content' );
			?>
		</main><!-- #main -->
	</div><!-- #primary -->

	<?php
	/**
	 * generate_after_primary_content_area hook.
	 *
	 * @since 2.0
	 */
	do_action( 'generate_after_primary_content_area' );

	generate_construct_sidebars();

get_footer();

Also I have created a page called contend-advert.php with the following code:

<?php
/**
 * The template for displaying posts within the loop.
 *
 * @package GeneratePress
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> <?php generate_do_microdata( 'article' ); ?>>
	<div class="inside-article">
		<?php
		/**
		 * generate_before_content hook.
		 *
		 * @since 0.1
		 *
		 * @hooked generate_featured_page_header_inside_single - 10
		 */
		do_action( 'generate_before_content' );
		?>

		<header class="entry-header">
			<?php
			/**
			 * generate_before_entry_title hook.
			 *
			 * @since 0.1
			 */
			do_action( 'generate_before_entry_title' );
			
			the_title( sprintf( '<h2 class="entry-title" itemprop="headline"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' );

			/**
			 * generate_after_entry_title hook.
			 *
			 * @since 0.1
			 *
			 * @hooked generate_post_meta - 10
			 */
			do_action( 'generate_after_entry_title' );
			?>
		</header><!-- .entry-header -->

		<?php
		/**
		 * generate_after_entry_header hook.
		 *
		 * @since 0.1
		 *
		 * @hooked generate_post_image - 10
		 */
		do_action( 'generate_after_entry_header' );

		if ( generate_show_excerpt() ) : ?>

			<div class="entry-summary" itemprop="text">
				<?php the_excerpt(); ?>
			</div><!-- .entry-summary -->

		<?php else : ?>

			<div class="entry-content" itemprop="text">
				<?php
				the_content();

				wp_link_pages( array(
					'before' => '<div class="page-links">' . __( 'Pages:', 'generatepress' ),
					'after'  => '</div>',
				) );
				?>
			</div><!-- .entry-content -->

		<?php endif;

		/**
		 * generate_after_entry_content hook.
		 *
		 * @since 0.1
		 *
		 * @hooked generate_footer_meta - 10
		 */
		do_action( 'generate_after_entry_content' );

		/**
		 * generate_after_content hook.
		 *
		 * @since 0.1
		 */
		do_action( 'generate_after_content' );
		?>
	</div><!-- .inside-article -->
</article><!-- #post-## -->

Within my functions.php of my child theme, i have added the following code:

// GP Columns
add_filter( 'generate_blog_columns','adverts_list_columns' );
function adverts_list_columns( $columns ) {
    if ( is_page_template( 'page-advert.php' ) ) {
        return true;
    }
    return $columns;
}
	
// GP Masonry
function adverts_list_masonry( $masonry ) {
	if ( is_page_template('page-advert.php') ) :
		return 'true';
	endif;
	return $masonry;
}
add_filter( 'generate_blog_masonry','adverts_list_masonry' );

What I like to have is, that the adverts page looks like the blog page. Could you help me please?

Also the page doesn't show the featured images of the 'advert' post-type. the archive does (linked on right side, use "Anzeigenkategorie-3" for example with posts with featured image)

Hope you can help me. Thanks and regards
Patrick

The adverts plugin i use is "WPAdverts".

Hmm, it looks like the <article> elements aren't getting the correct classes.

One thing I'm seeing is you need to replace this:

get_template_part( 'content', get_post_format() );

With this:

get_template_part( 'content', 'advert' );

Does that make a difference?

Hi Tom,

Thanks for your fast reply, but the change to
get_template_part( 'content', 'advert' );
does not make a difference.

do you have any other idea?

Instead of your adverts_list_columns function, can you try this?:

add_filter( 'generate_blog_columns', function( $columns ) {
    if ( is_page_template( 'page-advert.php' ) ) {
        return true;
    }

    return $columns;
}, 50 );

It seems like columns aren't activated on that page as of right now.

Hi Tom,

thanks again for your reply. I've changed to your last code, but the result is the same as before :(

Do you have any other custom functions? Something might be overwriting the filter.

Hi Tom,

no, I have no other changes within the functions.php. Also there are all other plugins deactivated. Only GP Premium, and WPAdverts are active.

Best regards
Patrick

If I change page-advert.php to index.php, than the design looks good. But I do not like to have this page as index.

Think there is anywhere a problem between columns/masonry with pages. hmmmm

Let's try this:

add_filter( 'generate_blog_columns', function( $columns ) {
    if ( is_page_template( 'page-advert.php' ) || 'advert' === get_post_type() ) {
        return true;
    }

    return $columns;
}, 50 );

Hopefully that does the trick :)

Hi Tom,

Unfortunately, the change did not help :(

I have setup a fresh installation with my child theme. Is it possible, that I give you the admin Informations?
Maybe that helps with troubleshooting?

How can I send these Informations in private mode?

Thanks and best regards
Patrick

Thanks, send some seconds ago :)

Ah, I think I see what's wrong.

Our code that adds the post classes check so to make sure we're not on a singular post/page. In this case, we're on a singular page (with a template), so the classes aren't being added.

There's a way around it, but it's not super backward-compatible.

What if you used a standard custom post type archive instead of a page template? The adverts custom post type should have an archive created automatically.

Let me know :)

Hi Tom,

This would be ok for me too. But can you help me with that? I had no luck with creating an advert archive because it redirects everytime to the standard listing page from wpadverts.

Best regards Patrick

This archived topic is closed to new replies.