General WordPress Related

(Difference between revisions)
Jump to: navigation, search
(Added "How to add a Read More link at end of excerpts")
(Added = Displaying value of custom field =)
(7 intermediate revisions by 2 users not shown)
Line 22: Line 22:
 
Source: http://ithemes.com/forum/index.php?/topic/9701-author-link-produces-404-page-not-found-error/#p45383
 
Source: http://ithemes.com/forum/index.php?/topic/9701-author-link-produces-404-page-not-found-error/#p45383
  
=How to show posts from only a certain category/categories or exclude posts from a certain category/categories=
+
=query_posts() examples=
  
 
References:
 
References:
Line 78: Line 78:
  
 
In the above, change "24" to the ID of category from which posts should NOT be shown. Posts from all other categories will appear.
 
In the above, change "24" to the ID of category from which posts should NOT be shown. Posts from all other categories will appear.
 +
 +
== Show only the first 2000 characters of latest post on posts page ==
 +
 +
Note: By using this method, HTML formatting and images etc will be shown in the text and will not get stripped. i.e., this is like using the_content() and not the_excerpt().
 +
 +
'''1.''' Add the following at the end of your theme's functions.php before the closing PHP tag:
 +
 +
<pre class="brush:php;">
 +
function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
 +
    $content = get_the_content($more_link_text, $stripteaser, $more_file);
 +
    $content = apply_filters('the_content', $content);
 +
 +
  if (strlen($_GET['p']) > 0) {
 +
      echo $content;
 +
  }
 +
  else if ((strlen($content)>$max_char) && ($space = strpos($content, " ", $max_char ))) {
 +
        $content = substr($content, 0, $space);
 +
        echo $content = $content . " <a href='"; the_permalink(); echo "'>".$more_link_text."</a>";
 +
  }
 +
  else {
 +
      echo $content;
 +
  }
 +
}
 +
</pre>
 +
 +
Source: http://ithemes.com/codex/page/Builder_Tips_and_Tricks#Limit_the_content_of_a_post_that_is_displayed_on_a_blog_index_page
 +
 +
'''2.''' Edit theme's home.php.
 +
 +
Add
 +
 +
<pre class="brush:php;">
 +
<?php if ( is_home() ) {
 +
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("posts_per_page=1&paged=$paged");
 +
}?>
 +
</pre>
 +
 +
above
 +
 +
<pre class="brush:php;">
 +
<?php while ( have_posts() ) : // The Loop ?>
 +
</pre>
 +
 +
'''3.''' Replace the_content() or the_excerpt() with
 +
 +
<pre class="brush:php;">
 +
<?php the_content_limit(2000, '...<br/><br/>Read the entire article'); ?>
 +
</pre>
 +
 +
Note: In some themes, the_content() will be like <code><?php the_content( __( 'Read More&rarr;', 'it-l10n-BuilderChild-Classen' ) ); ?></code>
 +
 +
== Show posts in alphabetical order (A to Z) ==
 +
 +
Edit the appropriate [http://codex.wordpress.org/File:Template_Hierarchy.png template file]. For example, if you want this only in category and archive pages then edit child theme's archive.php. If this file is not present, copy it from parent directory.
 +
 +
Add
 +
 +
<pre class="brush:php;">
 +
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("orderby=title&order=ASC&paged=$paged"); ?>
 +
</pre>
 +
 +
above
 +
 +
<pre class="brush:php;">
 +
<?php if ( have_posts() ) : ?>
 +
</pre>
 +
 +
== Show only sticky posts in WordPress ==
 +
 +
Edit the appropriate [http://codex.wordpress.org/File:Template_Hierarchy.png template file]. To show just the sticky posts in Posts page, edit child theme's index.php.
 +
 +
Add
 +
 +
<pre class="brush:php;">
 +
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts(array('post__in'=>get_option('sticky_posts'))); ?>
 +
</pre>
 +
 +
above the loop which might look like
 +
 +
<pre class="brush:php;">
 +
<?php while ( have_posts() ) : // The Loop ?>
 +
</pre>
 +
 +
Source: http://wordpress.org/support/topic/only-display-sticky-posts-on-27-homepage?replies=5#post-940354
  
 
=How to add a Read More link at end of excerpts=
 
=How to add a Read More link at end of excerpts=
Line 97: Line 181:
 
Source: http://ithemes.com/forum/index.php?/topic/11063-hybrid-of-americana-and-entree-pub-possible/#p52604
 
Source: http://ithemes.com/forum/index.php?/topic/11063-hybrid-of-americana-and-entree-pub-possible/#p52604
  
=Fourth entry=
+
=How to make shortcodes work in manual excerpts=
 +
 
 +
Add the following before closing PHP tag in functions.php:
 +
 
 +
<pre class="brush:php;">
 +
add_filter('the_excerpt', 'do_shortcode');
 +
</pre>
 +
 
 +
Note: If a extension (in Builder) is being used, then functions.php is the one inside extension's directory.
 +
 
 +
[http://www.aakashweb.com/forum/post385.html?sid=7129a5fdc5272f226a2b7ba0f5a4a0bf#p385 Source]
 +
 
 +
= How to update a plugin =
 +
 
 +
Not all plugins can be updated from the dashboard. In such cases the recommended method to update a WordPress plugin is to use [http://wordpress.org/extend/plugins/easy-theme-and-plugin-upgrades Easy Theme and Plugin Upgrades] plugin. If this plugin does not work, use a FTP client or cPanel file manager to replace the plugin's folder with the latest version.
 +
 
 +
= Editing or Adding Stuff to Functions.php =
 +
 
 +
Sometimes you have to add extra custom code to the functions.php file in your theme's folder.
 +
 
 +
For anything that involves customizing functions.php, you can put the custom code anywhere in the file, usually near the end of the file. Your functions.php file may instruct you, via comments inside it, where to put any custom code.
 +
 
 +
= Displaying value of custom field =
 +
 
 +
It is possible to create custom fields when editing a Post or a Page and set values for them.
 +
 
 +
Ex.:
 +
 
 +
[[File:Screen Shot 2012-06-23 at 2.12.19 PM.png|556px|thumb|none]]
 +
 
 +
The value of this custom field can be pulled and shown in any WordPress template file using the following sample code:
 +
 
 +
<pre class="brush:php;">
 +
<?php
 +
global $post;
 +
$price = get_post_meta($post->ID, 'price', true);
 +
if ( !empty($price) ) { echo 'Price: ' . $price; }
 +
?>
 +
</pre>
 +
 
 +
The above code checks for a custom field named "price", fetches its values and stores it in a variable called $price, checks to see if it is empty and if it's not empty will be shown on the page preceded by a text string.
 +
 
 +
[[File:Screen Shot 2012-06-23 at 3.04 PM.png|598px|thumb|none|Screenshot of functions.php in Magazine extension inside child theme directory]]
 +
 
 +
[[File:Screen Shot 2012-06-23 at 3.05 PM.png|746px|thumb|none]]

Revision as of 04:40, June 23, 2012

Contents

Author page Not Found

Problem: Clicking on the author name link (Ex.: http://www.leonardowood.com/author/Leonardo%20Wood/) in posts shows Page Not Found.

Solution:

The author link is supposed to be http://www.leonardowood.com/author/leonardo-wood/

Add the following to your theme's functions.php, reload your site twice.

global $wpdb;
 $wpdb->query("UPDATE `{$wpdb->users}` SET `user_nicename`='leonardo-wood' WHERE `ID`='1'");

In the above replace "leonardo-wood" with your user nicename (User name in small case with hypens between spaces).

The above also assumes that ID of user in question is 1. This can be checked via phpMyAdmin in wp_users table.

After ensuring that this has been fixed, remove the above code from functions.php.

Source: http://ithemes.com/forum/index.php?/topic/9701-author-link-produces-404-page-not-found-error/#p45383

query_posts() examples

References:

  1. http://codex.wordpress.org/Function_Reference/query_posts
  2. http://stylizedweb.com/2008/08/13/query_posts-pagination-problem/

Category ID can be seen by going to Posts -> Categories, placing the mouse cursor on desired category and observing the number at the end of URL in the browser status bar.

Only posts from a certain category should appear on Posts page

How to: Edit your theme's index.php.

Ex.: Change

	<?php if ( have_posts() ) : ?>
		<?php while ( have_posts() ) : // The Loop ?>
			<?php the_post(); ?>

to

	<?php if ( have_posts() ) : ?>
	<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("cat=1&paged=$paged"); ?>
		<?php while ( have_posts() ) : // The Loop ?>
			<?php the_post(); ?>

In the above, change "1" to the ID of category from which posts should be shown. Posts from all other categories won't appear.

Category ID can be seen by going to Posts -> Categories, placing the mouse cursor on desired category and observing the number at the end of URL in the browser status bar.

Posts from a certain category should be excluded from Posts page

How to: Edit your theme's index.php.

Ex.: Change

	<?php if ( have_posts() ) : ?>
		<?php while ( have_posts() ) : // The Loop ?>
			<?php the_post(); ?>

to

	<?php if ( have_posts() ) : ?>
	<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("cat=-24&paged=$paged"); ?>
		<?php while ( have_posts() ) : // The Loop ?>
			<?php the_post(); ?>

In the above, change "24" to the ID of category from which posts should NOT be shown. Posts from all other categories will appear.

Show only the first 2000 characters of latest post on posts page

Note: By using this method, HTML formatting and images etc will be shown in the text and will not get stripped. i.e., this is like using the_content() and not the_excerpt().

1. Add the following at the end of your theme's functions.php before the closing PHP tag:

function the_content_limit($max_char, $more_link_text = '(more...)', $stripteaser = 0, $more_file = '') {
    $content = get_the_content($more_link_text, $stripteaser, $more_file);
    $content = apply_filters('the_content', $content);

   if (strlen($_GET['p']) > 0) {
      echo $content;
   }
   else if ((strlen($content)>$max_char) && ($space = strpos($content, " ", $max_char ))) {
        $content = substr($content, 0, $space);
        echo $content = $content . " <a href='"; the_permalink(); echo "'>".$more_link_text."</a>";
   }
   else {
      echo $content;
   }
}

Source: http://ithemes.com/codex/page/Builder_Tips_and_Tricks#Limit_the_content_of_a_post_that_is_displayed_on_a_blog_index_page

2. Edit theme's home.php.

Add

<?php if ( is_home() ) {
							$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("posts_per_page=1&paged=$paged"); 
							}?>

above

<?php while ( have_posts() ) : // The Loop ?>

3. Replace the_content() or the_excerpt() with

<?php the_content_limit(2000, '...<br/><br/>Read the entire article'); ?>

Note: In some themes, the_content() will be like <?php the_content( __( 'Read More→', 'it-l10n-BuilderChild-Classen' ) ); ?>

Show posts in alphabetical order (A to Z)

Edit the appropriate template file. For example, if you want this only in category and archive pages then edit child theme's archive.php. If this file is not present, copy it from parent directory.

Add

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("orderby=title&order=ASC&paged=$paged"); ?>

above

<?php if ( have_posts() ) : ?>

Show only sticky posts in WordPress

Edit the appropriate template file. To show just the sticky posts in Posts page, edit child theme's index.php.

Add

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts(array('post__in'=>get_option('sticky_posts'))); ?>

above the loop which might look like

<?php while ( have_posts() ) : // The Loop ?>

Source: http://wordpress.org/support/topic/only-display-sticky-posts-on-27-homepage?replies=5#post-940354

How to add a Read More link at end of excerpts

Edit theme's templates like index.php.

Replace

<?php the_excerpt(); ?>

with

<?php the_excerpt(); ?><span class="readmore"><a href="<?php the_permalink(); ?>">(Read More ...)</a></span>

Source: http://ithemes.com/forum/index.php?/topic/11063-hybrid-of-americana-and-entree-pub-possible/#p52604

How to make shortcodes work in manual excerpts

Add the following before closing PHP tag in functions.php:

add_filter('the_excerpt', 'do_shortcode');

Note: If a extension (in Builder) is being used, then functions.php is the one inside extension's directory.

Source

How to update a plugin

Not all plugins can be updated from the dashboard. In such cases the recommended method to update a WordPress plugin is to use Easy Theme and Plugin Upgrades plugin. If this plugin does not work, use a FTP client or cPanel file manager to replace the plugin's folder with the latest version.

Editing or Adding Stuff to Functions.php

Sometimes you have to add extra custom code to the functions.php file in your theme's folder.

For anything that involves customizing functions.php, you can put the custom code anywhere in the file, usually near the end of the file. Your functions.php file may instruct you, via comments inside it, where to put any custom code.

Displaying value of custom field

It is possible to create custom fields when editing a Post or a Page and set values for them.

Ex.:

Screen Shot 2012-06-23 at 2.12.19 PM.png

The value of this custom field can be pulled and shown in any WordPress template file using the following sample code:

<?php
	global $post;
	$price = get_post_meta($post->ID, 'price', true);
	if ( !empty($price) ) { echo 'Price: ' . $price; }
?>

The above code checks for a custom field named "price", fetches its values and stores it in a variable called $price, checks to see if it is empty and if it's not empty will be shown on the page preceded by a text string.

Screenshot of functions.php in Magazine extension inside child theme directory
Screen Shot 2012-06-23 at 3.05 PM.png
Personal tools
Namespaces
Variants
Actions
iThemes Codex
Codex Navigation
Toolbox