Site logo

Archived topic

Custom field in Archive element

32 replies · Started by Dan on September 21, 2021

Viewing posts 1–15 of 33

Hi,
I have a custom post type which has some custom fields in it. One of them is called 'Short description'.
In the archive page which displays the list of post in that CPT category I would like to display under the post the 'short description' which is a custom field.
currently, it is not displaying.
You can see the setting in the screenshot:
https://www.dropbox.com/s/wx0rrsz961n2ybx/post_meta.jpg?dl=0

And the output:
https://www.dropbox.com/s/b8cdil8l0mhtv7m/output.jpg?dl=0

Also, I would like to make all archive pages (categories and tags) of this custom post type display in 3 columns. can that be done via the block elements, or will it need some custom CSS?

Thanks!
Dan

Hi Ying,
Thanks for replying.
the custom field is short_description

Regarding the columns, I added this snippet

add_filter( 'generate_blog_columns','tu_portfolio_columns' );
function tu_portfolio_columns( $columns ) {
    if ( is_post_type_archive( 'catalog' ) ) {
        return 33;
    }

    return $columns;
}

But it's still 1 column

Dan

Does your blog has 3 columns set already? If so, you only need this PHP snippet:
https://docs.generatepress.com/article/using-columns-in-the-blog/#adding-columns-to-your-custom-post-type

If your blog has different columns number, then you'll need this PHP snippet as well:
https://docs.generatepress.com/article/using-columns-in-the-blog/#changing-the-number-of-columns

For the custom filed, your settings in the element looks right, just wondering what's the content highlighted in red?
https://www.screencast.com/t/AAVJ7lXk4R

Let me know.

I think I'll figure out the columns with custom CSS, thanks.
The content in red it the post's title.

Not sure why it doesn't work by looking at the screenshots.

Can you provide access to admin so we can have look at the back end?

The custom field is from an ACF within a block, could that influence the output?
The site is still in local dev, I'll upload to staging in a few days.

Another related question, I would like to make the main category (archive) page a static one, so I tried with elements but the output isn't right, it still creates some sort of loop (when using the 'content template' element type).
Would it be correct to create a 'page header' element, set the location to the custom post type archive, create the blocks I want to see and then hide the loop with CSS?

The site is still in local dev, I’ll upload to staging in a few days.

We can have a look then, let us know.

Would it be correct to create a ‘page header’ element, set the location to the custom post type archive, create the blocks I want to see and then hide the loop with CSS?

Doesn't sound right, in this way, all the content is still loading, not great for your site performance.

Give this PHP snippet a try:
https://docs.wpshowposts.com/article/use-static-pages-as-category-archives/

Let's say the category is called 'event' , you need to create a new static page named 'event', put everything you want to show in this static page.

Then set the Category base to . at settings > permalinks.

With the PHP snippet, the default category archive page will be replaced by the static page.

Thanks, Yes, I thought there may be a better way.
But I would like the static page be for the main archive of the CPT not for specific categories.
Is it possible to check if it's the archive of the CPT and then replace it with the static page (without causing issues for other categories and the default WP posts and categories)

In this case, you can try something like this:

function archive_to_custom_archive() {
    if( is_post_type_archive( 'event' ) ) {
        wp_redirect( home_url( '/events/' ), 301 );
        exit();
    }
}
add_action( 'template_redirect', 'archive_to_custom_archive' );

ok, so that would be a simple redirect to the static page?

Yes.

ok, thanks

This archived topic is closed to new replies.