• I’m trying to automatically add a relevant hyperlinked logo to a post based on how the post is categorized. For example if the post is about Coca-Cola, the Coke logo should be displayed and linked to the Coke website.

    I thought I would create a new content type for all logos/links. Is there a way that I can associate these new pieces of content with posts based on categories and pull them into the posts in my template? Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • When you say a new content type, do you mean you’ll have a taxonomy or category that stores this information somewhere?

    From what I see, the easiest way to do this would be to build an array like this:

    <pre>
    <?php
    // Paste this code in functions.php
    // coke pepse sprite are shown like slugs here, you can also just use IDs (that would be easier)
    
    $category_extra_info = array(
    	'coke' => array(
    			'link' => 'http://domain.com',
    			'image' => 'http://domain.com/logo.png'
    			),
    	'pepsi' => array(
    			'link' => 'http://domain2.com',
    			'image' => 'http://domain2.com/logo.png'
    			),
    	'sprite' => array(
    			'link' => 'http://domain3.com',
    			'image' => 'http://domain3.com/logo.png'
    			),
    );
    
    // And then use it like this anywhere in template
    echo '<a href="' . $category_extra_info['coke']['link'] . '" rel="nofollow"><img src="' . $category_extra_info['coke']['image'] . '" alt="logo" /></a>';
    ?>
    </pre>

    If you want to autoreplace words and make things even more dynamic, you can do it this way:

    <?php
    // Paste this code in functions.php
    // coke pepse sprite are shown like slugs here, you can also just use IDs (that would be easier)
    
    $category_extra_info = array(
            'coke' => array(
                            'link' => 'http://domain.com',
                            'image' => 'http://domain.com/logo.png'
                            ),
            'pepsi' => array(
                            'link' => 'http://domain2.com',
                            'image' => 'http://domain2.com/logo.png'
                            ),
            'sprite' => array(
                            'link' => 'http://domain3.com',
                            'image' => 'http://domain3.com/logo.png'
                            ),
    );
    
    function category_extra_info_parsing( $text ){
            // make it accessible
            global $category_extra_info;
    
            // loop and build the $replace array
            foreach ( $category_extra_info as $cat => $info ) {
                    $replace[ $cat ] = '<a href="' . $category_extra_info['coke']['link'] . '">' . $cat . '</a>';
            }
    
            // replace words now
            $text = str_replace(array_keys($replace), $replace, $text);
    
            //return modified content
            return $text;
    }
    
    add_filter('the_content', 'category_extra_info_parsing');
    
    // add_filter('the_excerpt', 'category_extra_info_parsing'); // Uncomment this line for parsing the category words
    
    // And then use it like this in your template
    // echo '<a href="' . $category_extra_info['coke']['link'] . '"><img src="' . $category_extra_info['coke']['image'] . '" alt="logo" /></a>';
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Custom Content Within Post’ is closed to new replies.