I am trying to customise a Wordpress theme. I have a function in themes/functions.php that I would like to run on some pages.
I need to be to:
Detect the page ID to determine whether the function should execute
Determine which hook to attach the function to (preferably something like page load.
Cheers
The file functions.php is for theme specific functions called inside your theme. If this is a theme specific function then the function call should be in the header (or wherever you want the output of the function to appear) via <?php my_function() ?>.
Hooks are for plugins, not template specific code.
If you are inside The Loop, then you can call <?php the_ID(); ?> as WarrenB said. If you are outside of the loop, then <?php echo $post->ID?> will print the page ID.
To the questions you posed, given you want to select on id #9, run your loop like this:
<?php
query_posts('page_id=9');
if (have_posts()) : while (have_posts()) : the_post();
// Do whatever on post id #9
?>
<?php endwhile; else: ?>
// Do whatever on all the other posts
<?php endif; ?>
If this isn't the answer you're looking for, please add more information to your question.
Related
So I don't know if this is possible, or if there is a workaround, but I need to add a shortcode to my single.php template. However, inside the shortcode, I ideally need to call author meta data so that the shortcode renders the correct information...
It is something like this:
<? php do_shortcode('[form id="<? php the_author_meta( 'authorspecificformid' ); ?>"]') ?>
I know this doesn't work, but is there a workaround I can do?
you can construct this shortcode like this :
<?php
$authorId = get_the_author_meta("authorspecificformid");
echo do_shortcode("[form id=\"$authorId\"]");
In Wordpress we can split a post with <!--nextpage-->, if we put it in the code version of the post-editor. This is working for me. BUT what is, if I want to hardcode this in the themes loop file? How can I get this to work?
Assume I have something like this in my loop file:
<?php the_excerpt(); ?>
<?php the_content(); ?>
<?php wp_link_pages(foobarbaz); ?>
Obviously the following solution won't work:
<?php the_excerpt(); ?>
<!--nextpage-->
<?php the_content(); ?>
<?php wp_link_pages(foobarbaz); ?>
I have no clue where I could find the right php function that is executed when <!--nextpage--> gets parsed in the code editor.
The only solution I can think of is creating a new post with just <!--nextpage--> in it and somehow try to hardcode this specific post inside the loop file. But there has to be a much better and cleaner way of doing it...
Any suggestions?
Not sure what you're trying to do but you can control the number of posts that appear on an archive page on the Settings->Reading tab.
If you want to split your content in individual posts, I think the best way to do this is to hook into the 'the_content' filter. I'm not sure if you have criteria where you want to split the post but if it's word count and you don't want to use the_excerpt;, you can write a function (in your theme's functions.php file) like this:
add_filter('the_content', 'your_post_split');
function your_post_split($content) {
//do something with the content and insert <!--nextpage-->
return $content;
}
When you write filters, make sure you always return the variable coming into your function.
If you want to prepend your post with the excerpt text, try this:
function your_post_split($content) {
global $post;
$content = $post->post_excerpt . $content;
return $content;
}
Take <?php the_excerpt; ?> out of your template.
I have a wordpress website and I want to create a page that also has javascript and upon a certain user action the javascript calls the server with an AJAX request.
This is a GET request to a php script I created. Since I extended wordpress with my plugin I put this php in my plugin's folder.
The problem is that from this php script I want to access everything that wordpress offers, e.g. the database access, but I do not know how.
What do I have to include in this php file in order to access the functions offered by wordpress? I wanted to use database access so I included the wp-db.php file and declared the global wpdb variable, but it did not help.
Can anyone tell me how to accomplish this?
Thanks in advance!
Wordpress has it's own build in ajax: http://codex.wordpress.org/AJAX_in_Plugins
Use this instead of your own ajax script.
In this ajax script you can use all WP functions. It will also make your plugin more like wordpress standards.
As recommended in he WordPress Codex itself.
<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>
By using the blog header no database queries are executed by default, you have to supply the get_posts that will fetch what-ever articles you need based on your application's parameters:
<?php $posts = get_posts('numberposts=10&order=ASC&orderby=post_title'); ?>
<?php foreach ($posts as $post) : start_wp(); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
I have some custom post types, such as 'review'. I can't seem to find out how to make a section (e.g., www.mysite.com/reviews/) which works like the blog home page, but lists reviews instead of posts (with pagination and everything). I'd like to use a separate template for it too.
Create a page called reviews and then create a new template in your theme folder called page-reviews.php. Add all the necessary elements to the template and include a query_posts in front of your post loop. It should look like this:
<?php query_posts("post_type=reviews"); ?>
<?php if (have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" >
<h2><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</div><!-- Post ends -->
<?php endwhile; ?>
<?php else: ?>
<p>Sorry, we could not find what you were looking for</p>
<?php endif; wp_reset_query(); ?>
You need help getting .htaccess to convert your categories into hard URLs. I thought WP did this automatically so you'll want to look and make sure you have set up the directory permissions on WP so it can write to your .htaccess file.
Please read this guide and it will be cleared up.
Duplicate the file in your theme called "single.php" and rename it to "single-reviews.php"
Since "single.php" is used for your regular posts, you can append the name of the custom post type to the end of "single-" to automatically use that.
Now once inside of the file "single-reviews.php" you can adjust the layout to be how ever you want.
If you get a 404 error, or it is not showing you the correct layout, you may need to flush the rewrite rules. You can do this two ways.
Go to the permalinks page in your backend and it will sometimes auto flush them.
The best way to do it is in your "functions.php" file in your theme directory add the following code:
add_action ( 'init', 'flush_rewrite_rules' );
function flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
Create a new page called reviews. Create a new page template that calls the custom post type. Assign the page template to the page...
I'm pretty new to WordPress but have spent some 50 odd hours studying up on it, trying things out and such and have the feeling I got a pretty good handle on it now..
However the one thing I simply cannot get working is to have a page spit out a list of posts of a certain category.
Here is my example: http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters
I have a post that if I go to it directly works correctly, but does not show up on this page.
The post direct link.
The category id is '3' while the category name is 'typographic-posters'.
I have a custom page template for the typographic-posters page that looks like this:
<?php
/*
Template Name: Typographic Posters
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php if (in_category('3')): ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?=get_image('flutter-image');?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
Using this code however the page only shows gets the header, sidebar and nothing else..
If someone could help me out that would really help me get a handle on this filtering of wordpress categories.
Thanks for reading,
Jannis
in_category will only work outside of the loop on a single page. I suggest using the query_posts function to solve this problem. You may use query_posts('cat=3') or query_posts('category_name=typographic-posters') to get the posts you are looking for.
Once obtained, just use the normal WordPress loop to access these posts.
The easiest way is to create a file called category-3.php and use the standard code from normal index.php or category.php file. Wordpress will take care of fetching posts only from category with id=3 and it's child categories.
in_category will only work outside of the loop on a single page. I
suggest using the query_posts function to solve this problem. You may
use query_posts('cat=3') or
query_posts('category_name=typographic-posters') to get the posts you
are looking for.
Once obtained, just use the normal WordPress loop to access these
posts.
This worked excellent, but make sure that you go into Settings > Reading and set the posts page to the -- Select -- option or it will override this query and dump all recent posts there regardless of category.
Simply add before the loop:
<?php query_posts="cat=3&showposts=5">
This will force the loop to display 5 posts (showposts=5) from category 3 (cat=3).
I would 2nd Eimantas' suggestion. The Template Hierarchy will use the category-3.php to display posts in that category. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need. Plus the category template will better support pagination of posts.
But if you need to stick with a Page to display those posts, also see the Page of Posts example.
http://codex.wordpress.org/Template_Tags/query_posts
Just so you know where these answers are coming from...there are a lot more interesting functions you can do with query_posts as well.
This plugin could also help you if you want to be able to change the displayed categories without going through the code :
http://wordpress.org/extend/plugins/advanced-category-excluder/
I have filtered post by category Id using the method below:
query_posts('cat=1&showposts=3');
if (have_posts()) : while (have_posts()) :
// if(1) {
//echo the_category_ID();
the_post();
/**
* The default post formatting from the post.php template file will be used.
* If you want to customize the post formatting for your homepage:
*
* - Create a new file: post-homepage.php
* - Copy/Paste the content of post.php to post-homepage.php
* - Edit and customize the post-homepage.php file for your needs.
*
* Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part
*/
$is_post_wrap++;
if($is_post_wrap == '1') {
?><div class="post-wrap clearfix"><?php
}
get_template_part('post', 'homepage');
if($is_post_wrap == '3') {
$is_post_wrap = 0;
?></div><?php
}
endwhile;
else :
get_template_part('post', 'noresults');
endif;
thank you for sharing on your thought its a great thought. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need