• Resolved utilibre

    (@utilibre)


    I am trying to add a little icon (jpg) next to every WordPress posts’ titles that have a specific custom field through types. I have defined a custom field for some posts like this: $custom = 2 and i want these posts’ titles to include an icon.

    I have this but it doesn’t work when i add it in functions.php

    $key = ‘custom’;
    if (get_post_meta($post->ID, $key, true) == 2){
    echo /icon.jpg
    }

    https://wordpress.org/plugins/types/

Viewing 13 replies - 1 through 13 (of 13 total)
  • hi utilibre,

    Types prepend “wpcf-” before your custom field slug,
    Please try like this:

    $key = 'custom';
    if (get_post_meta($post->ID, 'wpcf-' . $key, true) == 2){
    do something here...
    }

    Thread Starter utilibre

    (@utilibre)

    Hi wdisk, yes i know the wpcf code but my issue is the integration of the code. In my case, it is wpcf-premium. So would you know where i should add the below code and is the code right?

    $key = ‘custom’;
    if (get_post_meta($post->ID, ‘wpcf-premium’ . $key, true) == 2){
    echo (-icon-url-)
    }

    I understand you want to append the icon to the title. In this case, you need to setup a filter that changes the title, like this:

    add_filter('the_title', 'add_icon_to_title', 10, 2);
    function add_icon_to_title( $title, $post_id ) {
        if ( get_post_meta( $post_id, 'wpcf-premium', true ) == 2 ) {
            $title = 'ICON ' . $title;
        }
        return $title;
    }
    Thread Starter utilibre

    (@utilibre)

    Thanks Caridad, I added below to the child theme’s function but no luck (no changes):

    /*add icon to the title*/
    add_filter('the_title', 'add_icon_to_title', 10, 2);
    function add_icon_to_title( $title, $post_id ) {
        if ( get_post_meta( $post_id, 'wpcf-type', true ) == 2 ) {
            $title = 'http://s1.wsj.net/img/icon_key.png' . $title;
        }
        return $title;
    };

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    *I put the icon image address of wsj as example

    You need to add the whole HTML tag, not just the URL:

    add_filter('the_title', 'add_icon_to_title', 10, 2);
    function add_icon_to_title( $title, $post_id ) {
        if ( get_post_meta( $post_id, 'wpcf-type', true ) == 2 ) {
            $title = '<img src="http://s1.wsj.net/img/icon_key.png"> ' . $title;
        }
        return $title;
    };

    Make sure you place the code between the PHP tags in the functions.php file of your theme.

    Thread Starter utilibre

    (@utilibre)

    still no luck. What is that “10” and “2” for?

    10 is the priority. You may try using a higher number.
    2 is the number of parameters that the function will receive, leave that one as it is.

    You have more information over here:
    http://codex.wordpress.org/Function_Reference/add_filter

    Thread Starter utilibre

    (@utilibre)

    sorry but still no luck, i changed 10 to 20 but no see no changes.
    I also added php codes but no luck. 🙁

    Have you enabled WP_DEBUG in your wp-config.php file? There might be an error message there that explain why this happens.

    Thread Starter utilibre

    (@utilibre)

    yes tried it but it doesn’t give any errors at all

    Do you mind opening a support thread in our website?
    You can share your credentials there so we can review whats wrong.
    http://wp-types.com/forums/forum/general-and-pre-sales-questions/

    Thread Starter utilibre

    (@utilibre)

    no problem, i just created an account and added the info there.

    Thread Starter utilibre

    (@utilibre)

    All, thanks to types admin, Adriano, and Caridad for this”

    Apparently, I had to type the value name for the second one. This is how I fixed it:

    add_filter(‘the_title’, ‘add_icon_to_title’, 10, 2);
    function add_icon_to_title( $title, $post_id ) {
    if ( get_post_meta( $post_id, ‘wpcf-type’, true ) == premium ) {
    $title = ‘<img src=”http://s1.wsj.net/img/icon_key.png”&gt; ‘ . $title;
    }
    return $title;
    };

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Adding icon next to WP post title based on custom field’ is closed to new replies.