Custom Fields
Re-Envisioned
Support MB Custom Post Type Permalink for CPT Archive of specific category

  • Creator
    Topic
  • #28513
    Resolved Jorge
    Participant

    On a regular post, we've got the /blog/category/category-slug/ structure. Is it possible to have the same structure for a custom post type?

    I've got a CPT, that has the Category taxonomy registered (default WP) and has got Archives enabled. The individual post and the archive page works without any issue. What I'd like to have as well is an "archive" page that will only show the CPT entries that have got a specific category assigned.

    What would be the best way to do that? Thanks a lot!

Viewing 10 replies - 1 through 10 (of 13 total)
  • Author
    Replies
  • #28525
    Long Nguyen
    Moderator

    Hi Jorge,

    You can create a custom taxonomy and assign it to the CPT, no need to use the Category default of WordPress which is assigned to the Post.

    Then use this plugin to create a custom permalink for the custom taxonomy https://wordpress.org/plugins/custom-post-type-permalinks/

    #28539
    Jorge
    Participant

    Thanks Long,

    Is there any "actual" difference/advantage on having a custom Taxonomy over the default WP one?

    I'll toy around with the CPT permalink plugin, thanks for the link!

    #28545
    Jorge
    Participant

    I've installed the plugin and did a custom taxonomy, so the archive for the category is working!
    However, tinkering with the view for it, if I select to render for the whole page layout or the layout between header and footer, it works just fine, and loads the layout without any issue.

    But if I select to render only in the post content area with any of the three options it doesn't work.

    The page displays the regular loop and it shows the metabox' view sanitized, no tags, classes, links, anything. Just plain text repeated after every item on the loop.

    I'm not sure what could I be doing wrong here. :/

    #28572
    Long Nguyen
    Moderator

    Hi Jorge,

    If you set the view render for the post content area in the Archive page, the view content will replace each post content in the list posts. Please refer to this topic https://metabox.io/support/topic/tag-archive-my-view-gets-ignored-when-rendering-only-the-post-content-area/

    #28575
    Jorge
    Participant

    Hi Long, thank you the video explains way better the situation, it's exactly that. I understand that it is in fact working. So let me rephrase/clarify my question:

    Is it possible to generate a metabox view that is used on a dynamic generated page (an archive page) to be displayed at only the post content area, replacing the post content?


    In the mean time, I'm trying the shortcode route (which would render the archive page "useless", which is not necessarily a bad thing), using this thread as reference.

    So, I set up the code like this, just to see if it works:

    <div class="doc-container">    
        {% set args = {post_type: 'documento', posts_per_page: -1} %}
        {% set posts = mb.get_posts( args ) %}
        {% for post in posts %}
            <p>{{ post.title }}</p>
        {% endfor %}    
    </div>

    The post is configured to be requested via query var, to be shown in REST, to be publicly queryable, it is not excluded from search and it's public.

    Placed the shortcode on a regular page. And the shortcode does create the div.doc-container and creates 2 empty p tags (which is expected, since there are only 2 posts published. Am I missing something here?

    #28583
    Long Nguyen
    Moderator

    Hi,

    As on this post, you can access the properties of the post object when using a custom query.

    <p>{{ post.post_title }}</p>

    #28708
    Jorge
    Participant

    Sorry for the late reply, as you can see on my previous post, I've got exactly the same structure however this <p>{{ post.post_title }}</p> returns empty. As well if I add the custom field (which is a File Advanced type). The shortcode, displays the whole html layout, but not the content from the query.

    <div class="doc-container">    
        {% set args = {post_type: 'documento', posts_per_page: -1} %}
        {% set posts = mb.get_posts( args ) %}
        {% for post in posts %}
            <p>This - {{ post.title }}</p>
            {% for item in post.descarga_archivo %}
                <a href="{{ item.url }}">{{ item.title }}</a>
            {% endfor %}
        {% endfor %}
    </div>

    So I'm not sure if I need to do the query on a different way, or if it's something wrong with the one I'm making.

    #28718
    Long Nguyen
    Moderator

    Hi Jorge,

    Please follow this documentation to know how to use the Custom Query https://docs.metabox.io/extensions/mb-views/#custom-query

    Let me know how it goes.

    #28719
    Jorge
    Participant

    Thanks Long, that solves the "Title" part, which now looks like this: <p>{{ post.post_title }}</p> and works great.

    I've got a File Advanced field on the post. On the documentation I couldn't find any reference on how to call those. I'm inserting it using the "Insert Field" button without any other change. Which I think it's not part of the post object, from what I understand is that I'd need to use the rwmb_meta() helper, but I am not sure on how to use it there (and how to call the File Advanced in there). Is there any example I could use to get this going properly? (I'm starting to feel a bit over my head with the custom fields thing).

    Thanks!

    #28727
    Long Nguyen
    Moderator

    Hi Jorge,

    Follow the documentation of the File Advanced field to know how to show file information https://docs.metabox.io/fields/file-advanced/#template-usage

    You need to create a for loop to show the file info, here is the sample code

    {% for post in posts %}
        {% set files = mb.rwmb_meta( 'descarga_archivo', '', post.ID ) %}
        {% for item in files %}
            <a href="{{ item.url }}">{{ item.name }}</a>
        {% endfor %}
    {% endfor %}
Viewing 10 replies - 1 through 10 (of 13 total)
  • You must be logged in to reply to this topic.