Site logo

[Support request] Post Format (post_format) – Customize “Link” Formatted Posts

Home Forums Support [Support request] Post Format (post_format) – Customize “Link” Formatted Posts

Home Forums Support Post Format (post_format) – Customize “Link” Formatted Posts

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #2543339
    Stephen

    Hi

    I’m starting to experiment with different post formats and the first one I’m trying out is has_post_format(link) post_format=link.

    How would I begin to enable WP to output something like the below when I select “Link” in the “Post Format” drop down.

    <a href="https://URL_to_link_I_want_to_share.com"><h5>Header Text</h5></a>&nbsp;<a href="https://www.sgclark.com/permalink-on-my-site-to-link-post-12345 ">#</a>
    <p>Any text I want to add to the post. Lorum ipsum.  Lorum ipsum.</p>

    I know that when I select “Link” in the “Post Format” then WP reads the first link in the body of the post and applies that to the Header text/link of the post. So that functionality seems pretty baked in, however I have to put the URL I want to share in the body of the post. Is there a way to create a custom Compose area whenever “Link” is selected where I paste the link and type whatever extra text I want on the post and it outputs somewhat similar to above?

    I’ve been reading on the WP support area at this URL to try to figure out how to do this but I’m not fully sure where the snippets they talk about would go within the GeneratePress child theme I use on my site.
    https://developer.wordpress.org/themes/functionality/post-formats/#supported-formats

    I tried putting this code into the single.php file and it output ‘This is the link format’ at the bottom of posts selected as “Link”, however nothing else changed in the post. Should I put some custom php code after the ‘echo’ below to output the custom format that I want these posts to render?

    <?php
    if ( has_post_format( 'link' ) ) {
    	echo 'This is the link format.';
    }

    Not sure if you can help but I figured I give it a shot.
    Thanks
    Stephen
    http://www.sgclark.com

    #2543934
    David
    Staff
    Customer Support

    Hi there,

    when viewing an archive page, each post in the loop is output using the content.php template.
    that includes the function callback the_title() of which we use two of its params:

    https://developer.wordpress.org/reference/functions/the_title/

    The before and after param, both of which are filterable using the generate_get_the_title_parameters hook, which is how we do things like change the post title permalink to the first url in the content of the Post Format is a Link.

    1. Add this PHP Snippet:

    add_filter('generate_get_the_title_parameters', function($params) {
        if ( 'link' === get_post_format() ) {
            // set the custom field that will store your URL
            $custom_url = get_post_meta( get_the_ID(), 'your_custom_field', true );
            $url = $custom_url ? $custom_url : generate_get_link_url();
            $params = array(
            'before' => sprintf(
                '<h2 class="entry-title"%2$s><a href="%1$s" rel="bookmark">',
                esc_url( $url ),
                'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
                ),
                'after' => '</a></h2>',
            );
        }
        return $params;
    });

    2. on this line $custom_url = get_post_meta( get_the_ID(), 'your_custom_field', true ); set your custom field name.

    3, Then you just need to register a custom field to match that.
    You can use the core custom fields:
    https://wordpress.org/documentation/article/assign-custom-fields/
    or a plugin like ACF or metabox.

    Now on each post you will have the option to complete your custom field.
    If you do, then a post_format(‘link’) will use that, if not it will look for a URL in the content, and if it finds none then it will use the single post permalink

    #2592198
    Stephen

    Hi

    Following up on this item…several questions:

    1. For item #1 above, where do I put that snippet of code? In the “content.php” page/template?
    2. I don’t understand item #3 above. What is the purpose? Why do I need to do this?
    2. I see in the base Generate Press theme, there is a ‘content-link.php’ template. Is that the template that is used when I select the “Link” Post Format?

    To me, the more fundamental issue here is that when I go into WP Dashboard, and click “Add New” post and go to the post Dashboard interface, and then select “Link” (or any other ‘Post Format’) in the ‘Post Format’ drop down, why doesn’t the area where I input/publish content change to reflect the “Post Format” I have selected?

    To me (keeping with the “Link” example), posting a “Link” Post Format seems to be a quick post. So why doesn’t the “Add New” dashboard change to accommodate that? The Title should remain, but there should be a field to paste the URL and then above/under that there should be a Paragraph area if I want to add some additional text. And that’s it. I’d think that Comments should probably be automatically disabled and any other meta data should also be suppressed.

    #2592403
    David
    Staff
    Customer Support

    Lets cover the basics first.
    The post_format options are a legacy of WordPress that never really developed. They are to all intents and purposes a baked in taxonomy term. They do not in any way change the behaviour of the editor, instead WordPress provides functions such as extracting the first link from the post content. This is 100% WordPress, and the theme has no control over that, all the theme can do is provide front end templates that affect how post_Formats are displayed.

    #1 is PHP – this article explains how to add that :

    https://docs.generatepress.com/article/adding-php/

    #3 you asked for an area outside of the post content to add your link, to do that in WordPress you need to add your own custom field.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.