[Resolved] Replace post title with custom field in a custom post archive

Home Forums Support [Resolved] Replace post title with custom field in a custom post archive

Home Forums Support Replace post title with custom field in a custom post archive

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1194273
    George

    Is there a filter I could use to replace post title with a custom field in a custom post archive loop and have this custom field also link to the actual post? Also, is there a way to do that for WP Show Posts, as well?

    #1194789
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    the_title() is the WordPress function responsible for displaying the title. It has a filter (the_title), but for some reason, that filter is applied all over the site (widget titles, menu items etc..). This means it’s impossible to target only the content titles when using the filter, unfortunately.

    For now, you’d likely need to create a custom page template with your custom title element.

    #1194852
    George

    Yeah, I could use an element hook to make it available where I want, though, isn’t that right?

    Anyway, I managed to display the custom title with a link to the single post in WP Show Posts like this:

    add_action( 'wpsp_before_title','wpsp_add_custom_meta' );
    function wpsp_add_custom_meta()
    {
        $meta = get_post_meta( get_the_ID(), 'custom_title', true );
        if ( isset( $meta ) && '' !== $meta )
    		$permalink = get_permalink( get_the_ID() );
            echo '<h4 class="custom-entry-title" itemprop="headline"><a href="' . $permalink . '"' . 'rel="bookmark"> ' . $meta . '</a>' . '</h4>';
    
    }

    Then, I used CSS to hide the original title:

    .wp-show-posts-entry-title {
    	display: none;
    }

    Is this a correct way of doing this also from an SEO perspective as well?

    #1195039
    Tom
    Lead Developer
    Lead Developer

    Instead of hiding it with CSS, you can use the settings to disable the title. That would be much better from an SEO perspective.

    #1195052
    George

    Hiding the title also hides the alternative title. Utilizing the wpsp_before_header hook does the job though so I guess, I will use that!

    Perfect, thanks, Tom!

    #1195583
    Tom
    Lead Developer
    Lead Developer
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.