Get custom fields value on archive page - wordpress

I want to display "Custom Field Value" in a archive page for wordpress. I tried
<?php echo get_post_meta($post->ID,'field_name',true); ?> but data can not display on archive page. what can i do?

Add this code your related page of archive.
<?php echo get_post_meta(get_the_ID(),'field_name',true); ?>
I have tested both code working fine. Please add code on archive.php just after while loop for test.

Related

Wordpress: Implement basic HTML form functionality to retrieve data and visualize the results

I have experience in HMTL/PHP but I cannot understand how someone could implement this basic functionality in Wordpress. I could not find an article or web page on their website or by searching through the internet.
I want to create a basic html form, pass some data as select queries in a database and visualize back the results paginated.
In traditional PHP we are using "action" in order to send the form data through POST or GET to the action page and then with some PHP code we can fetch the data and visualize it with tables etc.
I cannot understand how to do such a thing in a wordpress page. Where the action parameter should point to? How could I implement this basic functionality in WordPress?
Please could someone help?
This is the mode to develop a separated .php file which uses your wordpress theme and can access to almost all wordpress functions:
<?php include(’wp-blog-header.php’); ?>
<?php get_header(); ?>
<!– Your code here –>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Then you can use all WordPress functions cited here: http://codex.wordpress.org/Function_Reference
You can save this file anywhere, but be sure to insert correct path to wp-blog-header and there must not be any prohibiting .htaccess
This is the way to insert php code into a wordpress post:
You have to use this plugin http://wordpress.org/plugins/allow-php-in-posts-and-pages/
Then create a post and use [php] [/php] to insert your php code and open it. Look at the address bar. This it the URL to access this post. Use it as action parameter in your form. Then control $_REQUEST[] in your php code to extract parameters received from your form.
Now you can control this post as any other normal wordpress post from the wordpress admin panel.
You need to create custom wordpress templates for pages in your theme. Here i use templates
1.form-page.php --- with template name "Form-page-template" and
2.form-page-action.php ---with template name "Form-page-action-template"
form-page.php
<?php
/*
Template Name: Form-page-template
*/
?>
<?php get_header(); ?>
<form method="post" action="http://yourdomain.com/form-page-action/" name="input-form"/>
<!-- form contents -->
</form>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
You just save this template inside your theme : location => like wp-contents/themes/your-theme/form-page.php . and this will add Form-page-template in your theme .Now create a page inside from wordpress dashboard through pages->addnew ,here i give page name "form-page" and select template for page from right pannel,here we need to select "Form-page-template" that we created early.
Now we have the page url :: http://yourdomain.com/form-page/ where we can see our form,now create form-action-page.
2.form-page-action.php
<?php
/*
Template Name: Form-page-action-template
*/
?>
<?php get_header(); ?>
<!-- Your action page contents goes here -->
<?php
//getting input etc.. you need to do
$input = $post['input'];
?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Now you need to save this template inside your theme as above, Create a new page for this template as said above here i creating a page "form-page-action" with template "Form-page-action-template" so here i get a page url for our
action page like :: http://yourdomain.com/form-page-action/ , and you need to use this url as the action url in your form page.and this way you can add your form action page inside wordpress theme.
You can edit the contents of these page inside from wordpress like=> Appreance -> Editor , select the templates to edit.

add page title to latest posts frontpage in wordpress

I set my reading section to latest posts in Wordpress. My homepage logs fine but I would like to add a page title above the posts. I have looked in my theme's files and couldn't find the file to edit. As a test, I tried to add an image to my theme's loop.php and it appeared above the post on my homepage. The location was right but the image appeared on every one of my category blog list pages. How do I add a page title to the homepage that only appears on the homepage above the blog list?
Here are some resources for you to use, WordPress: https://codex.wordpress.org/Function_Reference/get_the_title
Eg:
you can use
<?php echo get_the_title(); ?>
or
<?php echo get_the_title($ID); ?>
There is lot of resources on WordPress, best of luck!

Enable shortcodes in a wordpress theme

I develop a new theme for wordpress 3.3.1 from scratch and shortcodes are not working on it.
As I searched until now it is a matter of filtering the content containing the shortcode, filter code added in a theme specific location(shorcodes are working with another theme).
So, my question is : What is the code for a general shortcode theme enable ?
To execute a single shortcode, run it with
echo do_shortcode('[your_short_code]');
If the shortcode(s) are in the post content, make sure you're displaying it with
<?php the_content();?>
Or
<?php echo apply_filters('the_content',$post_content);?>
Or
<?php echo apply_filters('the_content',$wp_query->post->post_content);?>
The important thing is: if you aren't using the function "the_content()" you need this line <?php echo apply_filters('the_content',$wp_query->post->post_content);?> where in the second argument you have to put the variable of the post content you want to show.
I had to save the theme's content to a variable and then use the second example.. Worked like a charm.
$mycontent = ot_get_option('rightcontent');
echo apply_filters('the_content',$mycontent);

page redirection in wordpress

what I have:
a google search form (displayed on all pages)
a wordpress page to display search results
what I want to do:
when a search is performed, redirect to the page containing the search results
how do I do this?
the search results page is a wordpress-created page with a permalink domain.com/search-results
if your template files doesn't have a file called "search.php" then create one,
this is were all the results will be shown, do a standard wp loop and style it..
thats if you were using the standard wp search, in this case
apply a template tag to the top of a new php page,
<?php
/*
Template Name: Search_Results
*/
?>
<?php get_header(); ?>
or include a new one
<?php include(TEMPLATEPATH . '/header_search.php'); ?>
<!-- INCLUDE GOOGLE SCRIPT -->
<?php get_sidebar();?>
<?php get_footer();?>
this should print out your results...

adding single.php page to wordpress or if condition for main page or post detail page

I use Barecity Theme for WordPress. i built everything, but now i need to add a single.php file to theme. now it displays all post content at homepage, I created a short_desc Custom Field. and I call it from code with;
<?php //get_post_meta($post->ID, 'short_desc', true); ?>
it is fine. but i need to display this short desc at home page listing, and the main content at details page. how can do that?
I appreciate helps!!
It sounds like what you are trying to do is show a custom field that you have set on a post on the index page (which lists all the posts).
To do that you'll need to modify index.php by adding your snippet where you would like to have the short description.
<?php echo get_post_meta($post->ID, 'short_desc', true); ?>
You need to use echo to display the results from the get_post_meta function.
Depending on how your posts are setup you can also use the More button when you write your posts. This will cut off your post at a certain point that you decide and only show that short part on the index and archive pages.
Another option would be to use
<?php the_excerpt(); ?>
Which shows the first 55 words (this can be adjusted though) of the post.
Hope that helps,
Paul

Resources