Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

jinseiichido on "Please help me converting this login in python to php(wp)"

$
0
0

Hello.

I have come up with this idea in python but being unfamiliar with php is not helping so I would like some help converting this python logic to php that can work with wordpress.

I want to get tags info assigned to a main article in single.php.
Then I have a dictionary(python) setup so a tagId is assigned an image.
so tag1 is assigned to image1.jpg
tag2 is assigned to image2.jpg and so on.
I want to display 4 different images on web, that are assigned to associated tags.
If the article has more than 4 tags assigned then I want to get 4 random tags so i can only display 4 images. If there are less than 4 tags assigned to the article then I just want to display them all.

here is the logic in python, hope someone can help. thank you.

# python
# in single.php
# get tags of this article
tags = getTagsOfThisArticle()
# assign images to tagIds
assignments = {"tagId1":"/img/path/1.jpg","tagId2":"/img/path/2.jpg","tagId3":"/img/path/3.jpg"}
# get 4 random tags - not the cleverest method but you get the idea
tagsRandom = []
import random
if (len(tags) > 4): # if there are more than 4 tags assigned
    random.sample(tags, 4) # get 4 random tags
else:
    tagsRandom = tags
# at this point, i should have 4 random tags(or less) this article is assigned to
for (tag in tagsRandom):
    # display the images
    print '<img src="'+assignments[tag]+'">

Viewing all articles
Browse latest Browse all 8245

Trending Articles