Get post by its ID (WordPress) - php

How do I get a Custom Post by its ID in wordpress and show its thumbnail, title and content? Here is what I am using, but didnt the content is messed up.
<div class="left-cont1">
<div class="left-cont1-text">
<?php
query_posts('post_id=790&post_type=homepage');
while (have_posts()): the_post();
?>
<h1><?php the_title(); ?></h1>
<div class="cont1-border"></div>
<p><?php echo(types_render_field("homepage-content", array("raw"=>"true"))); ?></p>
<h2><?php echo(types_render_field("homepage-urltitle", array("raw"=>"true"))); ?></h2>
</div>
<div class="left-cont1-image1"><?php echo get_the_post_thumbnail($page->ID, 'home-circle'); ?></div>
<?php endwhile;?>
</div>
Thanks

You should be able to take your current code, change get_the_post_thumbnail($page->ID, 'home-circle') to get_the_post_thumbnail(get_the_id(), 'home-circle'), and replicate your code for each post you want to pull in, modifying the post_id=xxx to match the ID of the post you want to pull in.
This is not the most attractive solution, but from the information you gave, it should at least work. Just so you know, there is an entire StackExchange site devoted to Wordpress: https://wordpress.stackexchange.com/ where you should be able to get more Wordpress-specific answers. If your question is about PHP, please feel free to ask here, but if it is Wordpress-specific, your better bet would be the dedicated Wordpress site.
Since you're new to PHP, I would recommend getting an introductory book or two and try to get some of the basic concepts down pat before working with WordPress. While there will be a larger up-front investment of time, it will pay off in the long run. Good luck!

Related

Wpml and Custom Widgets

In WPML some times custom widgets developed could not get translated.
Any string is translatable if the you copy paste the string then that string if searchable in WPML is translatable.
I have a website that has 4-5 Custom widgets + some hard coded part
For example there was a string in hard coded part-
Bet on your favorite sports online
I made it WPML compatible-
WPML version
N.B. Before posting the question here I have invested around 3-4 hours in understanding all the FAQ's at wpml.org site.
My problem-
I have various widgets, one such widgets has this portion-
<div class="signupoffers">
<div class="signupText"> <h2><?php echo $signup_text_title?></h2>
<p><?php echo $signup_text_content?></p><br>
</div>
<div class="signupbuttonDiv">
<div class="RegisterBlock">
<div class="block"><h2><?php echo $signup_btn_title?></h2>
<div>
<?php echo $signup_btn_content; ?>
</div>
</div>
</div>
<?php //nsu_signup_form();?>
</div>
</div>
Lets take an example this one-
I tried this http://screencast.com/t/3hJCPJgylXS
but the above one doesn't worked.
and various other versions, but no help so far.
Content here is coming dynamically from widget entry. May be I have some syntax issue. Does any one has encountered such situation before, Please guide me how to do that. Thanks!!
Let me ask the question in different way-
The string here is coming in a dynamic format-
<h2><?php echo $signup_btn_title?></h2>
How to convert it into wpml version so that wpml can read and search this string.
I tried this one, but it didn't worked-
<h2><?php _e('(<?php echo $seocontent_text_title;?>)','Casino'); ?></h2>
<h2><?php _e('(<?php echo $seocontent_text_title;?>)','Casino'); ?></h2>
this is definitely wrong
If any of similar approach, it should read similarly to this:
<?php _e( $seocontent_text_title, 'your_text_domain' ); ?>
You would probably also need to edit/create a wpml-config.xml file.
You could also try something along this lines:
(icl_translate function in strings)
<?php echo icl_translate(context, name, value); ?>">
I think this could help you to understand further:
https://wpml.org/forums/topic/transform-echo-to-php_e/
https://wpml.org/documentation/support/translation-for-texts-by-other-plugins-and-themes/
https://wpml.org/documentation/support/wpml-coding-api/

PHP Variables URL news page

To get it out of the way, I am learning php as I go and I am new to it. I am really trying to get my hands dirty in it on this project to learn and to make my life easier!
I have a scenario, and I would love for someone to help me out through it and point me in the right direction.
My scenario is:
I have a latest news page on my 'static' website.
I am using php includes on the header, navbar and footer for every page.
I am using includes to grab the latest news information I have in assets/news/form/news1.php. This is the following:
<?php
if (!defined("ACCESS"))
{
die ("Don't waste your time trying to access this file!! Please head to the families relief home page and donate!!");
}
?>
<?php
$header = 'Article Header';
$date = 'June 1st 2013';
$url = '/news/articleName';
$body = 'Body in HTML';
?>
I then have another file in assets/news/news1.php containing:
<div id="appeal3" class="row-fluid">
<div class="well span12">
<div class="span3">
<div class="thumbnail">
<img src="/img/news-blank.jpg">
</div>
</div><!-- span3 -->
<div class="span9 appeals">
<p class="app-date"><?php echo $date?></p>
<h3><?php echo $header ?></h3>
<p><?php echo substr($body, 0, 200) ?><span class="readmore"> ...Read More ></span></p>
</div><!-- span9 -->
</div><!-- well span12 -->
</div><!-- appeal# -->
I then use the following code on my news overview page at /news/index.php to display the html with the variables entered:
<?php include("$_SERVER[DOCUMENT_ROOT]/assets/news/news1.php"); ?>
So what this gives me is an overview page, with the news article displayed around a wrapper in overview view.
I then have another page when the overview 'read more' is clicked.
Within this page, I have a full view news article page with a wrapper where I can echo the variables in.
Wha La! That is my news article structure.. Long winded isn't it!
Now, what I want!! I want to be able to include the variables from an article into my overview page and include the 2nd block of code above as the template. I then want the articles to fill out the variables. However, I have 5 articles on this page with identical variables. What is the best and most efficient way to do this??
Secondly, I have a full-view article page. How can I use this page as a template, and change the php it gets it's variables from. But, I don't want to create a new php page every time I have a new article.
What I had in mind, is something like the following:
domain.com/news/article/index.php?news1
I have been reading up on passing variables in a URL. I want to expand my knowledge on this. Could this be the solution to my problem. My issue is that I have NO idea how to go about this? Can someone please lecture me to solve this and hopefully teach me a little in between!
Thanks!

Editing certain parts of page template in User mode

I have a web page I am working on in WP which needs some customization.
Firstly I have created a page template that will be used over and over again, only changing parts of the content. I am wondering about available options for me when enabling this fact, through the Admin panel in the HTML Editor(if possible)...
Hope my question is clear enough for you all.
Let me add some code to show what I am trying to accomplish.
<div id="header-style">
<?php get_header();?>
</div>
<div id="content">
<div id="about">
//This is what i want to be able to edit
</div>
<div id="features">
//This is what i want to be able to edit
</div>
</div>
Dino:
There are lots of ways you could do this. The main question I would ask you is, who is going to be adding/editing this content? If you're going to have a community of people adding content, the input needs to be stripped and sanitized (to avoid injecting tags or other harmful content). If its just going to be you, then here's the easiest/fastest solution:
Use custom fields. If you can't see them in the post/page edit screen, go to the little tab on the top right of the post-edit screen that says Screen Options (or something like that) and click "Custom Fields".
Once you can see the Custom Fields edit box, you can add as many fields as you want. These are stored as post meta data. You can use the <?php the_meta(); ?> function in the loop to display all of your custom fields.
You can access a specific field by using get_post_meta(). You pass in the postID and the key of the meta field:
<?php echo get_post_meta(get_the_ID(), 'my_key'); ?>
So, for your example, you would add in the post-edit screen:
about: Some text to go in the about section.
features: Some text to go in the features section.
Then, you would access these on your page like so:
<div id="header-style">
<?php get_header();?>
</div>
<div id="content">
<div id="about">
<?php echo get_post_meta(get_the_ID(), 'about'); ?>
</div>
<div id="features">
<?php echo get_post_meta(get_the_ID(), 'features'); ?>
</div>
</div>

Using columns in Wordpress page that include header

I'm a little bit of a newb when it comes to PHP, but know enough to get around and would like to know if this can be done. sample
I want to break my wordpress page into 2 columns, but also want to have the header in the 1st column.... along with other text. I don't want the header floating over both columns...
The second column will house images only...
is that possible? In my head it makes sense, but then when I try and work it out, I'm just not sure....
And I just got thinking... I have my home page static with the smooth slider on it, so that is now going to cause more grief.
Any help, advice or pointers would be greatly appreciated.
Thanks in advance
This is more CSS/HTML than PHP, however that's fine. The first thing you need to do is understand how to make a two column layout. Then you will need to have the post title in the first column, something like this:
<article>
<div id="col1">
<h1>Post Title</h1>
Lorem ipsum dolor sit amet...
</div>
<div id="col2">
<img src="" />
</div>
</article>
To make this into WordPress you will of course need to add the WordPress Tags:
<div id="col1">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</div>
Finally, adding the image(s) on the right. It can be done easily using WordPress' built in functionality, if you only need one image: (Note you will have to add something in your theme's functions.php file as per the WordPress Docs)
<div id="col2">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
?>
</div>
To add multiple images, it gets more complex and you'll have to start looking for a plugin to achieve that goal.

Wordpress and Internal Page Links

I'm trying to construct a Table of Content on a custom template in Wordpress. Because I already know beforehand the headings of the internal sections I want to link to I hardcoded that into the template.
My problem is that in Wordpress it doesn't scroll to that section at all? Here's the link to a page with an existing TOC section that's not working.
In terms of code, this is an illustration of what I got:
<div class="table_content">
<h4>Table of Contents</h4>
<ol>
<li>Overview</li>
</ol>
</div>
Which should link to an internal section with the code:
<h2><a id="#test_link" class="internal"><?php the_title(); ?></a></h2>
Note: I'm using "ID" instead of "name" because it's deprecated in HTML5, which is the doctype I'm using.
I'm also wondering is there an easier way of doing this with jQuery?
Any help in saving my hairline would be GREATLY appreciated...
Nevermind, I'm a muppet.
It's got nothing to do with doctypes or Wordpress issues. Just my incorrect, sleep-deprived implementation of HTML.
This
<h2><a id="#test_link" class="internal"><?php the_title(); ?></a></h2>
should be
<h2><a id="test_link" class="internal"><?php the_title(); ?></a></h2>
Notice the missing "#" in the "ID" attribute, removing that makes everything work nicely :)
You don't need the separate a element...
http://www.yourhtmlsource.com/text/internallinks.html

Categories