• Resolved justcarl

    (@justcarl)


    Dear support,

    Could you please assist me in creating a view that displays the title of the current custom post type (such as $post_title), which is linked to the corresponding file field?

    When I’m using the link field, where the output it FILE-URL, it still shows the filename as title instead of post_title.

    Thank you.

    Best regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author WPLake

    (@wplakeorg)

    Hi @justcarl,

    The task isn’t ordinary, so you’ll need to use some of our filters.
    I can suggest the following solution:

    • Add the $Post$ ‘Title’ field to your View (at the very top)
    • Add the following code to your theme, that will do the trick, and replace the file’s name with the current post title
    class FileWithPostTitle
    {
        private string $currentPostTitle = '';
    
        public function __construct()
        {
            // fixme use your view id instead of '5'
            add_filter('acf_views/view/field_markup/view_id=5', [$this, 'maybeModifyMarkup',], 10, 4);
        }
    
        /**
         * @param \org\wplake\acf_views\AcfView\FieldMetaInterface $fieldMeta
         */
        public function maybeModifyMarkup($fieldHTML, $fieldMeta, $fieldValue, $viewId): string
        {
            $name = $fieldMeta->getName();
    
            switch ($name) {
                case '_post_title':
                    $this->currentPostTitle = $fieldValue;
                    $fieldHTML = '';
                    break;
                // fixme use your file field name instead of 'file'
                case 'file':
                    // 1. replace the current link label with the stub
                    $fieldHTML = preg_replace('/(<a[^>]+>)([^<]+)(<\/a>)/', '$1_title_$3', $fieldHTML);
                    // 2. replace the stub with the current post title
                    $fieldHTML = str_replace('_title_', $this->currentPostTitle, $fieldHTML);
                    break;
            }
    
            return $fieldHTML;
        }
    }
    
    new FileWithPostTitle();

    Don’t forget to replace the values to yours (see fixme comments).

    Best regards.

    Thread Starter justcarl

    (@justcarl)

    This works like charm! Amazing. Thank you.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Combine Post_title and File type’ is closed to new replies.